Merge branch 'release/2.1.1'

* release/2.1.1:
  CHANGELOG
  pom.xml: version set to 2.1.1
  plugin:AbstractCheckMojo: load the version from the plugin's pom file
  plugin:pom.xml: remove properties-maven-plugin
  builder:CheckstyleWriterTest: use system's file separator
  builder:CheckstyleWriter: use system's line separator
  pom.xml: version set to 2.2.0-SNAPSHOT
This commit is contained in:
Paul Campbell 2017-02-09 09:26:35 +00:00
commit 68dc770f8d
9 changed files with 90 additions and 57 deletions

View file

@ -1,6 +1,12 @@
CHANGELOG CHANGELOG
========= =========
2.1.1
-----
* Load plugin dependency versions from the plugin's own pom
* Cross-platform build
2.1.0 2.1.0
----- -----

View file

@ -12,7 +12,7 @@
</parent> </parent>
<artifactId>kemitix-checkstyle-ruleset-builder</artifactId> <artifactId>kemitix-checkstyle-ruleset-builder</artifactId>
<version>2.1.0</version> <version>2.1.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Kemitix Checkstyle Ruleset Builder</name> <name>Kemitix Checkstyle Ruleset Builder</name>

View file

@ -50,7 +50,7 @@ import java.util.stream.Stream;
@RequiredArgsConstructor @RequiredArgsConstructor
class CheckstyleWriter implements CommandLineRunner { class CheckstyleWriter implements CommandLineRunner {
private static final String NEWLINE = "\n"; private static final String NEWLINE = System.getProperty("line.separator");
private final OutputProperties outputProperties; private final OutputProperties outputProperties;

View file

@ -30,6 +30,8 @@ public class CheckstyleWriterTest {
private static final String TEMPLATE = "C:%s\nTW:%s"; private static final String TEMPLATE = "C:%s\nTW:%s";
private static final String FILE_SEPARATOR = System.getProperty("file.separator");
private CheckstyleWriter checkstyleWriter; private CheckstyleWriter checkstyleWriter;
private OutputProperties outputProperties; private OutputProperties outputProperties;
@ -181,9 +183,11 @@ public class CheckstyleWriterTest {
@Test @Test
public void throwRteIfErrorWritingFile() throws Exception { public void throwRteIfErrorWritingFile() throws Exception {
//given //given
outputProperties.setDirectory(Paths.get("/../imaginary")); final String imaginary = String.join(FILE_SEPARATOR, "", "..", "imaginary");
outputProperties.setDirectory(Paths.get(imaginary));
exception.expect(RuntimeException.class); exception.expect(RuntimeException.class);
exception.expectMessage("java.nio.file.NoSuchFileException: /../imaginary/checkstyle-LAYOUT.xml"); exception.expectMessage(
"java.nio.file.NoSuchFileException: " + imaginary + FILE_SEPARATOR + "checkstyle-LAYOUT.xml");
//when //when
checkstyleWriter.run(); checkstyleWriter.run();
} }

View file

@ -8,7 +8,7 @@
<!-- don't use parent to avoid using grandparent's checkstyle configuration --> <!-- don't use parent to avoid using grandparent's checkstyle configuration -->
<groupId>net.kemitix</groupId> <groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-plugin-sample</artifactId> <artifactId>kemitix-checkstyle-ruleset-plugin-sample</artifactId>
<version>2.1.0</version> <version>2.1.1</version>
<name>Kemitix Checkstyle Ruleset Plugin Sample</name> <name>Kemitix Checkstyle Ruleset Plugin Sample</name>
<description>Sample usage of the Kemitix Checkstyle Ruleset Plugin</description> <description>Sample usage of the Kemitix Checkstyle Ruleset Plugin</description>

View file

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>net.kemitix</groupId> <groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-parent</artifactId> <artifactId>kemitix-checkstyle-ruleset-parent</artifactId>
<version>2.1.0</version> <version>2.1.1</version>
</parent> </parent>
<artifactId>kemitix-checkstyle-ruleset-maven-plugin</artifactId> <artifactId>kemitix-checkstyle-ruleset-maven-plugin</artifactId>
@ -101,34 +101,6 @@
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin><!-- maven-compiler-plugin --> </plugin><!-- maven-compiler-plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>${properties-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<property>
<name>maven-checkstyle-plugin.version</name>
<value>${maven-checkstyle-plugin.version}</value>
</property>
<property>
<name>checkstyle.version</name>
<value>${checkstyle.version}</value>
</property>
<property>
<name>sevntu.version</name>
<value>${sevntu.version}</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin><!-- properties-maven-plugin -->
</plugins> </plugins>
</build> </build>
<profiles> <profiles>

View file

@ -26,7 +26,12 @@ package net.kemitix.checkstyle.ruleset.plugin;
import lombok.Setter; import lombok.Setter;
import lombok.val; import lombok.val;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession; import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager; import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoExecutionException;
@ -34,14 +39,19 @@ import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.twdata.maven.mojoexecutor.MojoExecutor; import org.twdata.maven.mojoexecutor.MojoExecutor;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
/** /**
* Runs the Checkstyle Maven Plugin with the Kemitix Checkstyle Ruleset. * Runs the Checkstyle Maven Plugin with the Kemitix Checkstyle Ruleset.
* *
* @author Paul Campbell (pcampbell@kemitix.net) * @author Paul Campbell (pcampbell@kemitix.net)
*/ */
public abstract class AbstractCheckMojo extends AbstractMojo { abstract class AbstractCheckMojo extends AbstractMojo {
private static final String CHECKSTYLE_GROUPID = "com.puppycrawl.tools"; private static final String CHECKSTYLE_GROUPID = "com.puppycrawl.tools";
@ -73,8 +83,11 @@ public abstract class AbstractCheckMojo extends AbstractMojo {
@Parameter(defaultValue = "${session}", readonly = true) @Parameter(defaultValue = "${session}", readonly = true)
private MavenSession mavenSession; private MavenSession mavenSession;
@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository artifactRepository = null;
@Component @Component
private BuildPluginManager pluginManager; private BuildPluginManager pluginManager = null;
/** /**
* Execute Checkstyle Check. * Execute Checkstyle Check.
@ -84,28 +97,66 @@ public abstract class AbstractCheckMojo extends AbstractMojo {
* @throws MojoExecutionException on execution error * @throws MojoExecutionException on execution error
* @throws MojoFailureException on execution failure * @throws MojoFailureException on execution failure
*/ */
protected final void performCheck(final String level) throws MojoExecutionException, MojoFailureException { final void performCheck(final String level) throws MojoExecutionException, MojoFailureException {
// load versions from plugin's pom.xml val properties = getProperties();
val properties = System.getProperties(); debug("properties: %s", properties);
val mavenCheckstylePluginVersion = properties.getProperty("maven-checkstyle-plugin.version");
val checkstyleVersion = properties.getProperty("checkstyle.version");
val sevntuVersion = properties.getProperty("sevntu.version");
// get versions from pom.xml properties
val pluginVersion = getProperty(properties, "maven-checkstyle-plugin.version");
val checkstyleVersion = getProperty(properties, "checkstyle.version");
val sevntuVersion = getProperty(properties, "sevntu.version");
// configure
val checkstylePlugin = getPlugin(pluginVersion, checkstyleVersion, sevntuVersion);
val configuration = MojoExecutor.configuration(
MojoExecutor.element(CONFIG_LOCATION, String.format("net/kemitix/checkstyle-%s.xml", level)));
val environment = MojoExecutor.executionEnvironment(mavenProject, mavenSession, pluginManager);
// run
info("Running Checkstyle %s (sevntu: %s) with ruleset %s (%s)", checkstyleVersion, sevntuVersion, level,
rulesetVersion
);
MojoExecutor.executeMojo(checkstylePlugin, "check", configuration, environment);
info("Checkstyle complete");
}
private String getProperty(final Properties properties, final String key) {
val property = properties.getProperty(key);
debug(key + ": %s", property);
return property;
}
private Plugin getPlugin(final String pluginVersion, final String checkstyleVersion, final String sevntuVersion) {
// create checkstyle dependencies
val checkstyle = MojoExecutor.dependency(CHECKSTYLE_GROUPID, CHECKSTYLE_ARTIFACTID, checkstyleVersion); val checkstyle = MojoExecutor.dependency(CHECKSTYLE_GROUPID, CHECKSTYLE_ARTIFACTID, checkstyleVersion);
val sevntu = MojoExecutor.dependency(SEVNTU_GROUPID, SEVNTU_ARTIFACTID, sevntuVersion); val sevntu = MojoExecutor.dependency(SEVNTU_GROUPID, SEVNTU_ARTIFACTID, sevntuVersion);
val ruleset = MojoExecutor.dependency(KEMITIX_GROUPID, KEMITIX_ARTIFACTID, rulesetVersion); val ruleset = MojoExecutor.dependency(KEMITIX_GROUPID, KEMITIX_ARTIFACTID, rulesetVersion);
val checkstylePlugin = val dependencies = MojoExecutor.dependencies(checkstyle, sevntu, ruleset);
MojoExecutor.plugin(APACHE_PLUGIN_GROUPID, APACHE_PLUGIN_ARTIFACTID, mavenCheckstylePluginVersion,
MojoExecutor.dependencies(checkstyle, sevntu, ruleset)
);
val configLocation =
MojoExecutor.element(CONFIG_LOCATION, String.format("net/kemitix/checkstyle-%s.xml", level));
getLog().info(String.format("Running Checkstyle %s (sevntu: %s) with ruleset %s (%s)", checkstyleVersion, return MojoExecutor.plugin(APACHE_PLUGIN_GROUPID, APACHE_PLUGIN_ARTIFACTID, pluginVersion, dependencies);
sevntuVersion, level, rulesetVersion }
));
MojoExecutor.executeMojo(checkstylePlugin, "check", MojoExecutor.configuration(configLocation), private Properties getProperties() throws MojoFailureException {
MojoExecutor.executionEnvironment(mavenProject, mavenSession, pluginManager) // load properties from the plugin pom.xml
); val pluginArtifactId = KEMITIX_ARTIFACTID + "-maven-plugin";
val pluginArtifact = new DefaultArtifact(KEMITIX_GROUPID, pluginArtifactId, rulesetVersion, null, "", null,
new DefaultArtifactHandler("pom")
);
try {
val pomReader = new FileReader(artifactRepository.find(pluginArtifact)
.getFile());
return new MavenXpp3Reader().read(pomReader)
.getProperties();
} catch (XmlPullParserException | IOException e) {
throw new MojoFailureException("Can't load properties from plugin's pom.xml", e);
}
}
private void info(final String format, final Object... args) {
getLog().info(String.format(format, args));
}
private void debug(final String format, final Object... args) {
getLog().debug(String.format(format, args));
} }
} }

View file

@ -6,7 +6,7 @@
<groupId>net.kemitix</groupId> <groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-parent</artifactId> <artifactId>kemitix-checkstyle-ruleset-parent</artifactId>
<version>2.1.0</version> <version>2.1.1</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>Kemitix Checkstyle Ruleset (Parent)</name> <name>Kemitix Checkstyle Ruleset (Parent)</name>

View file

@ -7,11 +7,11 @@
<parent> <parent>
<groupId>net.kemitix</groupId> <groupId>net.kemitix</groupId>
<artifactId>kemitix-checkstyle-ruleset-parent</artifactId> <artifactId>kemitix-checkstyle-ruleset-parent</artifactId>
<version>2.1.0</version> <version>2.1.1</version>
</parent> </parent>
<artifactId>kemitix-checkstyle-ruleset</artifactId> <artifactId>kemitix-checkstyle-ruleset</artifactId>
<version>2.1.0</version> <version>2.1.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Kemitix Checkstyle Ruleset</name> <name>Kemitix Checkstyle Ruleset</name>