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 information reports summary.
38   *
39   * @author Edwin Punzalan
40   * @version $Id: ProjectSummaryReport.html 861765 2013-05-12 18:46:29Z hboutemy $
41   * @since 2.0
42   */
43  @Mojo( name = "summary" )
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             
156             final String pluginId = "org.apache.maven.plugins:maven-compiler-plugin";
157             String sourceConfigured = getPluginParameter( pluginId, "source" );
158             String targetConfigured = getPluginParameter( pluginId, "target" );
159             
160             String forkFlag = getPluginParameter( pluginId, "fork" );
161             String compilerVersionConfigured = null;
162                 if ( "true".equalsIgnoreCase( forkFlag ) )
163                 {
164                     compilerVersionConfigured = getPluginParameter( pluginId, "compilerVersion" );
165                 }
166 
167             String minimumJavaVersion = compilerVersionConfigured;
168             if ( targetConfigured != null )
169             {
170                 minimumJavaVersion = targetConfigured;
171             }
172             else if ( sourceConfigured != null )
173             {
174                 minimumJavaVersion = sourceConfigured;
175             }
176             else
177             {
178                 // no source, target, compilerVersion: toolchain? default target attribute of current
179                 // maven-compiler-plugin's version? analyze packaged jar (like dependencies)?
180             }
181 
182             return minimumJavaVersion;
183         }
184 
185         private void tableRowWithLink( String[] content )
186         {
187             sink.tableRow();
188 
189             for ( int ctr = 0; ctr < content.length; ctr++ )
190             {
191                 String cell = content[ctr];
192 
193                 sink.tableCell();
194 
195                 if ( StringUtils.isEmpty( cell ) )
196                 {
197                     sink.text( "-" );
198                 }
199                 else if ( ctr == content.length - 1 && cell.length() > 0 )
200                 {
201                     sink.link( cell );
202                     sink.text( cell );
203                     sink.link_();
204                 }
205                 else
206                 {
207                     sink.text( cell );
208                 }
209                 sink.tableCell_();
210             }
211 
212             sink.tableRow_();
213         }
214 
215         /**
216          * @param project not null
217          * @return return <code>true</code> if the Maven project sounds like a Java Project, i.e. has a java type
218          *         packaging (like jar, war...) or java files in the source directory, <code>false</code> otherwise.
219          * @since 2.3
220          */
221         private boolean isJavaProject( MavenProject project )
222         {
223             String packaging = project.getPackaging().trim().toLowerCase( Locale.ENGLISH );
224             if ( packaging.equals( "pom" ) )
225             {
226                 return false;
227             }
228 
229             // some commons java packaging
230             if ( packaging.equals( "jar" ) || packaging.equals( "ear" ) || packaging.equals( "war" )
231                 || packaging.equals( "rar" ) || packaging.equals( "sar" ) || packaging.equals( "har" )
232                 || packaging.equals( "par" ) || packaging.equals( "ejb" ) )
233             {
234                 return true;
235             }
236 
237             // java files in the source directory?
238             final File sourceDir = new File( project.getBuild().getSourceDirectory() );
239             if ( sourceDir.exists() )
240             {
241                 try
242                 {
243                     if ( FileUtils.getFileNames( sourceDir, "**/*.java", null, false ).size() > 0 )
244                     {
245                         return true;
246                     }
247                 }
248                 catch ( IOException e )
249                 {
250                     //ignored
251                 }
252             }
253 
254             // maven-compiler-plugin ?
255             Xpp3Dom pluginConfig =
256                 project.getGoalConfiguration( "org.apache.maven.plugins", "maven-compiler-plugin", null, null );
257             if ( pluginConfig != null )
258             {
259                 return true;
260             }
261 
262             return false;
263         }
264     }
265 }