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 lombok.Setter;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession; 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.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;
@ -42,6 +44,10 @@ import org.apache.maven.project.MavenProject;
*/ */
abstract class AbstractCheckMojo extends AbstractMojo { abstract class AbstractCheckMojo extends AbstractMojo {
private static final String KEMITIX_GROUPID = "net.kemitix";
private static final String KEMITIX_ARTIFACTID = "kemitix-checkstyle-ruleset";
@Setter @Setter
@Parameter(defaultValue = "${project}", readonly = true) @Parameter(defaultValue = "${project}", readonly = true)
private MavenProject mavenProject; private MavenProject mavenProject;
@ -50,22 +56,41 @@ abstract class AbstractCheckMojo extends AbstractMojo {
@Parameter(defaultValue = "${session}", readonly = true) @Parameter(defaultValue = "${session}", readonly = true)
private MavenSession mavenSession; private MavenSession mavenSession;
@Setter
@Parameter(defaultValue = "${localRepository}", readonly = true, required = true) @Parameter(defaultValue = "${localRepository}", readonly = true, required = true)
private ArtifactRepository artifactRepository = null; private ArtifactRepository artifactRepository = null;
@Setter
@Component @Component
private BuildPluginManager pluginManager = null; private BuildPluginManager pluginManager = null;
private final CheckstyleExecutor checkstyleExecutor; private final CheckstyleExecutor checkstyleExecutor;
AbstractCheckMojo(final PluginExecutor pluginExecutor) { AbstractCheckMojo(final CheckstyleExecutor checkstyleExecutor) {
checkstyleExecutor = new DefaultCheckstyleExecutor(getLog(), getLevel(), pluginExecutor); this.checkstyleExecutor = checkstyleExecutor;
checkstyleExecutor.setLog(getLog());
} }
abstract String getLevel();
@Override @Override
public void execute() throws MojoExecutionException, MojoFailureException { 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; 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.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject; import org.apache.maven.plugin.logging.Log;
/** /**
* Executes the Checkstyle plugin. * Executes the Checkstyle plugin.
@ -38,16 +35,12 @@ interface CheckstyleExecutor {
/** /**
* Execute Checkstyle Check. * Execute Checkstyle Check.
* *
* @param mavenProject The Maven Project * @param configuration The Configuration
* @param mavenSession The Maven Session
* @param artifactRepository The Artifact Repository
* @param pluginManager The Maven Plugin Manager
* *
* @throws MojoExecutionException on execution error * @throws MojoExecutionException on execution error
* @throws MojoFailureException on execution failure * @throws MojoFailureException on execution failure
*/ */
void performCheck( void performCheck(CheckConfiguration configuration) throws MojoExecutionException, MojoFailureException;
final MavenProject mavenProject, final MavenSession mavenSession,
final ArtifactRepository artifactRepository, final BuildPluginManager pluginManager void setLog(Log log);
) throws MojoExecutionException, MojoFailureException;
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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