2626import java .io .OutputStream ;
2727import java .nio .file .Path ;
2828import java .util .ArrayList ;
29+ import java .util .Collections ;
2930import java .util .List ;
3031import java .util .Locale ;
3132import java .util .Map ;
4142import org .apache .maven .model .Plugin ;
4243import org .apache .maven .model .PluginManagement ;
4344import org .apache .maven .model .ReportPlugin ;
45+ import org .apache .maven .model .Reporting ;
4446import org .apache .maven .model .Resource ;
4547import org .apache .maven .plugin .descriptor .PluginDescriptor ;
4648import org .apache .maven .plugins .annotations .Component ;
@@ -345,24 +347,28 @@ 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.
360+ * <br>
361+ * <strong>Default</strong>: {@link #getReportOutputDirectory()} + {@code /xref}
358362 */
359- @ Parameter ( defaultValue = "${project.reporting.outputDirectory}/xref" )
363+ @ Parameter
360364 private File xrefLocation ;
361365
362366 /**
363- * Location of the XrefTests to link to.
367+ * Location where Test Source XRef is generated for this project.
368+ * <br>
369+ * <strong>Default</strong>: {@link #getReportOutputDirectory()} + {@code /xref-test}
364370 */
365- @ Parameter ( defaultValue = "${project.reporting.outputDirectory}/xref-test" )
371+ @ Parameter
366372 private File xrefTestLocation ;
367373
368374 /**
@@ -482,6 +488,45 @@ protected List<MavenProject> getReactorProjects() {
482488 return reactorProjects ;
483489 }
484490
491+ protected String constructXrefLocation (boolean test , boolean haveResults ) {
492+ String location = null ;
493+ if (linkXRef ) {
494+ File xrefLocation = getXrefLocation (test );
495+
496+ String relativePath = PathTool .getRelativePath (
497+ getReportOutputDirectory ().getAbsolutePath (), xrefLocation .getAbsolutePath ());
498+ if (relativePath == null || relativePath .isEmpty ()) {
499+ relativePath = "." ;
500+ }
501+ relativePath = relativePath + "/" + xrefLocation .getName ();
502+ if (xrefLocation .exists ()) {
503+ // XRef was already generated by manual execution of a lifecycle binding
504+ location = relativePath ;
505+ } else {
506+ // Not yet generated - check if the report is on its way
507+ Reporting reporting = project .getModel ().getReporting ();
508+ List <ReportPlugin > reportPlugins =
509+ reporting != null ? reporting .getPlugins () : Collections .<ReportPlugin >emptyList ();
510+ for (ReportPlugin plugin : reportPlugins ) {
511+ String artifactId = plugin .getArtifactId ();
512+ if ("maven-jxr-plugin" .equals (artifactId ) || "jxr-maven-plugin" .equals (artifactId )) {
513+ location = relativePath ;
514+ }
515+ }
516+ }
517+
518+ if (location == null && haveResults ) {
519+ getLog ().warn ("Unable to locate" + (test ? " Test" : "" ) + " Source XRef to link to - DISABLED" );
520+ }
521+ }
522+ return location ;
523+ }
524+
525+ protected File getXrefLocation (boolean test ) {
526+ File location = test ? xrefTestLocation : xrefLocation ;
527+ return location != null ? location : new File (getReportOutputDirectory (), test ? "xref-test" : "xref" );
528+ }
529+
485530 /** {@inheritDoc} */
486531 public void executeReport (Locale locale ) throws MavenReportException {
487532 checkDeprecatedParameterUsage (sourceDirectory , "sourceDirectory" , "sourceDirectories" );
@@ -527,30 +572,21 @@ public void executeReport(Locale locale) throws MavenReportException {
527572
528573 CheckstyleResults results = checkstyleExecutor .executeCheckstyle (request );
529574
575+ boolean haveResults = results .getFileCount () > 0 ;
530576 CheckstyleReportRenderer r = new CheckstyleReportRenderer (
531577 getSink (),
532578 i18n ,
533579 locale ,
534580 project ,
535581 siteTool ,
536582 effectiveConfigLocation ,
583+ constructXrefLocation (false , haveResults ),
584+ constructXrefLocation (true , haveResults ),
585+ linkXRef ? getTestSourceDirectories () : Collections .emptyList (),
537586 enableRulesSummary ,
538587 enableSeveritySummary ,
539588 enableFilesSummary ,
540589 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- }
554590 if (treeWalkerNames != null ) {
555591 r .setTreeWalkerNames (treeWalkerNames );
556592 }
@@ -675,24 +711,6 @@ protected DefaultLogger getConsoleListener() throws MavenReportException {
675711 return consoleListener ;
676712 }
677713
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-
696714 private String determineRelativePath (File location ) {
697715 String relativePath =
698716 PathTool .getRelativePath (getReportOutputDirectory ().getAbsolutePath (), location .getAbsolutePath ());
@@ -703,17 +721,6 @@ private String determineRelativePath(File location) {
703721 return relativePath + "/" + location .getName ();
704722 }
705723
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-
717724 protected List <File > getSourceDirectories () {
718725 if (sourceDirectories == null ) {
719726 sourceDirectories = filterBuildTarget (project .getCompileSourceRoots ());
0 commit comments