Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
elharo committed Nov 24, 2024
2 parents e418ac4 + b0ce8f7 commit 9e0d485
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 106 deletions.
18 changes: 5 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ under the License.

<!-- plexus -->
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>2.2.0</version>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.inject</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
Expand Down Expand Up @@ -325,16 +325,8 @@ under the License.
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>2.2.0</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
</executions>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Expand Down
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 @@ -58,12 +57,9 @@
import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.FileResourceLoader;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.PathTool;

/**
* 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 +437,35 @@ 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;
protected final CheckstyleExecutor checkstyleExecutor;

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

protected ByteArrayOutputStream stringOutputStream;

public AbstractCheckstyleReport(
final ResourceManager locator, final CheckstyleExecutor checkstyleExecutor, final 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 Expand Up @@ -675,16 +677,6 @@ protected AuditListener getConsoleListener() throws MavenReportException {
return consoleListener;
}

private String determineRelativePath(File location) {
String relativePath =
PathTool.getRelativePath(getReportOutputDirectory().getAbsolutePath(), location.getAbsolutePath());
if (relativePath == null || relativePath.trim().isEmpty()) {
relativePath = ".";
}

return relativePath + "/" + location.getName();
}

protected List<File> getSourceDirectories() {
if (sourceDirectories == null) {
sourceDirectories = filterBuildTarget(project.getCompileSourceRoots());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,39 @@
*/
package org.apache.maven.plugins.checkstyle;

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

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
* HTML report on the violations that Checkstyle finds in a multi-module reactor
* build.
*
*
*/
@Mojo(
name = "checkstyle-aggregate",
aggregator = true,
requiresDependencyResolution = ResolutionScope.COMPILE,
threadSafe = true)
public class CheckstyleAggregateReport extends AbstractCheckstyleReport {

@Inject
public CheckstyleAggregateReport(
ResourceManager locator, @Named("default") 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,20 @@
*/
package org.apache.maven.plugins.checkstyle;

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

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 +44,19 @@
*/
@Mojo(name = "checkstyle", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
public class CheckstyleReport extends AbstractCheckstyleReport {

@Inject
public CheckstyleReport(
final ResourceManager locator,
final @Named("default") CheckstyleExecutor checkstyleExecutor,
final 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 +86,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 @@ -40,8 +40,6 @@

/**
* Generate a report based on CheckstyleResults.
*
*
*/
public class CheckstyleReportRenderer extends AbstractMavenReportRenderer {
private static final int NO_TEXT = 0;
Expand Down Expand Up @@ -120,6 +118,7 @@ private String getI18nString(String key) {
return i18n.getString("checkstyle-report", locale, "report.checkstyle." + key);
}

@Override
protected void renderBody() {
startSection(getTitle());

Expand Down Expand Up @@ -150,31 +149,31 @@ protected void renderBody() {
}

/**
* Get the value of the specified attribute from the Checkstyle configuration.
* Get the value of the specified property from the Checkstyle configuration.
* If parentConfigurations is non-null and non-empty, the parent
* configurations are searched if the attribute cannot be found in the
* current configuration. If the attribute is still not found, the
* configurations are searched if the property cannot be found in the
* current configuration. If the property is still not found, the
* specified default value will be returned.
*
* @param config The current Checkstyle configuration
* @param parentConfiguration The configuration of the parent of the current configuration
* @param attributeName The name of the attribute
* @param defaultValue The default value to use if the attribute cannot be found in any configuration
* @return The value of the specified attribute
* @param config the current Checkstyle configuration
* @param parentConfiguration the configuration of the parent of the current configuration
* @param propertyName the name of the property
* @param defaultValue the default value to use if the property cannot be found in any configuration
* @return the value of the specified property
*/
private String getConfigAttribute(
private String getConfigProperty(
Configuration config,
ChainedItem<Configuration> parentConfiguration,
String attributeName,
String propertyName,
String defaultValue) {
String ret;
try {
ret = config.getAttribute(attributeName);
ret = config.getProperty(propertyName);
} catch (CheckstyleException e) {
// Try to find the attribute in a parent, if there are any
// Try to find the property in a parent, if there are any
if (parentConfiguration != null) {
ret = getConfigAttribute(
parentConfiguration.value, parentConfiguration.parent, attributeName, defaultValue);
ret = getConfigProperty(
parentConfiguration.value, parentConfiguration.parent, propertyName, defaultValue);
} else {
ret = defaultValue;
}
Expand All @@ -185,7 +184,7 @@ private String getConfigAttribute(
/**
* Create the rules summary section of the report.
*
* @param results The results to summarize
* @param results the results to summarize
*/
private void renderRulesSummarySection() {
if (!enableRulesSummary) {
Expand Down Expand Up @@ -256,16 +255,16 @@ private void renderRuleRow(ConfReference ref, CheckstyleResults results, String
sink.text(ruleName);
}

List<String> attribnames = new ArrayList<>(Arrays.asList(checkerConfig.getAttributeNames()));
attribnames.remove("severity"); // special value (deserves unique column)
if (!attribnames.isEmpty()) {
List<String> propertyNames = new ArrayList<>(Arrays.asList(checkerConfig.getPropertyNames()));
propertyNames.remove("severity"); // special value (deserves unique column)
if (!propertyNames.isEmpty()) {
sink.list();
for (String name : attribnames) {
for (String name : propertyNames) {
sink.listItem();

sink.text(name);

String value = getConfigAttribute(checkerConfig, null, name, "");
String value = getConfigProperty(checkerConfig, null, name, "");
// special case, Header.header and RegexpHeader.header
if ("header".equals(name) && ("Header".equals(ruleName) || "RegexpHeader".equals(ruleName))) {
String[] lines = StringUtils.split(value, "\\n");
Expand Down Expand Up @@ -316,7 +315,7 @@ private void renderRuleRow(ConfReference ref, CheckstyleResults results, String
sink.tableCell();
// Grab the severity from the rule configuration, this time use error as default value
// Also pass along all parent configurations, so that we can try to find the severity there
String severity = getConfigAttribute(checkerConfig, parentConfiguration, "severity", "error");
String severity = getConfigProperty(checkerConfig, parentConfiguration, "severity", "error");
iconSeverity(severity, TEXT_SIMPLE);
sink.tableCell_();

Expand Down Expand Up @@ -626,11 +625,11 @@ private void sortConfiguration(
// special sub-case: TreeWalker is the parent of multiple rules, not an effective rule
sortConfiguration(result, childConfig, new ChainedItem<>(config, parent), results);
} else {
String fixedmessage = getConfigAttribute(childConfig, null, "message", null);
String fixedmessage = getConfigProperty(childConfig, null, "message", null);
// Grab the severity from the rule configuration. Do not set default value here as
// it breaks our rule aggregate section entirely. The counts are off but this is
// not appropriate fix location per MCHECKSTYLE-365.
String configSeverity = getConfigAttribute(childConfig, null, "severity", null);
String configSeverity = getConfigProperty(childConfig, null, "severity", null);

// count rule violations
long violations = 0;
Expand Down Expand Up @@ -674,6 +673,7 @@ private static class ConfReference implements Comparable<ConfReference> {
this.count = count;
}

@Override
public int compareTo(ConfReference o) {
int compare = category.compareTo(o.category);
if (compare == 0) {
Expand Down
Loading

0 comments on commit 9e0d485

Please sign in to comment.