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