Skip to content

Commit 9043f8a

Browse files
committed
[MCHECKSTYLE-446] Dynamically calculate xrefLocation/xrefTestLocation
1 parent 0e7bd00 commit 9043f8a

File tree

2 files changed

+64
-71
lines changed

2 files changed

+64
-71
lines changed

src/main/java/org/apache/maven/plugins/checkstyle/AbstractCheckstyleReport.java

+55-48
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.OutputStream;
2727
import java.nio.file.Path;
2828
import java.util.ArrayList;
29+
import java.util.Collections;
2930
import java.util.List;
3031
import java.util.Locale;
3132
import java.util.Map;
@@ -42,6 +43,7 @@
4243
import org.apache.maven.model.Plugin;
4344
import org.apache.maven.model.PluginManagement;
4445
import org.apache.maven.model.ReportPlugin;
46+
import org.apache.maven.model.Reporting;
4547
import org.apache.maven.model.Resource;
4648
import org.apache.maven.plugin.descriptor.PluginDescriptor;
4749
import org.apache.maven.plugins.annotations.Component;
@@ -346,24 +348,28 @@ public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
346348
private PluginDescriptor plugin;
347349

348350
/**
349-
* Link the violation line numbers to the source xref. Will link
350-
* automatically if Maven JXR plugin is being used.
351+
* Link the violation line numbers to the (Test) Source XRef. Links will be created automatically if the JXR plugin is
352+
* being used.
351353
*
352354
* @since 2.1
353355
*/
354356
@Parameter(property = "linkXRef", defaultValue = "true")
355357
private boolean linkXRef;
356358

357359
/**
358-
* Location of the Xrefs to link to.
360+
* Location where Source XRef is generated for this project.
361+
* <br>
362+
* <strong>Default</strong>: {@link #getReportOutputDirectory()} + {@code /xref}
359363
*/
360-
@Parameter(defaultValue = "${project.reporting.outputDirectory}/xref")
364+
@Parameter
361365
private File xrefLocation;
362366

363367
/**
364-
* Location of the XrefTests to link to.
368+
* Location where Test Source XRef is generated for this project.
369+
* <br>
370+
* <strong>Default</strong>: {@link #getReportOutputDirectory()} + {@code /xref-test}
365371
*/
366-
@Parameter(defaultValue = "${project.reporting.outputDirectory}/xref-test")
372+
@Parameter
367373
private File xrefTestLocation;
368374

369375
/**
@@ -483,6 +489,45 @@ protected List<MavenProject> getReactorProjects() {
483489
return reactorProjects;
484490
}
485491

492+
protected String constructXrefLocation(boolean test, boolean haveResults) {
493+
String location = null;
494+
if (linkXRef) {
495+
File xrefLocation = getXrefLocation(test);
496+
497+
String relativePath = PathTool.getRelativePath(
498+
getReportOutputDirectory().getAbsolutePath(), xrefLocation.getAbsolutePath());
499+
if (relativePath == null || relativePath.isEmpty()) {
500+
relativePath = ".";
501+
}
502+
relativePath = relativePath + "/" + xrefLocation.getName();
503+
if (xrefLocation.exists()) {
504+
// XRef was already generated by manual execution of a lifecycle binding
505+
location = relativePath;
506+
} else {
507+
// Not yet generated - check if the report is on its way
508+
Reporting reporting = project.getModel().getReporting();
509+
List<ReportPlugin> reportPlugins =
510+
reporting != null ? reporting.getPlugins() : Collections.<ReportPlugin>emptyList();
511+
for (ReportPlugin plugin : reportPlugins) {
512+
String artifactId = plugin.getArtifactId();
513+
if ("maven-jxr-plugin".equals(artifactId) || "jxr-maven-plugin".equals(artifactId)) {
514+
location = relativePath;
515+
}
516+
}
517+
}
518+
519+
if (location == null && haveResults) {
520+
getLog().warn("Unable to locate" + (test ? " Test" : "") + " Source XRef to link to - DISABLED");
521+
}
522+
}
523+
return location;
524+
}
525+
526+
protected File getXrefLocation(boolean test) {
527+
File location = test ? xrefTestLocation : xrefLocation;
528+
return location != null ? location : new File(getReportOutputDirectory(), test ? "xref-test" : "xref");
529+
}
530+
486531
/** {@inheritDoc} */
487532
public void executeReport(Locale locale) throws MavenReportException {
488533
checkDeprecatedParameterUsage(sourceDirectory, "sourceDirectory", "sourceDirectories");
@@ -528,30 +573,21 @@ public void executeReport(Locale locale) throws MavenReportException {
528573

529574
CheckstyleResults results = checkstyleExecutor.executeCheckstyle(request);
530575

576+
boolean haveResults = results.getFileCount() > 0;
531577
CheckstyleReportRenderer r = new CheckstyleReportRenderer(
532578
getSink(),
533579
i18n,
534580
locale,
535581
project,
536582
siteTool,
537583
effectiveConfigLocation,
584+
constructXrefLocation(false, haveResults),
585+
constructXrefLocation(true, haveResults),
586+
linkXRef ? getTestSourceDirectories() : Collections.emptyList(),
538587
enableRulesSummary,
539588
enableSeveritySummary,
540589
enableFilesSummary,
541590
results);
542-
if (linkXRef) {
543-
initializeXrefLocation(r);
544-
if (r.getXrefLocation() == null && results.getFileCount() > 0) {
545-
getLog().warn("Unable to locate Source XRef to link to - DISABLED");
546-
}
547-
548-
initializeXrefTestLocation(r);
549-
if (r.getXrefTestLocation() == null && results.getFileCount() > 0) {
550-
getLog().warn("Unable to locate Test Source XRef to link to - DISABLED");
551-
}
552-
553-
r.setTestSourceDirectories(getTestSourceDirectories());
554-
}
555591
if (treeWalkerNames != null) {
556592
r.setTreeWalkerNames(treeWalkerNames);
557593
}
@@ -682,24 +718,6 @@ protected DefaultLogger getConsoleListener() throws MavenReportException {
682718
return consoleListener;
683719
}
684720

685-
private void initializeXrefLocation(CheckstyleReportRenderer renderer) {
686-
String relativePath = determineRelativePath(xrefLocation);
687-
if (xrefLocation.exists() || checkMavenJxrPluginIsConfigured()) {
688-
// XRef was already generated by manual execution of a lifecycle binding
689-
// the report is on its way
690-
renderer.setXrefLocation(relativePath);
691-
}
692-
}
693-
694-
private void initializeXrefTestLocation(CheckstyleReportRenderer renderer) {
695-
String relativePath = determineRelativePath(xrefTestLocation);
696-
if (xrefTestLocation.exists() || checkMavenJxrPluginIsConfigured()) {
697-
// XRef was already generated by manual execution of a lifecycle binding
698-
// the report is on its way
699-
renderer.setXrefTestLocation(relativePath);
700-
}
701-
}
702-
703721
private String determineRelativePath(File location) {
704722
String relativePath =
705723
PathTool.getRelativePath(getReportOutputDirectory().getAbsolutePath(), location.getAbsolutePath());
@@ -710,17 +728,6 @@ private String determineRelativePath(File location) {
710728
return relativePath + "/" + location.getName();
711729
}
712730

713-
private boolean checkMavenJxrPluginIsConfigured() {
714-
for (ReportPlugin report : (Iterable<ReportPlugin>) getProject().getReportPlugins()) {
715-
String artifactId = report.getArtifactId();
716-
if ("maven-jxr-plugin".equals(artifactId) || "jxr-maven-plugin".equals(artifactId)) {
717-
return true;
718-
}
719-
}
720-
721-
return false;
722-
}
723-
724731
protected List<File> getSourceDirectories() {
725732
if (sourceDirectories == null) {
726733
sourceDirectories = filterBuildTarget(project.getCompileSourceRoots());

src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleReportRenderer.java

+9-23
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class CheckstyleReportRenderer extends AbstractMavenReportRenderer {
6969

7070
private String xrefTestLocation;
7171

72-
private List<File> testSourceDirectories = new ArrayList<>();
72+
private List<File> testSourceDirectories;
7373

7474
private List<String> treeWalkerNames = Collections.singletonList("TreeWalker");
7575

@@ -84,6 +84,9 @@ public CheckstyleReportRenderer(
8484
MavenProject project,
8585
SiteTool siteTool,
8686
String ruleset,
87+
String xrefLocation,
88+
String xrefTestLocation,
89+
List<File> testSourceDirectories,
8790
boolean enableRulesSummary,
8891
boolean enableSeveritySummary,
8992
boolean enableFilesSummary,
@@ -94,6 +97,9 @@ public CheckstyleReportRenderer(
9497
this.project = project;
9598
this.siteTool = siteTool;
9699
this.ruleset = ruleset;
100+
this.xrefLocation = xrefLocation;
101+
this.xrefTestLocation = xrefTestLocation;
102+
this.testSourceDirectories = testSourceDirectories;
97103
this.enableRulesSummary = enableRulesSummary;
98104
this.enableSeveritySummary = enableSeveritySummary;
99105
this.enableFilesSummary = enableFilesSummary;
@@ -542,9 +548,9 @@ private void renderFileEvents(List<AuditEvent> eventList, String filename) {
542548
private String getEffectiveXrefLocation(List<AuditEvent> eventList) {
543549
String absoluteFilename = eventList.get(0).getFileName();
544550
if (isTestSource(absoluteFilename)) {
545-
return getXrefTestLocation();
551+
return xrefTestLocation;
546552
} else {
547-
return getXrefLocation();
553+
return xrefLocation;
548554
}
549555
}
550556

@@ -558,26 +564,6 @@ private boolean isTestSource(final String absoluteFilename) {
558564
return false;
559565
}
560566

561-
public String getXrefLocation() {
562-
return xrefLocation;
563-
}
564-
565-
public void setXrefLocation(String xrefLocation) {
566-
this.xrefLocation = xrefLocation;
567-
}
568-
569-
public String getXrefTestLocation() {
570-
return xrefTestLocation;
571-
}
572-
573-
public void setXrefTestLocation(String xrefTestLocation) {
574-
this.xrefTestLocation = xrefTestLocation;
575-
}
576-
577-
public void setTestSourceDirectories(List<File> testSourceDirectories) {
578-
this.testSourceDirectories = testSourceDirectories;
579-
}
580-
581567
public void setTreeWalkerNames(List<String> treeWalkerNames) {
582568
this.treeWalkerNames = treeWalkerNames;
583569
}

0 commit comments

Comments
 (0)