Skip to content

Commit

Permalink
Dynamically calculate xrefLocation/xrefTestLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-o committed Nov 22, 2023
1 parent 0c8c756 commit 2d90568
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java
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 @@ -268,18 +272,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());
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 @@ -296,12 +300,17 @@ protected String constructXRefLocation(boolean test) {
}

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 @@ -335,7 +344,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 @@ -348,7 +357,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 @@ -359,7 +368,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 @@ -368,7 +377,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 2d90568

Please sign in to comment.