26
26
import java .io .OutputStream ;
27
27
import java .nio .file .Path ;
28
28
import java .util .ArrayList ;
29
+ import java .util .Collections ;
29
30
import java .util .List ;
30
31
import java .util .Locale ;
31
32
import java .util .Map ;
41
42
import org .apache .maven .model .Plugin ;
42
43
import org .apache .maven .model .PluginManagement ;
43
44
import org .apache .maven .model .ReportPlugin ;
45
+ import org .apache .maven .model .Reporting ;
44
46
import org .apache .maven .model .Resource ;
45
47
import org .apache .maven .plugin .descriptor .PluginDescriptor ;
46
48
import org .apache .maven .plugins .annotations .Component ;
@@ -345,24 +347,28 @@ public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
345
347
private PluginDescriptor plugin ;
346
348
347
349
/**
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.
350
352
*
351
353
* @since 2.1
352
354
*/
353
355
@ Parameter (property = "linkXRef" , defaultValue = "true" )
354
356
private boolean linkXRef ;
355
357
356
358
/**
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}
358
362
*/
359
- @ Parameter ( defaultValue = "${project.reporting.outputDirectory}/xref" )
363
+ @ Parameter
360
364
private File xrefLocation ;
361
365
362
366
/**
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}
364
370
*/
365
- @ Parameter ( defaultValue = "${project.reporting.outputDirectory}/xref-test" )
371
+ @ Parameter
366
372
private File xrefTestLocation ;
367
373
368
374
/**
@@ -482,6 +488,45 @@ protected List<MavenProject> getReactorProjects() {
482
488
return reactorProjects ;
483
489
}
484
490
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
+
485
530
/** {@inheritDoc} */
486
531
public void executeReport (Locale locale ) throws MavenReportException {
487
532
checkDeprecatedParameterUsage (sourceDirectory , "sourceDirectory" , "sourceDirectories" );
@@ -527,30 +572,21 @@ public void executeReport(Locale locale) throws MavenReportException {
527
572
528
573
CheckstyleResults results = checkstyleExecutor .executeCheckstyle (request );
529
574
575
+ boolean haveResults = results .getFileCount () > 0 ;
530
576
CheckstyleReportRenderer r = new CheckstyleReportRenderer (
531
577
getSink (),
532
578
i18n ,
533
579
locale ,
534
580
project ,
535
581
siteTool ,
536
582
effectiveConfigLocation ,
583
+ constructXrefLocation (false , haveResults ),
584
+ constructXrefLocation (true , haveResults ),
585
+ linkXRef ? getTestSourceDirectories () : Collections .emptyList (),
537
586
enableRulesSummary ,
538
587
enableSeveritySummary ,
539
588
enableFilesSummary ,
540
589
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
- }
554
590
if (treeWalkerNames != null ) {
555
591
r .setTreeWalkerNames (treeWalkerNames );
556
592
}
@@ -675,24 +711,6 @@ protected DefaultLogger getConsoleListener() throws MavenReportException {
675
711
return consoleListener ;
676
712
}
677
713
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
-
696
714
private String determineRelativePath (File location ) {
697
715
String relativePath =
698
716
PathTool .getRelativePath (getReportOutputDirectory ().getAbsolutePath (), location .getAbsolutePath ());
@@ -703,17 +721,6 @@ private String determineRelativePath(File location) {
703
721
return relativePath + "/" + location .getName ();
704
722
}
705
723
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
-
717
724
protected List <File > getSourceDirectories () {
718
725
if (sourceDirectories == null ) {
719
726
sourceDirectories = filterBuildTarget (project .getCompileSourceRoots ());
0 commit comments