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,26 @@ 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. The default value is calculated from
360
+ * {@link #getReportOutputDirectory()} and concatenated with {@code xref}.
358
361
*/
359
- @ Parameter ( defaultValue = "${project.reporting.outputDirectory}/xref" )
362
+ @ Parameter
360
363
private File xrefLocation ;
361
364
362
365
/**
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}.
364
368
*/
365
- @ Parameter ( defaultValue = "${project.reporting.outputDirectory}/xref-test" )
369
+ @ Parameter
366
370
private File xrefTestLocation ;
367
371
368
372
/**
@@ -482,6 +486,45 @@ protected List<MavenProject> getReactorProjects() {
482
486
return reactorProjects ;
483
487
}
484
488
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
+
485
528
/** {@inheritDoc} */
486
529
public void executeReport (Locale locale ) throws MavenReportException {
487
530
checkDeprecatedParameterUsage (sourceDirectory , "sourceDirectory" , "sourceDirectories" );
@@ -527,30 +570,21 @@ public void executeReport(Locale locale) throws MavenReportException {
527
570
528
571
CheckstyleResults results = checkstyleExecutor .executeCheckstyle (request );
529
572
573
+ boolean haveResults = results .getFileCount () > 0 ;
530
574
CheckstyleReportRenderer r = new CheckstyleReportRenderer (
531
575
getSink (),
532
576
i18n ,
533
577
locale ,
534
578
project ,
535
579
siteTool ,
536
580
effectiveConfigLocation ,
581
+ constructXrefLocation (false , haveResults ),
582
+ constructXrefLocation (true , haveResults ),
583
+ linkXRef ? getTestSourceDirectories () : Collections .emptyList (),
537
584
enableRulesSummary ,
538
585
enableSeveritySummary ,
539
586
enableFilesSummary ,
540
587
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
588
if (treeWalkerNames != null ) {
555
589
r .setTreeWalkerNames (treeWalkerNames );
556
590
}
@@ -675,24 +709,6 @@ protected DefaultLogger getConsoleListener() throws MavenReportException {
675
709
return consoleListener ;
676
710
}
677
711
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
712
private String determineRelativePath (File location ) {
697
713
String relativePath = PathTool .getRelativePath (getReportOutputDirectory ().getAbsolutePath (),
698
714
location .getAbsolutePath ());
@@ -703,17 +719,6 @@ private String determineRelativePath(File location) {
703
719
return relativePath + "/" + location .getName ();
704
720
}
705
721
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
722
protected List <File > getSourceDirectories () {
718
723
if (sourceDirectories == null ) {
719
724
sourceDirectories = filterBuildTarget (project .getCompileSourceRoots ());
0 commit comments