View Javadoc
1   package org.apache.maven.report.projectinfo;
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.doxia.sink.Sink;
23  import org.apache.maven.model.DistributionManagement;
24  import org.apache.maven.model.Organization;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.reporting.MavenReportException;
28  import org.codehaus.plexus.util.FileUtils;
29  import org.codehaus.plexus.util.StringUtils;
30  import org.codehaus.plexus.util.xml.Xpp3Dom;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.Locale;
35  
36  /**
37   * Generates the Project Summary report.
38   *
39   * @author Edwin Punzalan
40   * @version $Id: SummaryReport.java 1732734 2016-02-28 14:13:05Z michaelo $
41   * @since 2.0
42   */
43  @Mojo( name = "summary" )
44  public class SummaryReport
45      extends AbstractProjectInfoReport
46  {
47      // ----------------------------------------------------------------------
48      // Mojo parameters
49      // ----------------------------------------------------------------------
50  
51      // ----------------------------------------------------------------------
52      // Public methods
53      // ----------------------------------------------------------------------
54  
55      @Override
56      protected void executeReport( Locale locale )
57          throws MavenReportException
58      {
59          new ProjectSummaryRenderer( getSink(), locale ).render();
60      }
61  
62      /** {@inheritDoc} */
63      public String getOutputName()
64      {
65          return "project-summary";
66      }
67  
68      @Override
69      protected String getI18Nsection()
70      {
71          return "summary";
72      }
73  
74      // ----------------------------------------------------------------------
75      // Private
76      // ----------------------------------------------------------------------
77  
78      /**
79       * Internal renderer class
80       */
81      private class ProjectSummaryRenderer
82          extends AbstractProjectInfoRenderer
83      {
84          ProjectSummaryRenderer( Sink sink, Locale locale )
85          {
86              super( sink, getI18N( locale ), locale );
87          }
88  
89          @Override
90          protected String getI18Nsection()
91          {
92              return "summary";
93          }
94  
95          @Override
96          protected void renderBody()
97          {
98              startSection( getTitle() );
99  
100             // general information sub-section
101             startSection( getI18nString( "general.title" ) );
102             startTable();
103             tableHeader( new String[] { getI18nString( "field" ), getI18nString( "value" ) } );
104             tableRow( new String[] { getI18nString( "general.name" ), project.getName() } );
105             tableRow( new String[] { getI18nString( "general.description" ), project.getDescription() } );
106             tableRowWithLink( new String[] { getI18nString( "general.homepage" ), project.getUrl() } );
107             endTable();
108             endSection();
109 
110             // organization sub-section
111             startSection( getI18nString( "organization.title" ) );
112             Organization organization = project.getOrganization();
113             if ( organization == null )
114             {
115                 paragraph( getI18nString( "noorganization" ) );
116             }
117             else
118             {
119                 startTable();
120                 tableHeader( new String[] { getI18nString( "field" ), getI18nString( "value" ) } );
121                 tableRow( new String[] { getI18nString( "organization.name" ), organization.getName() } );
122                 tableRowWithLink( new String[] { getI18nString( "organization.url" ), organization.getUrl() } );
123                 endTable();
124             }
125             endSection();
126 
127             // build section
128             startSection( getI18nString( "build.title" ) );
129             startTable();
130             tableHeader( new String[] { getI18nString( "field" ), getI18nString( "value" ) } );
131             tableRow( new String[] { getI18nString( "build.groupid" ), project.getGroupId() } );
132             tableRow( new String[] { getI18nString( "build.artifactid" ), project.getArtifactId() } );
133             tableRow( new String[] { getI18nString( "build.version" ), project.getVersion() } );
134             tableRow( new String[] { getI18nString( "build.type" ), project.getPackaging() } );
135             if ( isJavaProject( project ) )
136             {
137                 tableRow( new String[] { getI18nString( "build.javaVersion" ), getMinimumJavaVersion() } );
138             }
139             endTable();
140             endSection();
141 
142             // download section
143             DistributionManagement distributionManagement = project.getDistributionManagement();
144             if ( distributionManagement != null )
145             {
146                 if ( StringUtils.isNotEmpty( distributionManagement.getDownloadUrl() ) )
147                 {
148                     startSection( getI18nString( "download" ) );
149                     link( distributionManagement.getDownloadUrl(), distributionManagement.getDownloadUrl() );
150                     endSection();
151                 }
152             }
153 
154             endSection();
155         }
156 
157         private String getMinimumJavaVersion()
158         {
159 
160             final String pluginId = "org.apache.maven.plugins:maven-compiler-plugin";
161             String sourceConfigured = getPluginParameter( pluginId, "source" );
162             String targetConfigured = getPluginParameter( pluginId, "target" );
163 
164             String forkFlag = getPluginParameter( pluginId, "fork" );
165             String compilerVersionConfigured = null;
166             if ( "true".equalsIgnoreCase( forkFlag ) )
167             {
168                 compilerVersionConfigured = getPluginParameter( pluginId, "compilerVersion" );
169             }
170 
171             String minimumJavaVersion = compilerVersionConfigured;
172             if ( targetConfigured != null )
173             {
174                 minimumJavaVersion = targetConfigured;
175             }
176             else if ( sourceConfigured != null )
177             {
178                 minimumJavaVersion = sourceConfigured;
179             }
180             else
181             {
182                 // ${maven.compiler.target} default value
183                 minimumJavaVersion = project.getProperties().getProperty( "maven.compiler.target" );
184 
185                 // default to 1.5 if not set?
186             }
187 
188             return minimumJavaVersion;
189         }
190 
191         private void tableRowWithLink( String[] content )
192         {
193             sink.tableRow();
194 
195             for ( int ctr = 0; ctr < content.length; ctr++ )
196             {
197                 String cell = content[ctr];
198 
199                 sink.tableCell();
200 
201                 if ( StringUtils.isEmpty( cell ) )
202                 {
203                     sink.text( "-" );
204                 }
205                 else if ( ctr == content.length - 1 && cell.length() > 0 )
206                 {
207                     sink.link( cell );
208                     sink.text( cell );
209                     sink.link_();
210                 }
211                 else
212                 {
213                     sink.text( cell );
214                 }
215                 sink.tableCell_();
216             }
217 
218             sink.tableRow_();
219         }
220 
221         /**
222          * @param project not null
223          * @return return <code>true</code> if the Maven project sounds like a Java Project, i.e. has a java type
224          *         packaging (like jar, war...) or java files in the source directory, <code>false</code> otherwise.
225          * @since 2.3
226          */
227         private boolean isJavaProject( MavenProject project )
228         {
229             String packaging = project.getPackaging().trim().toLowerCase( Locale.ENGLISH );
230             if ( packaging.equals( "pom" ) )
231             {
232                 return false;
233             }
234 
235             // some commons java packaging
236             if ( packaging.equals( "jar" ) || packaging.equals( "ear" ) || packaging.equals( "war" )
237                 || packaging.equals( "rar" ) || packaging.equals( "sar" ) || packaging.equals( "har" )
238                 || packaging.equals( "par" ) || packaging.equals( "ejb" ) )
239             {
240                 return true;
241             }
242 
243             // java files in the source directory?
244             final File sourceDir = new File( project.getBuild().getSourceDirectory() );
245             if ( sourceDir.exists() )
246             {
247                 try
248                 {
249                     if ( FileUtils.getFileNames( sourceDir, "**/*.java", null, false ).size() > 0 )
250                     {
251                         return true;
252                     }
253                 }
254                 catch ( IOException e )
255                 {
256                     //ignored
257                 }
258             }
259 
260             // maven-compiler-plugin ?
261             Xpp3Dom pluginConfig =
262                 project.getGoalConfiguration( "org.apache.maven.plugins", "maven-compiler-plugin", null, null );
263             if ( pluginConfig != null )
264             {
265                 return true;
266             }
267 
268             return false;
269         }
270     }
271 }