Skip to content

Commit 6769305

Browse files
committed
Dynamically calculate xrefLocation/xrefTestLocation
@kriegaex
1 parent 5e7636b commit 6769305

File tree

2 files changed

+62
-71
lines changed

2 files changed

+62
-71
lines changed

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

Lines changed: 53 additions & 48 deletions
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;
@@ -41,6 +42,7 @@
4142
import org.apache.maven.model.Plugin;
4243
import org.apache.maven.model.PluginManagement;
4344
import org.apache.maven.model.ReportPlugin;
45+
import org.apache.maven.model.Reporting;
4446
import org.apache.maven.model.Resource;
4547
import org.apache.maven.plugin.descriptor.PluginDescriptor;
4648
import org.apache.maven.plugins.annotations.Component;
@@ -345,24 +347,26 @@ public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
345347
private PluginDescriptor plugin;
346348

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

356358
/**
357-
* Location of the Xrefs to link to.
359+
* Location where Source XRef is generated for this project. The default value is calculated from
360+
* {@link #getReportOutputDirectory()} and concatenated with {@code xref}.
358361
*/
359-
@Parameter(defaultValue = "${project.reporting.outputDirectory}/xref")
362+
@Parameter
360363
private File xrefLocation;
361364

362365
/**
363-
* Location of the XrefTests to link to.
366+
* Location where Test Source XRef is generated for this project. The default value is calculated from
367+
* {@link #getReportOutputDirectory()} and concatenated with {@code xref-test}.
364368
*/
365-
@Parameter(defaultValue = "${project.reporting.outputDirectory}/xref-test")
369+
@Parameter
366370
private File xrefTestLocation;
367371

368372
/**
@@ -482,6 +486,45 @@ protected List<MavenProject> getReactorProjects() {
482486
return reactorProjects;
483487
}
484488

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

528571
CheckstyleResults results = checkstyleExecutor.executeCheckstyle(request);
529572

573+
boolean haveResults = results.getFileCount() > 0;
530574
CheckstyleReportRenderer r = new CheckstyleReportRenderer(
531575
getSink(),
532576
i18n,
533577
locale,
534578
project,
535579
siteTool,
536580
effectiveConfigLocation,
581+
constructXrefLocation(false, haveResults),
582+
constructXrefLocation(true, haveResults),
583+
linkXRef ? getTestSourceDirectories() : Collections.emptyList(),
537584
enableRulesSummary,
538585
enableSeveritySummary,
539586
enableFilesSummary,
540587
results);
541-
if (linkXRef) {
542-
initializeXrefLocation(r);
543-
if (r.getXrefLocation() == null && results.getFileCount() > 0) {
544-
getLog().warn("Unable to locate Source XRef to link to - DISABLED");
545-
}
546-
547-
initializeXrefTestLocation(r);
548-
if (r.getXrefTestLocation() == null && results.getFileCount() > 0) {
549-
getLog().warn("Unable to locate Test Source XRef to link to - DISABLED");
550-
}
551-
552-
r.setTestSourceDirectories(getTestSourceDirectories());
553-
}
554588
if (treeWalkerNames != null) {
555589
r.setTreeWalkerNames(treeWalkerNames);
556590
}
@@ -675,24 +709,6 @@ protected DefaultLogger getConsoleListener() throws MavenReportException {
675709
return consoleListener;
676710
}
677711

678-
private void initializeXrefLocation(CheckstyleReportRenderer renderer) {
679-
String relativePath = determineRelativePath(xrefLocation);
680-
if (xrefLocation.exists() || checkMavenJxrPluginIsConfigured()) {
681-
// XRef was already generated by manual execution of a lifecycle binding
682-
// the report is on its way
683-
renderer.setXrefLocation(relativePath);
684-
}
685-
}
686-
687-
private void initializeXrefTestLocation(CheckstyleReportRenderer renderer) {
688-
String relativePath = determineRelativePath(xrefTestLocation);
689-
if (xrefTestLocation.exists() || checkMavenJxrPluginIsConfigured()) {
690-
// XRef was already generated by manual execution of a lifecycle binding
691-
// the report is on its way
692-
renderer.setXrefTestLocation(relativePath);
693-
}
694-
}
695-
696712
private String determineRelativePath(File location) {
697713
String relativePath = PathTool.getRelativePath(getReportOutputDirectory().getAbsolutePath(),
698714
location.getAbsolutePath());
@@ -703,17 +719,6 @@ private String determineRelativePath(File location) {
703719
return relativePath + "/" + location.getName();
704720
}
705721

706-
private boolean checkMavenJxrPluginIsConfigured() {
707-
for (ReportPlugin report : (Iterable<ReportPlugin>) getProject().getReportPlugins()) {
708-
String artifactId = report.getArtifactId();
709-
if ("maven-jxr-plugin".equals(artifactId) || "jxr-maven-plugin".equals(artifactId)) {
710-
return true;
711-
}
712-
}
713-
714-
return false;
715-
}
716-
717722
protected List<File> getSourceDirectories() {
718723
if (sourceDirectories == null) {
719724
sourceDirectories = filterBuildTarget(project.getCompileSourceRoots());

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

Lines changed: 9 additions & 23 deletions
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;
@@ -541,9 +547,9 @@ private void renderFileEvents(List<AuditEvent> eventList, String filename) {
541547
private String getEffectiveXrefLocation(List<AuditEvent> eventList) {
542548
String absoluteFilename = eventList.get(0).getFileName();
543549
if (isTestSource(absoluteFilename)) {
544-
return getXrefTestLocation();
550+
return xrefTestLocation;
545551
} else {
546-
return getXrefLocation();
552+
return xrefLocation;
547553
}
548554
}
549555

@@ -557,26 +563,6 @@ private boolean isTestSource(final String absoluteFilename) {
557563
return false;
558564
}
559565

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

0 commit comments

Comments
 (0)