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 1049441 2010-12-15 07:05:00Z ltheussl $
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             }
149 
150             endSection();
151         }
152 
153         private String getMinimumJavaVersion()
154         {
155             Xpp3Dom pluginConfig =
156                 project.getGoalConfiguration( "org.apache.maven.plugins", "maven-compiler-plugin", null, null );
157 
158             String source = null;
159             String target = null;
160             String compilerVersion = null;
161 
162             if ( pluginConfig != null )
163             {
164                 source = getChildValue( pluginConfig, "source" );
165                 target = getChildValue( pluginConfig, "target" );
166 
167                 String fork = getChildValue( pluginConfig, "fork" );
168                 if ( "true".equalsIgnoreCase( fork ) )
169                 {
170                     compilerVersion = getChildValue( pluginConfig, "compilerVersion" );
171                 }
172             }
173 
174             String minimumJavaVersion = compilerVersion;
175             if ( target != null )
176             {
177                 minimumJavaVersion = target;
178             }
179             else if ( source != null )
180             {
181                 minimumJavaVersion = source;
182             }
183             else if ( compilerVersion != null )
184             {
185                 minimumJavaVersion = compilerVersion;
186             }
187             else
188             {
189                 // no source, target, compilerVersion: toolchain? default target attribute of current
190                 // maven-compiler-plugin's version? analyze packaged jar (like dependencies)?
191             }
192 
193             return minimumJavaVersion;
194         }
195 
196         private String getChildValue( Xpp3Dom parent, String childName )
197         {
198             if ( parent == null )
199             {
200                 return null;
201             }
202 
203             Xpp3Dom child = parent.getChild( childName );
204 
205             if ( child == null )
206             {
207                 return null;
208             }
209 
210             String value = child.getValue();
211 
212             if ( value == null || value.trim().length() == 0 )
213             {
214                 return null;
215             }
216 
217             return value.trim();
218         }
219 
220         private void tableRowWithLink( String[] content )
221         {
222             sink.tableRow();
223 
224             for ( int ctr = 0; ctr < content.length; ctr++ )
225             {
226                 String cell = content[ctr];
227 
228                 sink.tableCell();
229 
230                 if ( StringUtils.isEmpty( cell ) )
231                 {
232                     sink.text( "-" );
233                 }
234                 else if ( ctr == content.length - 1 && cell.length() > 0 )
235                 {
236                     sink.link( cell );
237                     sink.text( cell );
238                     sink.link_();
239                 }
240                 else
241                 {
242                     sink.text( cell );
243                 }
244                 sink.tableCell_();
245             }
246 
247             sink.tableRow_();
248         }
249 
250         /**
251          * @param project not null
252          * @return return <code>true</code> if the Maven project sounds like a Java Project, i.e. has a java type
253          *         packaging (like jar, war...) or java files in the source directory, <code>false</code> otherwise.
254          * @since 2.3
255          */
256         private boolean isJavaProject( MavenProject project )
257         {
258             String packaging = project.getPackaging().trim().toLowerCase( Locale.ENGLISH );
259             if ( packaging.equals( "pom" ) )
260             {
261                 return false;
262             }
263 
264             // some commons java packaging
265             if ( packaging.equals( "jar" ) || packaging.equals( "ear" ) || packaging.equals( "war" )
266                 || packaging.equals( "rar" ) || packaging.equals( "sar" ) || packaging.equals( "har" )
267                 || packaging.equals( "par" ) || packaging.equals( "ejb" ) )
268             {
269                 return true;
270             }
271 
272             // java files in the source directory?
273             final File sourceDir = new File( project.getBuild().getSourceDirectory() );
274             if ( sourceDir.exists() )
275             {
276                 try
277                 {
278                     if ( FileUtils.getFileNames( sourceDir, "**/*.java", null, false ).size() > 0 )
279                     {
280                         return true;
281                     }
282                 }
283                 catch ( IOException e )
284                 {
285                     //ignored
286                 }
287             }
288 
289             // maven-compiler-plugin ?
290             Xpp3Dom pluginConfig =
291                 project.getGoalConfiguration( "org.apache.maven.plugins", "maven-compiler-plugin", null, null );
292             if ( pluginConfig != null )
293             {
294                 return true;
295             }
296 
297             return false;
298         }
299     }
300 }