diff --git a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java index bf94a39e..cfd8222a 100644 --- a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java +++ b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.io.StringWriter; import java.nio.file.Path; import java.nio.file.Paths; @@ -95,6 +96,17 @@ public abstract class ConfigurableRewriteMojo extends AbstractMojo { @Parameter(property = "rewrite.checkstyleDetectionEnabled", alias = "checkstyleDetectionEnabled", defaultValue = "true") protected boolean checkstyleDetectionEnabled; + @Nullable + @Parameter(property = "rewrite.checkstyleProperties", alias = "checkstyleProperties") + private LinkedHashSet checkstyleProperties; + /** + * @deprecated Use {@code rewrite.checkstyleProperties} instead. + */ + @Nullable + @Parameter(property = "checkstyleProperties") + @Deprecated + private LinkedHashSet deprecatedCheckstyleProperties; + @Nullable @Parameter(property = "rewrite.exclusions") private LinkedHashSet exclusions; @@ -103,6 +115,10 @@ protected Set getExclusions() { return getCleanedSet(exclusions); } + protected Set getCheckstyleProperties() { + return getMergedAndCleaned(checkstyleProperties, deprecatedCheckstyleProperties); + } + @Nullable @Parameter(property = "rewrite.plainTextMasks") private LinkedHashSet plainTextMasks; @@ -235,7 +251,16 @@ protected List loadStyles(MavenProject project, Environment env) { try { Plugin checkstylePlugin = project.getPlugin("org.apache.maven.plugins:maven-checkstyle-plugin"); if (checkstyleConfigFile != null && !checkstyleConfigFile.isEmpty()) { - styles.add(loadCheckstyleConfig(Paths.get(checkstyleConfigFile), emptyMap())); + // Convert the checkstyle config file contents to a String + String checkstyleConfig = new String(Files.readAllBytes(Paths.get(checkstyleConfigFile))); + Set checkstyleProperties = getCheckstyleProperties(); + if (!checkstyleProperties.isEmpty()) { + checkstyleConfig = checkstyleProperties.stream() + .map(s -> "-P" + s) + .collect(Collectors.joining("\n", "", "\n")) + checkstyleConfig; + } + styles.add(loadCheckstyleConfig(checkstyleConfig, emptyMap())); +// styles.add(loadCheckstyleConfig(Paths.get(checkstyleConfigFile), emptyMap())); } else if (checkstyleDetectionEnabled && checkstylePlugin != null) { Object checkstyleConfRaw = checkstylePlugin.getConfiguration(); if (checkstyleConfRaw instanceof Xpp3Dom) { diff --git a/src/test/java/org/openrewrite/maven/CheckstylePropertiesIT.java b/src/test/java/org/openrewrite/maven/CheckstylePropertiesIT.java new file mode 100644 index 00000000..261b2c9b --- /dev/null +++ b/src/test/java/org/openrewrite/maven/CheckstylePropertiesIT.java @@ -0,0 +1,33 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.maven; + +import com.soebes.itf.jupiter.extension.*; +import com.soebes.itf.jupiter.maven.MavenExecutionResult; + +import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat; + +@MavenJupiterExtension +@MavenOption(MavenCLIOptions.NO_TRANSFER_PROGRESS) +@MavenOption(MavenCLIExtra.MUTE_PLUGIN_VALIDATION_WARNING) +@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:run") +class CheckstylePropertiesIT { + @MavenTest + void checkstyle_properties(MavenExecutionResult result) { + assertThat(result) + .isSuccessful(); + } +} diff --git a/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/.checkstyle/checkstyle-suppressions.xml b/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/.checkstyle/checkstyle-suppressions.xml new file mode 100644 index 00000000..a2573be0 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/.checkstyle/checkstyle-suppressions.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/.checkstyle/checkstyle.xml b/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/.checkstyle/checkstyle.xml new file mode 100644 index 00000000..e349fcd9 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/.checkstyle/checkstyle.xml @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/.checkstyle/import-control.xml b/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/.checkstyle/import-control.xml new file mode 100644 index 00000000..8ea83633 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/.checkstyle/import-control.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/pom.xml b/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/pom.xml new file mode 100644 index 00000000..50593387 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + org.openrewrite.maven + checkstyle_properties + 1.0 + jar + CheckstylePropertiesTest#checkstyle_properties + + + 1.8 + 1.8 + UTF-8 + + + + + + @project.groupId@ + @project.artifactId@ + @project.version@ + + + org.openrewrite.staticanalysis.CodeCleanup + + + + + + + + org.openrewrite.recipe + rewrite-static-analysis + RELEASE + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.3.1 + + .checkstyle/checkstyle.xml + true + false + importControlFile=${project.basedir}/.checkstyle/import-control.xml + .checkstyle/checkstyle-suppressions.xml + + + + com.puppycrawl.tools + checkstyle + 10.12.7 + + + + + + \ No newline at end of file diff --git a/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/src/main/java/sample/ImportOrderSample.java b/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/src/main/java/sample/ImportOrderSample.java new file mode 100644 index 00000000..99b666d9 --- /dev/null +++ b/src/test/resources-its/org/openrewrite/maven/CheckstylePropertiesIT/checkstyle_properties/src/main/java/sample/ImportOrderSample.java @@ -0,0 +1,24 @@ +package sample; + +import java.net.URL; +import java.util.ArrayList; + +public class ImportOrderSample { + public void useUtilPackage() { + ArrayList list = new ArrayList<>(); + list.add("Hello"); + list.add("World"); + for (String s : list) { + System.out.println(s); + } + } + + public void useNetPackage() { + try { + URL url = new URL("https://github.com"); + url.openConnection(); + } catch (Exception e) { + System.out.println("Failed to connect to github.com! " + e.getMessage()); + } + } +}