View Javadoc
1   package org.apache.maven.plugins.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugins.annotations.Execute;
23  import org.apache.maven.plugins.annotations.LifecyclePhase;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.Parameter;
26  import org.apache.maven.project.MavenProject;
27  
28  import java.io.File;
29  import java.util.Locale;
30  import java.util.ResourceBundle;
31  
32  import static org.apache.maven.shared.utils.StringUtils.isEmpty;
33  
34  /**
35   * Creates a nicely formatted Surefire Test Report in html format.
36   *
37   * @author <a href="mailto:jruiz@exist.com">Johnny R. Ruiz III</a>
38   */
39  @Mojo( name = "report", inheritByDefault = false )
40  @Execute( lifecycle = "surefire", phase = LifecyclePhase.TEST )
41  @SuppressWarnings( "unused" )
42  public class SurefireReportMojo
43      extends AbstractSurefireReportMojo
44  {
45  
46      /**
47       * The filename to use for the report.
48       */
49      @Parameter( defaultValue = "surefire-report", property = "outputName", required = true )
50      private String outputName;
51  
52      /**
53       * If set to true the surefire report will be generated even when there are no surefire result files.
54       * Defaults to {@code true} to preserve legacy behaviour pre 2.10.
55       * @since 2.11
56       */
57      @Parameter( defaultValue = "true", property = "alwaysGenerateSurefireReport" )
58      private boolean alwaysGenerateSurefireReport;
59  
60      /**
61       * If set to true the surefire report generation will be skipped.
62       * @since 2.11
63       */
64      @Parameter( defaultValue = "false", property = "skipSurefireReport" )
65      private boolean skipSurefireReport;
66  
67      /**
68       * A custom title of the report for the menu and the project reports page.
69       * @since 2.21.0
70       */
71      @Parameter( defaultValue = "", property = "surefire.report.title" )
72      private String title;
73  
74      /**
75       * A custom description for the project reports page.
76       * @since 2.21.0
77       */
78      @Parameter( defaultValue = "", property = "surefire.report.description" )
79      private String description;
80  
81      @Override
82      protected File getSurefireReportsDirectory( MavenProject subProject )
83      {
84          String buildDir = subProject.getBuild().getDirectory();
85          return new File( buildDir + "/surefire-reports" );
86      }
87  
88      @Override
89      public String getOutputName()
90      {
91          return outputName;
92      }
93  
94      @Override
95      protected LocalizedProperties getBundle( Locale locale, ClassLoader resourceBundleClassLoader )
96      {
97          ResourceBundle bundle = ResourceBundle.getBundle( "surefire-report", locale, resourceBundleClassLoader );
98          return new LocalizedProperties( bundle )
99          {
100             @Override
101             public String getReportName()
102             {
103                 return isEmpty( SurefireReportMojo.this.getTitle() )
104                         ? toLocalizedValue( "report.surefire.name" ) : SurefireReportMojo.this.getTitle();
105             }
106 
107             @Override
108             public String getReportDescription()
109             {
110                 return isEmpty( SurefireReportMojo.this.getDescription() )
111                         ? toLocalizedValue( "report.surefire.description" ) : SurefireReportMojo.this.getDescription();
112             }
113 
114             @Override
115             public String getReportHeader()
116             {
117                 return isEmpty( SurefireReportMojo.this.getTitle() )
118                         ? toLocalizedValue( "report.surefire.header" ) : SurefireReportMojo.this.getTitle();
119             }
120         };
121     }
122 
123     @Override
124     protected boolean isSkipped()
125     {
126         return skipSurefireReport;
127     }
128 
129     @Override
130     protected boolean isGeneratedWhenNoResults()
131     {
132         return alwaysGenerateSurefireReport;
133     }
134 
135     @Override
136     public void setTitle( String title )
137     {
138         this.title = title;
139     }
140 
141     @Override
142     public String getTitle()
143     {
144         return title;
145     }
146 
147     @Override
148     public void setDescription( String description )
149     {
150         this.description = description;
151     }
152 
153     @Override
154     public String getDescription()
155     {
156         return description;
157     }
158 }