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,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 ());
0 commit comments