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 ;
42
43
import org .apache .maven .model .Plugin ;
43
44
import org .apache .maven .model .PluginManagement ;
44
45
import org .apache .maven .model .ReportPlugin ;
46
+ import org .apache .maven .model .Reporting ;
45
47
import org .apache .maven .model .Resource ;
46
48
import org .apache .maven .plugin .descriptor .PluginDescriptor ;
47
49
import org .apache .maven .plugins .annotations .Component ;
@@ -346,24 +348,28 @@ public abstract class AbstractCheckstyleReport extends AbstractMavenReport {
346
348
private PluginDescriptor plugin ;
347
349
348
350
/**
349
- * Link the violation line numbers to the source xref. Will link
350
- * automatically if Maven JXR plugin is being used.
351
+ * Link the violation line numbers to the (Test) Source XRef. Links will be created automatically if the JXR plugin is
352
+ * being used.
351
353
*
352
354
* @since 2.1
353
355
*/
354
356
@ Parameter (property = "linkXRef" , defaultValue = "true" )
355
357
private boolean linkXRef ;
356
358
357
359
/**
358
- * Location of the Xrefs to link to.
360
+ * Location where Source XRef is generated for this project.
361
+ * <br>
362
+ * <strong>Default</strong>: {@link #getReportOutputDirectory()} + {@code /xref}
359
363
*/
360
- @ Parameter ( defaultValue = "${project.reporting.outputDirectory}/xref" )
364
+ @ Parameter
361
365
private File xrefLocation ;
362
366
363
367
/**
364
- * Location of the XrefTests to link to.
368
+ * Location where Test Source XRef is generated for this project.
369
+ * <br>
370
+ * <strong>Default</strong>: {@link #getReportOutputDirectory()} + {@code /xref-test}
365
371
*/
366
- @ Parameter ( defaultValue = "${project.reporting.outputDirectory}/xref-test" )
372
+ @ Parameter
367
373
private File xrefTestLocation ;
368
374
369
375
/**
@@ -483,6 +489,45 @@ protected List<MavenProject> getReactorProjects() {
483
489
return reactorProjects ;
484
490
}
485
491
492
+ protected String constructXrefLocation (boolean test , boolean haveResults ) {
493
+ String location = null ;
494
+ if (linkXRef ) {
495
+ File xrefLocation = getXrefLocation (test );
496
+
497
+ String relativePath = PathTool .getRelativePath (
498
+ getReportOutputDirectory ().getAbsolutePath (), xrefLocation .getAbsolutePath ());
499
+ if (relativePath == null || relativePath .isEmpty ()) {
500
+ relativePath = "." ;
501
+ }
502
+ relativePath = relativePath + "/" + xrefLocation .getName ();
503
+ if (xrefLocation .exists ()) {
504
+ // XRef was already generated by manual execution of a lifecycle binding
505
+ location = relativePath ;
506
+ } else {
507
+ // Not yet generated - check if the report is on its way
508
+ Reporting reporting = project .getModel ().getReporting ();
509
+ List <ReportPlugin > reportPlugins =
510
+ reporting != null ? reporting .getPlugins () : Collections .<ReportPlugin >emptyList ();
511
+ for (ReportPlugin plugin : reportPlugins ) {
512
+ String artifactId = plugin .getArtifactId ();
513
+ if ("maven-jxr-plugin" .equals (artifactId ) || "jxr-maven-plugin" .equals (artifactId )) {
514
+ location = relativePath ;
515
+ }
516
+ }
517
+ }
518
+
519
+ if (location == null && haveResults ) {
520
+ getLog ().warn ("Unable to locate" + (test ? " Test" : "" ) + " Source XRef to link to - DISABLED" );
521
+ }
522
+ }
523
+ return location ;
524
+ }
525
+
526
+ protected File getXrefLocation (boolean test ) {
527
+ File location = test ? xrefTestLocation : xrefLocation ;
528
+ return location != null ? location : new File (getReportOutputDirectory (), test ? "xref-test" : "xref" );
529
+ }
530
+
486
531
/** {@inheritDoc} */
487
532
public void executeReport (Locale locale ) throws MavenReportException {
488
533
checkDeprecatedParameterUsage (sourceDirectory , "sourceDirectory" , "sourceDirectories" );
@@ -528,30 +573,21 @@ public void executeReport(Locale locale) throws MavenReportException {
528
573
529
574
CheckstyleResults results = checkstyleExecutor .executeCheckstyle (request );
530
575
576
+ boolean haveResults = results .getFileCount () > 0 ;
531
577
CheckstyleReportRenderer r = new CheckstyleReportRenderer (
532
578
getSink (),
533
579
i18n ,
534
580
locale ,
535
581
project ,
536
582
siteTool ,
537
583
effectiveConfigLocation ,
584
+ constructXrefLocation (false , haveResults ),
585
+ constructXrefLocation (true , haveResults ),
586
+ linkXRef ? getTestSourceDirectories () : Collections .emptyList (),
538
587
enableRulesSummary ,
539
588
enableSeveritySummary ,
540
589
enableFilesSummary ,
541
590
results );
542
- if (linkXRef ) {
543
- initializeXrefLocation (r );
544
- if (r .getXrefLocation () == null && results .getFileCount () > 0 ) {
545
- getLog ().warn ("Unable to locate Source XRef to link to - DISABLED" );
546
- }
547
-
548
- initializeXrefTestLocation (r );
549
- if (r .getXrefTestLocation () == null && results .getFileCount () > 0 ) {
550
- getLog ().warn ("Unable to locate Test Source XRef to link to - DISABLED" );
551
- }
552
-
553
- r .setTestSourceDirectories (getTestSourceDirectories ());
554
- }
555
591
if (treeWalkerNames != null ) {
556
592
r .setTreeWalkerNames (treeWalkerNames );
557
593
}
@@ -682,24 +718,6 @@ protected DefaultLogger getConsoleListener() throws MavenReportException {
682
718
return consoleListener ;
683
719
}
684
720
685
- private void initializeXrefLocation (CheckstyleReportRenderer renderer ) {
686
- String relativePath = determineRelativePath (xrefLocation );
687
- if (xrefLocation .exists () || checkMavenJxrPluginIsConfigured ()) {
688
- // XRef was already generated by manual execution of a lifecycle binding
689
- // the report is on its way
690
- renderer .setXrefLocation (relativePath );
691
- }
692
- }
693
-
694
- private void initializeXrefTestLocation (CheckstyleReportRenderer renderer ) {
695
- String relativePath = determineRelativePath (xrefTestLocation );
696
- if (xrefTestLocation .exists () || checkMavenJxrPluginIsConfigured ()) {
697
- // XRef was already generated by manual execution of a lifecycle binding
698
- // the report is on its way
699
- renderer .setXrefTestLocation (relativePath );
700
- }
701
- }
702
-
703
721
private String determineRelativePath (File location ) {
704
722
String relativePath =
705
723
PathTool .getRelativePath (getReportOutputDirectory ().getAbsolutePath (), location .getAbsolutePath ());
@@ -710,17 +728,6 @@ private String determineRelativePath(File location) {
710
728
return relativePath + "/" + location .getName ();
711
729
}
712
730
713
- private boolean checkMavenJxrPluginIsConfigured () {
714
- for (ReportPlugin report : (Iterable <ReportPlugin >) getProject ().getReportPlugins ()) {
715
- String artifactId = report .getArtifactId ();
716
- if ("maven-jxr-plugin" .equals (artifactId ) || "jxr-maven-plugin" .equals (artifactId )) {
717
- return true ;
718
- }
719
- }
720
-
721
- return false ;
722
- }
723
-
724
731
protected List <File > getSourceDirectories () {
725
732
if (sourceDirectories == null ) {
726
733
sourceDirectories = filterBuildTarget (project .getCompileSourceRoots ());
0 commit comments