Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Nov 21, 2024
1 parent a1cbc0f commit 68d9c45
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.apache.maven.model.PluginManagement;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutor;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutorException;
Expand All @@ -62,8 +61,6 @@

/**
* Base abstract class for Checkstyle reports.
*
*
*/
public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
protected static final String JAVA_FILES = "**\\/*.java";
Expand Down Expand Up @@ -441,31 +438,34 @@ public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
@Parameter(property = "checkstyle.excludeGeneratedSources", defaultValue = "false")
private boolean excludeGeneratedSources;

/**
*/
@Component
protected ResourceManager locator;

/**
* @since 2.5
*/
@Component(role = CheckstyleExecutor.class, hint = "default")
protected CheckstyleExecutor checkstyleExecutor;

/**
* Internationalization component
*/
@Component
private I18N i18n;

protected ByteArrayOutputStream stringOutputStream;

public AbstractCheckstyleReport(ResourceManager locator, CheckstyleExecutor checkstyleExecutor, I18N i18n) {
this.locator = locator;
this.checkstyleExecutor = checkstyleExecutor;
this.i18n = i18n;
}

/** {@inheritDoc} */
@Override
public String getName(Locale locale) {
return getI18nString(locale, "name");
}

/** {@inheritDoc} */
@Override
public String getDescription(Locale locale) {
return getI18nString(locale, "description");
}
Expand All @@ -479,6 +479,7 @@ protected String getI18nString(Locale locale, String key) {
return i18n.getString("checkstyle-report", locale, "report.checkstyle." + key);
}

@Override
protected MavenProject getProject() {
return project;
}
Expand All @@ -488,6 +489,7 @@ protected List<MavenProject> getReactorProjects() {
}

/** {@inheritDoc} */
@Override
public void executeReport(Locale locale) throws MavenReportException {
checkDeprecatedParameterUsage(sourceDirectory, "sourceDirectory", "sourceDirectories");
checkDeprecatedParameterUsage(testSourceDirectory, "testSourceDirectory", "testSourceDirectories");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
*/
package org.apache.maven.plugins.checkstyle;

import javax.inject.Inject;

import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutor;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutorRequest;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.resource.ResourceManager;

/**
* A reporting task that performs Checkstyle analysis and generates an aggregate
Expand All @@ -36,9 +41,16 @@
requiresDependencyResolution = ResolutionScope.COMPILE,
threadSafe = true)
public class CheckstyleAggregateReport extends AbstractCheckstyleReport {

@Inject
public CheckstyleAggregateReport(ResourceManager locator, CheckstyleExecutor checkstyleExecutor, I18N i18n) {
super(locator, checkstyleExecutor, i18n);
}

/**
* {@inheritDoc}
*/
@Override
protected CheckstyleExecutorRequest createRequest() throws MavenReportException {
CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
request.setAggregate(true)
Expand Down Expand Up @@ -70,11 +82,13 @@ protected CheckstyleExecutorRequest createRequest() throws MavenReportException
}

/** {@inheritDoc} */
@Override
public String getOutputName() {
return "checkstyle-aggregate";
}

/** {@inheritDoc} */
@Override
public boolean canGenerateReport() {
// TODO: would be good to scan the files here
return !skip && project.isExecutionRoot() && reactorProjects.size() > 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
*/
package org.apache.maven.plugins.checkstyle;

import javax.inject.Inject;

import java.io.File;
import java.util.List;

import org.apache.maven.model.Resource;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutor;
import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutorRequest;
import org.apache.maven.reporting.MavenReportException;
import org.codehaus.plexus.i18n.I18N;
import org.codehaus.plexus.resource.ResourceManager;

/**
* A reporting task that performs Checkstyle analysis and generates an HTML
Expand All @@ -38,9 +43,16 @@
*/
@Mojo(name = "checkstyle", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
public class CheckstyleReport extends AbstractCheckstyleReport {

@Inject
public CheckstyleReport(ResourceManager locator, CheckstyleExecutor checkstyleExecutor, I18N i18n) {
super(locator, checkstyleExecutor, i18n);
}

/**
* {@inheritDoc}
*/
@Override
protected CheckstyleExecutorRequest createRequest() throws MavenReportException {
CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
request.setConsoleListener(getConsoleListener())
Expand Down Expand Up @@ -70,11 +82,13 @@ protected CheckstyleExecutorRequest createRequest() throws MavenReportException
}

/** {@inheritDoc} */
@Override
public String getOutputName() {
return "checkstyle";
}

/** {@inheritDoc} */
@Override
public boolean canGenerateReport() {
if (skip) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.maven.plugins.checkstyle;

import javax.inject.Inject;
import javax.inject.Named;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -47,7 +50,6 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand Down Expand Up @@ -294,12 +296,6 @@ public class CheckstyleViolationCheckMojo extends AbstractMojo {
@Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
private String inputEncoding;

/**
* @since 2.5
*/
@Component(role = CheckstyleExecutor.class, hint = "default")
protected CheckstyleExecutor checkstyleExecutor;

/**
* Output errors to console.
*/
Expand Down Expand Up @@ -489,7 +485,18 @@ public class CheckstyleViolationCheckMojo extends AbstractMojo {

private File outputXmlFile;

/**
* @since 2.5
*/
protected CheckstyleExecutor checkstyleExecutor;

@Inject
public CheckstyleViolationCheckMojo(final @Named("default") CheckstyleExecutor checkstyleExecutor) {
this.checkstyleExecutor = checkstyleExecutor;
}

/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
checkDeprecatedParameterUsage(sourceDirectory, "sourceDirectory", "sourceDirectories");
checkDeprecatedParameterUsage(testSourceDirectory, "testSourceDirectory", "testSourceDirectories");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.apache.maven.plugins.checkstyle.exec;

import javax.inject.Inject;
import javax.inject.Named;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -49,8 +52,6 @@
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.FileResourceCreationException;
Expand All @@ -61,16 +62,21 @@
/**
* @author Olivier Lamy
* @since 2.5
*
*/
@Component(role = CheckstyleExecutor.class, hint = "default", instantiationStrategy = "per-lookup")
@Named
public class DefaultCheckstyleExecutor extends AbstractLogEnabled implements CheckstyleExecutor {
@Requirement(hint = "default")
private ResourceManager locator;

@Requirement(hint = "license")
private ResourceManager licenseLocator;

@Inject
public DefaultCheckstyleExecutor(
final @Named("default") ResourceManager locator, final @Named("license") ResourceManager licenseLocator) {
this.locator = locator;
this.licenseLocator = licenseLocator;
}

@Override
public CheckstyleResults executeCheckstyle(CheckstyleExecutorRequest request)
throws CheckstyleExecutorException, CheckstyleException {
if (getLogger().isDebugEnabled()) {
Expand Down Expand Up @@ -255,6 +261,7 @@ protected void addSourceDirectory(
}
}

@Override
public Configuration getConfiguration(CheckstyleExecutorRequest request) throws CheckstyleExecutorException {
try {
// Checkstyle will always use the context classloader in order
Expand Down

0 comments on commit 68d9c45

Please sign in to comment.