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