Skip to content

Commit

Permalink
[MPMD-390] Dynamically calculate xrefLocation/xrefTestLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Aug 19, 2024
1 parent c31b29a commit 2498240
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,26 @@ public abstract class AbstractPmdReport extends AbstractMavenReport {
protected String format = "xml";

/**
* Link the violation line numbers to the source xref. Links will be created automatically if the jxr plugin is
* Link the violation line numbers to the (Test) Source XRef. Links will be created automatically if the JXR plugin is
* being used.
*/
@Parameter(property = "linkXRef", defaultValue = "true")
private boolean linkXRef;

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

/**
* Location of the Test Xrefs to link to.
* Location where Test Source XRef is generated for this project.
* <br>
* <strong>Default</strong>: {@link #getReportOutputDirectory()} + {@code /xref-test}
*/
@Parameter(defaultValue = "${project.reporting.outputDirectory}/xref-test")
@Parameter
private File xrefTestLocation;

/**
Expand Down Expand Up @@ -279,18 +283,18 @@ protected MojoExecution getMojoExecution() {
return mojoExecution;
}

protected String constructXRefLocation(boolean test) {
protected String constructXrefLocation(boolean test) {
String location = null;
if (linkXRef) {
File xrefLoc = test ? xrefTestLocation : xrefLocation;
File xrefLocation = getXrefLocation(test);

String relativePath =
PathTool.getRelativePath(getReportOutputDirectory().getAbsolutePath(), xrefLoc.getAbsolutePath());
String relativePath = PathTool.getRelativePath(
getReportOutputDirectory().getAbsolutePath(), xrefLocation.getAbsolutePath());
if (relativePath == null || relativePath.isEmpty()) {
relativePath = ".";
}
relativePath = relativePath + "/" + xrefLoc.getName();
if (xrefLoc.exists()) {
relativePath = relativePath + "/" + xrefLocation.getName();
if (xrefLocation.exists()) {
// XRef was already generated by manual execution of a lifecycle binding
location = relativePath;
} else {
Expand All @@ -300,19 +304,24 @@ protected String constructXRefLocation(boolean test) {
reporting != null ? reporting.getPlugins() : Collections.<ReportPlugin>emptyList();
for (ReportPlugin plugin : reportPlugins) {
String artifactId = plugin.getArtifactId();
if ("maven-jxr-plugin".equals(artifactId) || "jxr-maven-plugin".equals(artifactId)) {
if ("maven-jxr-plugin".equals(artifactId)) {
location = relativePath;
}
}
}

if (location == null) {
getLog().warn("Unable to locate Source XRef to link to - DISABLED");
getLog().warn("Unable to locate" + (test ? " Test" : "") + " Source XRef to link to - DISABLED");
}
}
return location;
}

protected File getXrefLocation(boolean test) {
File location = test ? xrefTestLocation : xrefLocation;
return location != null ? location : new File(getReportOutputDirectory(), test ? "xref-test" : "xref");
}

/**
* Convenience method to get the list of files where the PMD tool will be executed
*
Expand Down Expand Up @@ -346,7 +355,7 @@ protected Map<File, PmdFileInfo> getFilesToProcess() throws IOException {
for (String root : compileSourceRoots) {
File sroot = new File(root);
if (sroot.exists()) {
String sourceXref = constructXRefLocation(false);
String sourceXref = constructXrefLocation(false);
directories.add(new PmdFileInfo(project, sroot, sourceXref));
}
}
Expand All @@ -359,7 +368,7 @@ protected Map<File, PmdFileInfo> getFilesToProcess() throws IOException {
for (String root : testSourceRoots) {
File sroot = new File(root);
if (sroot.exists()) {
String testXref = constructXRefLocation(true);
String testXref = constructXrefLocation(true);
directories.add(new PmdFileInfo(project, sroot, testXref));
}
}
Expand All @@ -370,7 +379,7 @@ protected Map<File, PmdFileInfo> getFilesToProcess() throws IOException {
for (String root : localCompileSourceRoots) {
File sroot = new File(root);
if (sroot.exists()) {
String sourceXref = constructXRefLocation(false);
String sourceXref = constructXrefLocation(false);
directories.add(new PmdFileInfo(localProject, sroot, sourceXref));
}
}
Expand All @@ -379,7 +388,7 @@ protected Map<File, PmdFileInfo> getFilesToProcess() throws IOException {
for (String root : localTestCompileSourceRoots) {
File sroot = new File(root);
if (sroot.exists()) {
String testXref = constructXRefLocation(true);
String testXref = constructXrefLocation(true);
directories.add(new PmdFileInfo(localProject, sroot, testXref));
}
}
Expand Down

0 comments on commit 2498240

Please sign in to comment.