Skip to content

Commit 8207729

Browse files
committed
Add additional skip properties
Allow formatting to skipped and refine skip properties to allow individual goals to be skipped for both goals to be skipped. Closes gh-360
1 parent 097bb33 commit 8207729

File tree

12 files changed

+180
-3
lines changed

12 files changed

+180
-3
lines changed

README.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ NOTE: The source formatter does not fundamentally change your code.
7676
For example, it will not change the order of import statements.
7777
It is effectively limited to adding or removing whitespace and line feeds.
7878

79+
TIP: You can use `-Dspring-javaformat.validate.skip=true` or `-Dspring-javaformat.apply.skip=true` command line arguments to temporarily skip the validation or format goals.
80+
If you want to skip both, you can use `-Dspring-javaformat.skip=true`.
81+
7982

8083

8184
=== Checkstyle
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.spring.javaformat</groupId>
6+
<artifactId>apply-skip</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
12+
</properties>
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>@project.groupId@</groupId>
17+
<artifactId>@project.artifactId@</artifactId>
18+
<version>@project.version@</version>
19+
<executions>
20+
<execution>
21+
<goals>
22+
<goal>apply</goal>
23+
</goals>
24+
<configuration>
25+
<skip>true</skip>
26+
</configuration>
27+
</execution>
28+
</executions>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package simple;
2+
3+
/**
4+
* Simple.
5+
* @author Phillip Webb
6+
* @since 1.0.0
7+
*/
8+
public class Simple {
9+
10+
public static void main(String[] args) throws Exception {
11+
// Main method
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
new io.spring.format.maven.VerifyApply().verifyNoApply(basedir)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.spring.javaformat</groupId>
6+
<artifactId>validate-skip-global-property</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
12+
<spring-javaformat.skip>true</spring-javaformat.skip>
13+
</properties>
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>@project.groupId@</groupId>
18+
<artifactId>@project.artifactId@</artifactId>
19+
<version>@project.version@</version>
20+
<executions>
21+
<execution>
22+
<goals>
23+
<goal>validate</goal>
24+
</goals>
25+
</execution>
26+
</executions>
27+
</plugin>
28+
</plugins>
29+
</build>
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package simple;
2+
3+
/**
4+
* Simple.
5+
*
6+
* @author Phillip Webb
7+
* @since 1.0.0
8+
*/
9+
public class Simple {
10+
11+
public static void main(String[] args) throws Exception {
12+
// Main method
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.spring.javaformat</groupId>
6+
<artifactId>validate-skip-property</artifactId>
7+
<version>0.0.1.BUILD-SNAPSHOT</version>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
12+
<spring-javaformat.validate.skip>true</spring-javaformat.validate.skip>
13+
</properties>
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>@project.groupId@</groupId>
18+
<artifactId>@project.artifactId@</artifactId>
19+
<version>@project.version@</version>
20+
<executions>
21+
<execution>
22+
<goals>
23+
<goal>validate</goal>
24+
</goals>
25+
</execution>
26+
</executions>
27+
</plugin>
28+
</plugins>
29+
</build>
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package simple;
2+
3+
/**
4+
* Simple.
5+
*
6+
* @author Phillip Webb
7+
* @since 1.0.0
8+
*/
9+
public class Simple {
10+
11+
public static void main(String[] args) throws Exception {
12+
// Main method
13+
}
14+
15+
}

spring-javaformat-maven/spring-javaformat-maven-plugin/src/main/java/io/spring/format/maven/ApplyMojo.java

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.maven.plugin.MojoFailureException;
2525
import org.apache.maven.plugins.annotations.LifecyclePhase;
2626
import org.apache.maven.plugins.annotations.Mojo;
27+
import org.apache.maven.plugins.annotations.Parameter;
2728

2829
import io.spring.javaformat.formatter.FileEdit;
2930
import io.spring.javaformat.formatter.FileFormatterException;
@@ -36,9 +37,19 @@
3637
@Mojo(name = "apply", defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true)
3738
public class ApplyMojo extends FormatMojo {
3839

40+
/**
41+
* Skip the execution.
42+
*/
43+
@Parameter(property = "spring-javaformat.format.skip", defaultValue = "false")
44+
private boolean skip;
45+
3946
@Override
4047
protected void execute(List<File> files, Charset encoding, String lineSeparator)
4148
throws MojoExecutionException, MojoFailureException {
49+
if (this.skip || skipGlobally()) {
50+
getLog().debug("skipping format apply as per configuration.");
51+
return;
52+
}
4253
try {
4354
getFormatter().formatFiles(files, encoding, lineSeparator).filter(FileEdit::hasEdits).forEach(this::save);
4455
}

spring-javaformat-maven/spring-javaformat-maven-plugin/src/main/java/io/spring/format/maven/FormatMojo.java

+20
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import java.util.LinkedHashMap;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.Properties;
2930
import java.util.stream.Collectors;
3031
import java.util.stream.Stream;
3132

33+
import org.apache.maven.execution.MavenSession;
3234
import org.apache.maven.plugin.AbstractMojo;
3335
import org.apache.maven.plugin.MojoExecutionException;
3436
import org.apache.maven.plugin.MojoFailureException;
@@ -68,6 +70,12 @@ public abstract class FormatMojo extends AbstractMojo {
6870
@Parameter(defaultValue = "${project}", readonly = true, required = true)
6971
protected MavenProject project;
7072

73+
/**
74+
* The Maven Session Object.
75+
*/
76+
@Parameter(defaultValue = "${session}", readonly = true, required = true)
77+
protected MavenSession session;
78+
7179
/**
7280
* Specifies the location of the source directories to use.
7381
*/
@@ -195,4 +203,16 @@ protected final FileFormatter getFormatter() {
195203
return new FileFormatter(javaFormatConfig);
196204
}
197205

206+
protected boolean skipGlobally() {
207+
boolean result = false;
208+
result = result || skipGlobally(this.session.getUserProperties());
209+
result = result || skipGlobally(this.session.getSystemProperties());
210+
result = result || skipGlobally(this.project.getProperties());
211+
return result;
212+
}
213+
214+
private boolean skipGlobally(Properties properties) {
215+
return Boolean.valueOf(properties.getProperty("spring-javaformat.skip"));
216+
}
217+
198218
}

spring-javaformat-maven/spring-javaformat-maven-plugin/src/main/java/io/spring/format/maven/ValidateMojo.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public class ValidateMojo extends FormatMojo {
4040
/**
4141
* Skip the execution.
4242
*/
43-
@Parameter(property = "spring-javaformat.skip", defaultValue = "false")
43+
@Parameter(property = "spring-javaformat.validate.skip", defaultValue = "false")
4444
private boolean skip;
4545

4646
@Override
4747
protected void execute(List<File> files, Charset encoding, String lineSeparator)
4848
throws MojoExecutionException, MojoFailureException {
49-
if (this.skip) {
50-
getLog().debug("skipping validation as per configuration.");
49+
if (this.skip || skipGlobally()) {
50+
getLog().debug("skipping format validate as per configuration.");
5151
return;
5252
}
5353
List<File> problems = getFormatter().formatFiles(files, encoding, lineSeparator)

spring-javaformat-maven/spring-javaformat-maven-plugin/src/test/java/io/spring/format/maven/VerifyApply.java

+6
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@ public void verify(File base, String lineSeparator, boolean spaces) throws IOExc
5656
.contains(indent + "public static void main");
5757
}
5858

59+
public void verifyNoApply(File base) throws IOException {
60+
String formated = new String(Files.readAllBytes(base.toPath().resolve(JAVA_FILE)), StandardCharsets.UTF_8);
61+
formated = formated.replace("\r\n", "\n").replace('\r', '\n');
62+
assertThat(formated).contains("Simple {");
63+
}
64+
5965
}

0 commit comments

Comments
 (0)