diff --git a/src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java b/src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java index 85deb3a..a4fd3a7 100644 --- a/src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java +++ b/src/main/java/org/checkstyle/autofix/CheckstyleAutoFix.java @@ -42,6 +42,12 @@ public class CheckstyleAutoFix extends Recipe { example = "config/checkstyle.xml") private String configurationPath; + @Option(displayName = "Checkstyle properties file path", + description = "Path to the file containing the Checkstyle Properties.", + example = "config/checkstyle.properties", + required = false) + private String propertiesPath; + @Override public String getDisplayName() { return "Checkstyle autoFix"; @@ -60,6 +66,10 @@ public String getConfigurationPath() { return configurationPath; } + public String getPropertiesPath() { + return propertiesPath; + } + @Override public List getRecipeList() { final List violations = CheckstyleReportParser @@ -69,6 +79,6 @@ public List getRecipeList() { } private CheckConfiguration loadCheckstyleConfiguration() { - return ConfigurationLoader.loadConfiguration(getConfigurationPath()); + return ConfigurationLoader.loadConfiguration(getConfigurationPath(), getPropertiesPath()); } } diff --git a/src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java b/src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java index b0b4b97..154eca0 100644 --- a/src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java +++ b/src/main/java/org/checkstyle/autofix/parser/ConfigurationLoader.java @@ -17,6 +17,8 @@ package org.checkstyle.autofix.parser; +import java.io.FileInputStream; +import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -57,13 +59,26 @@ private static CheckConfiguration mapConfiguration(Configuration config) { return new CheckConfiguration(config.getName(), properties, List.of(simpleChildren)); } - public static CheckConfiguration loadConfiguration(String checkstyleConfigurationPath) { + public static CheckConfiguration loadConfiguration(String checkstyleConfigurationPath, + String propFile) { + Properties props = new Properties(); + if (propFile == null) { + props = System.getProperties(); + } + else { + try (FileInputStream input = new FileInputStream(propFile)) { + props.load(input); + } + catch (IOException exception) { + throw new IllegalStateException("Failed to read: " + propFile, exception); + } + } final Configuration checkstyleConfig; try { checkstyleConfig = com.puppycrawl.tools.checkstyle.ConfigurationLoader .loadConfiguration(checkstyleConfigurationPath, - new PropertiesExpander(new Properties())); + new PropertiesExpander(props)); } catch (CheckstyleException exception) { throw new IllegalStateException("Failed to load configuration:" diff --git a/src/main/resources/META-INF/rewrite/recipes.yml b/src/main/resources/META-INF/rewrite/recipes.yml index ae1b7de..eb794d6 100644 --- a/src/main/resources/META-INF/rewrite/recipes.yml +++ b/src/main/resources/META-INF/rewrite/recipes.yml @@ -16,3 +16,4 @@ recipeList: - org.checkstyle.autofix.CheckstyleAutoFix: violationReportPath: "target/checkstyle/checkstyle-report.xml" configurationPath: "https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-${checkstyle.version}/config/checkstyle-checks.xml" + propertiesPath: "config/checkstyle.properties"