plugin: extract CheckConfiguration and variour other refactorings

This commit is contained in:
Paul Campbell 2017-05-26 17:08:44 +01:00
parent d91f392bf1
commit 92e9a289fa
11 changed files with 171 additions and 104 deletions

View file

@ -27,6 +27,8 @@ package net.kemitix.checkstyle.ruleset.plugin;
import lombok.Setter;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Build;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
@ -42,6 +44,10 @@ import org.apache.maven.project.MavenProject;
*/
abstract class AbstractCheckMojo extends AbstractMojo {
private static final String KEMITIX_GROUPID = "net.kemitix";
private static final String KEMITIX_ARTIFACTID = "kemitix-checkstyle-ruleset";
@Setter
@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject mavenProject;
@ -50,22 +56,41 @@ abstract class AbstractCheckMojo extends AbstractMojo {
@Parameter(defaultValue = "${session}", readonly = true)
private MavenSession mavenSession;
@Setter
@Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository artifactRepository = null;
@Setter
@Component
private BuildPluginManager pluginManager = null;
private final CheckstyleExecutor checkstyleExecutor;
AbstractCheckMojo(final PluginExecutor pluginExecutor) {
checkstyleExecutor = new DefaultCheckstyleExecutor(getLog(), getLevel(), pluginExecutor);
AbstractCheckMojo(final CheckstyleExecutor checkstyleExecutor) {
this.checkstyleExecutor = checkstyleExecutor;
checkstyleExecutor.setLog(getLog());
}
abstract String getLevel();
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
checkstyleExecutor.performCheck(mavenProject, mavenSession, artifactRepository, pluginManager);
CheckConfiguration config = getCheckConfiguration();
checkstyleExecutor.performCheck(config);
}
private CheckConfiguration getCheckConfiguration() {
final Plugin rulesetPlugin = getRulesetPlugin();
final Build build = mavenProject.getBuild();
return CheckConfiguration.builder()
.mavenProject(mavenProject)
.mavenSession(mavenSession)
.artifactRepository(artifactRepository)
.pluginManager(pluginManager)
.rulesetVersion(rulesetPlugin.getVersion())
.sourceDirectory(build.getSourceDirectory())
.build();
}
private Plugin getRulesetPlugin() {
return mavenProject.getPlugin(KEMITIX_GROUPID + ":" + KEMITIX_ARTIFACTID + "-maven-plugin");
}
}

View file

@ -0,0 +1,51 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Paul Campbell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.checkstyle.ruleset.plugin;
import lombok.Builder;
import lombok.Getter;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.project.MavenProject;
/**
* Configuration for performing a Checkstyle Check.
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
@Getter
@Builder
class CheckConfiguration {
private MavenProject mavenProject;
private MavenSession mavenSession;
private ArtifactRepository artifactRepository;
private BuildPluginManager pluginManager;
private String rulesetVersion;
private String sourceDirectory;
}

View file

@ -21,12 +21,9 @@
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.plugin.logging.Log;
/**
* Executes the Checkstyle plugin.
@ -38,16 +35,12 @@ interface CheckstyleExecutor {
/**
* Execute Checkstyle Check.
*
* @param mavenProject The Maven Project
* @param mavenSession The Maven Session
* @param artifactRepository The Artifact Repository
* @param pluginManager The Maven Plugin Manager
* @param configuration The Configuration
*
* @throws MojoExecutionException on execution error
* @throws MojoFailureException on execution failure
*/
void performCheck(
final MavenProject mavenProject, final MavenSession mavenSession,
final ArtifactRepository artifactRepository, final BuildPluginManager pluginManager
) throws MojoExecutionException, MojoFailureException;
void performCheck(CheckConfiguration configuration) throws MojoExecutionException, MojoFailureException;
void setLog(Log log);
}

View file

@ -24,6 +24,7 @@ SOFTWARE.
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
@ -41,11 +42,6 @@ public final class ComplexityCheckMojo extends AbstractCheckMojo {
* Create the Mojo.
*/
public ComplexityCheckMojo() {
super(new DefaultPluginExecutor());
}
@Override
String getLevel() {
return LEVEL;
super(new DefaultCheckstyleExecutor(LEVEL, new DefaultPluginExecutor(), new MavenXpp3Reader()));
}
}

View file

@ -23,22 +23,26 @@ package net.kemitix.checkstyle.ruleset.plugin;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.val;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;
/**
@ -69,80 +73,105 @@ class DefaultCheckstyleExecutor implements CheckstyleExecutor {
private static final String SOURCE_DIR = "sourceDirectory";
@Getter
private final Log log;
private static final ArtifactHandler POM_ARTIFACT_HANDLER = new DefaultArtifactHandler("pom");
private static final String PLUGIN_ARTIFACT_ID = KEMITIX_ARTIFACTID + "-parent";
private final String level;
private final PluginExecutor pluginExecutor;
private final MavenXpp3Reader mavenXpp3Reader;
@Setter
@Getter
private Log log;
private String rulesetVersion;
@Override
public final void performCheck(
final MavenProject mavenProject, final MavenSession mavenSession,
final ArtifactRepository artifactRepository, final BuildPluginManager pluginManager
) throws MojoExecutionException, MojoFailureException {
rulesetVersion = mavenProject.getPlugin(KEMITIX_GROUPID + ":" + KEMITIX_ARTIFACTID + "-maven-plugin")
.getVersion();
debug("rulesetVersion: %s", rulesetVersion);
val properties = getProperties(artifactRepository);
debug("properties: %s", properties);
// 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");
public final void performCheck(final CheckConfiguration config)
throws MojoExecutionException, MojoFailureException {
rulesetVersion = config.getRulesetVersion();
val properties = getProperties(config.getArtifactRepository());
// configure
val checkstylePlugin = getPlugin(pluginVersion, checkstyleVersion, sevntuVersion);
val configuration = pluginExecutor.configuration(
pluginExecutor.element(CONFIG_LOCATION, String.format("net/kemitix/checkstyle-%s.xml", level)),
pluginExecutor.element(SOURCE_DIR, mavenProject.getBuild()
.getSourceDirectory())
val checkstylePlugin = getPlugin(properties);
final String sourceDirectory = config.getSourceDirectory();
final String configFile = String.format("net/kemitix/checkstyle-%s.xml", level);
val configuration = pluginExecutor.configuration(pluginExecutor.element(CONFIG_LOCATION, configFile),
pluginExecutor.element(SOURCE_DIR, sourceDirectory)
);
val environment = pluginExecutor.executionEnvironment(mavenProject, mavenSession, pluginManager);
val environment = pluginExecutor.executionEnvironment(config);
// run
info("Running Checkstyle %s (sevntu: %s) with ruleset %s (%s)", checkstyleVersion, sevntuVersion, level,
rulesetVersion
);
pluginExecutor.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);
debug("%s: %s", key, property);
return property;
}
private Plugin getPlugin(final String pluginVersion, final String checkstyleVersion, final String sevntuVersion) {
private Plugin getPlugin(final Properties properties) {
// 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");
info("Checkstyle %s (plugin: %s, sevntu: %s) with ruleset %s (%s)", checkstyleVersion, pluginVersion,
sevntuVersion, level, rulesetVersion
);
// create checkstyle dependencies
val checkstyle = pluginExecutor.dependency(CHECKSTYLE_GROUPID, CHECKSTYLE_ARTIFACTID, checkstyleVersion);
val sevntu = pluginExecutor.dependency(SEVNTU_GROUPID, SEVNTU_ARTIFACTID, sevntuVersion);
val ruleset = pluginExecutor.dependency(KEMITIX_GROUPID, KEMITIX_ARTIFACTID, rulesetVersion);
val checkstyle = getCheckstyleDependency(checkstyleVersion);
val sevntu = getSevntuDependency(sevntuVersion);
val ruleset = getRulesetDependency();
val dependencies = pluginExecutor.dependencies(checkstyle, sevntu, ruleset);
return pluginExecutor.plugin(APACHE_PLUGIN_GROUPID, APACHE_PLUGIN_ARTIFACTID, pluginVersion, dependencies);
}
private Dependency getRulesetDependency() {
return pluginExecutor.dependency(KEMITIX_GROUPID, KEMITIX_ARTIFACTID, rulesetVersion);
}
private Dependency getSevntuDependency(final String sevntuVersion) {
return pluginExecutor.dependency(SEVNTU_GROUPID, SEVNTU_ARTIFACTID, sevntuVersion);
}
private Dependency getCheckstyleDependency(final String checkstyleVersion) {
return pluginExecutor.dependency(CHECKSTYLE_GROUPID, CHECKSTYLE_ARTIFACTID, checkstyleVersion);
}
private Properties getProperties(final ArtifactRepository artifactRepository) throws MojoFailureException {
// load properties from the plugin pom.xml
val pluginArtifactId = KEMITIX_ARTIFACTID + "-parent";
val pluginArtifact = new DefaultArtifact(KEMITIX_GROUPID, pluginArtifactId, rulesetVersion, null, "", null,
new DefaultArtifactHandler("pom")
);
final Artifact pluginArtifact = getPluginArtifact(artifactRepository);
try {
val pomReader = new FileReader(artifactRepository.find(pluginArtifact)
.getFile());
return new MavenXpp3Reader().read(pomReader)
.getProperties();
final File pomFile = pluginArtifact.getFile();
return parsePomFile(pomFile).getProperties();
} catch (XmlPullParserException | IOException e) {
throw new MojoFailureException("Can't load properties from plugin's pom.xml", e);
}
}
private Model parsePomFile(final File pomFile) throws IOException, XmlPullParserException {
final Reader pomReader = new FileReader(pomFile);
return mavenXpp3Reader.read(pomReader);
}
private Artifact getPluginArtifact(final ArtifactRepository artifactRepository) {
final Artifact templateArtifact = getRulesetArtifactTemplate();
return artifactRepository.find(templateArtifact);
}
private DefaultArtifact getRulesetArtifactTemplate() {
return new DefaultArtifact(KEMITIX_GROUPID, PLUGIN_ARTIFACT_ID, rulesetVersion, null, "", null,
POM_ARTIFACT_HANDLER
);
}
private void info(final String format, final Object... args) {
getLog().info(String.format(format, args));
}

View file

@ -21,12 +21,9 @@
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.twdata.maven.mojoexecutor.MojoExecutor;
@ -50,10 +47,9 @@ public final class DefaultPluginExecutor implements PluginExecutor {
}
@Override
public MojoExecutor.ExecutionEnvironment executionEnvironment(
final MavenProject mavenProject, final MavenSession mavenSession, final BuildPluginManager pluginManager
) {
return MojoExecutor.executionEnvironment(mavenProject, mavenSession, pluginManager);
public MojoExecutor.ExecutionEnvironment executionEnvironment(final CheckConfiguration config) {
return MojoExecutor.executionEnvironment(
config.getMavenProject(), config.getMavenSession(), config.getPluginManager());
}
@Override

View file

@ -24,6 +24,7 @@ SOFTWARE.
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
@ -41,11 +42,6 @@ public final class JavadocCheckMojo extends AbstractCheckMojo {
* Create the Mojo.
*/
public JavadocCheckMojo() {
super(new DefaultPluginExecutor());
}
@Override
String getLevel() {
return LEVEL;
super(new DefaultCheckstyleExecutor(LEVEL, new DefaultPluginExecutor(), new MavenXpp3Reader()));
}
}

View file

@ -24,6 +24,7 @@ SOFTWARE.
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
@ -41,11 +42,6 @@ public final class LayoutCheckMojo extends AbstractCheckMojo {
* Create the Mojo.
*/
public LayoutCheckMojo() {
super(new DefaultPluginExecutor());
}
@Override
String getLevel() {
return LEVEL;
super(new DefaultCheckstyleExecutor(LEVEL, new DefaultPluginExecutor(), new MavenXpp3Reader()));
}
}

View file

@ -24,6 +24,7 @@ SOFTWARE.
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
@ -41,11 +42,6 @@ public final class NamingCheckMojo extends AbstractCheckMojo {
* Create the Mojo.
*/
public NamingCheckMojo() {
super(new DefaultPluginExecutor());
}
@Override
String getLevel() {
return LEVEL;
super(new DefaultCheckstyleExecutor(LEVEL, new DefaultPluginExecutor(), new MavenXpp3Reader()));
}
}

View file

@ -21,12 +21,9 @@
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.twdata.maven.mojoexecutor.MojoExecutor;
@ -61,15 +58,11 @@ public interface PluginExecutor {
/**
* Constructs the {@link MojoExecutor.ExecutionEnvironment} instance fluently.
*
* @param mavenProject The current Maven project
* @param mavenSession The current Maven session
* @param pluginManager The Build plugin manager
* @param configuration The Configuration
*
* @return The execution environment
*/
MojoExecutor.ExecutionEnvironment executionEnvironment(
MavenProject mavenProject, MavenSession mavenSession, BuildPluginManager pluginManager
);
MojoExecutor.ExecutionEnvironment executionEnvironment(CheckConfiguration configuration);
/**
* Entry point for executing a mojo.

View file

@ -24,6 +24,7 @@ SOFTWARE.
package net.kemitix.checkstyle.ruleset.plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
@ -41,11 +42,6 @@ public final class TweaksCheckMojo extends AbstractCheckMojo {
* Create the Mojo.
*/
public TweaksCheckMojo() {
super(new DefaultPluginExecutor());
}
@Override
String getLevel() {
return LEVEL;
super(new DefaultCheckstyleExecutor(LEVEL, new DefaultPluginExecutor(), new MavenXpp3Reader()));
}
}