View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.report.projectinfo;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Locale;
24  
25  import org.apache.maven.doxia.sink.Sink;
26  import org.apache.maven.model.DistributionManagement;
27  import org.apache.maven.model.Organization;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.reporting.MavenReportException;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.codehaus.plexus.util.StringUtils;
33  import org.codehaus.plexus.util.xml.Xpp3Dom;
34  
35  /**
36   * Generates the Project Summary report.
37   *
38   * @author Edwin Punzalan
39   * @since 2.0
40   */
41  @Mojo(name = "summary")
42  public class SummaryReport extends AbstractProjectInfoReport {
43      // ----------------------------------------------------------------------
44      // Mojo parameters
45      // ----------------------------------------------------------------------
46  
47      // ----------------------------------------------------------------------
48      // Public methods
49      // ----------------------------------------------------------------------
50  
51      @Override
52      protected void executeReport(Locale locale) throws MavenReportException {
53          new ProjectSummaryRenderer(getSink(), locale).render();
54      }
55  
56      /** {@inheritDoc} */
57      public String getOutputName() {
58          return "summary";
59      }
60  
61      @Override
62      protected String getI18Nsection() {
63          return "summary";
64      }
65  
66      // ----------------------------------------------------------------------
67      // Private
68      // ----------------------------------------------------------------------
69  
70      /**
71       * Internal renderer class
72       */
73      private class ProjectSummaryRenderer extends AbstractProjectInfoRenderer {
74          ProjectSummaryRenderer(Sink sink, Locale locale) {
75              super(sink, getI18N(locale), locale);
76          }
77  
78          @Override
79          protected String getI18Nsection() {
80              return "summary";
81          }
82  
83          @Override
84          protected void renderBody() {
85              startSection(getTitle());
86  
87              // general information sub-section
88              startSection(getI18nString("general.title"));
89              startTable();
90              tableHeader(new String[] {getI18nString("field"), getI18nString("value")});
91              tableRow(new String[] {getI18nString("general.name"), project.getName()});
92              tableRow(new String[] {getI18nString("general.description"), project.getDescription()});
93              tableRowWithLink(new String[] {getI18nString("general.homepage"), project.getUrl()});
94              endTable();
95              endSection();
96  
97              // organization sub-section
98              startSection(getI18nString("organization.title"));
99              Organization organization = project.getOrganization();
100             if (organization == null) {
101                 paragraph(getI18nString("noorganization"));
102             } else {
103                 startTable();
104                 tableHeader(new String[] {getI18nString("field"), getI18nString("value")});
105                 tableRow(new String[] {getI18nString("organization.name"), organization.getName()});
106                 tableRowWithLink(new String[] {getI18nString("organization.url"), organization.getUrl()});
107                 endTable();
108             }
109             endSection();
110 
111             // build section
112             startSection(getI18nString("build.title"));
113             startTable();
114             tableHeader(new String[] {getI18nString("field"), getI18nString("value")});
115             tableRow(new String[] {getI18nString("build.groupid"), project.getGroupId()});
116             tableRow(new String[] {getI18nString("build.artifactid"), project.getArtifactId()});
117             tableRow(new String[] {getI18nString("build.version"), project.getVersion()});
118             tableRow(new String[] {getI18nString("build.type"), project.getPackaging()});
119             if (isJavaProject(project)) {
120                 tableRow(new String[] {getI18nString("build.javaVersion"), getMinimumJavaVersion()});
121             }
122             endTable();
123             endSection();
124 
125             // download section
126             DistributionManagement distributionManagement = project.getDistributionManagement();
127             if (distributionManagement != null) {
128                 if (StringUtils.isNotEmpty(distributionManagement.getDownloadUrl())) {
129                     startSection(getI18nString("download"));
130                     link(distributionManagement.getDownloadUrl(), distributionManagement.getDownloadUrl());
131                     endSection();
132                 }
133             }
134 
135             endSection();
136         }
137 
138         private String getMinimumJavaVersion() {
139 
140             final String pluginId = "org.apache.maven.plugins:maven-compiler-plugin";
141             String sourceConfigured = getPluginParameter(pluginId, "source");
142             String targetConfigured = getPluginParameter(pluginId, "target");
143 
144             String forkFlag = getPluginParameter(pluginId, "fork");
145             String compilerVersionConfigured = null;
146             if ("true".equalsIgnoreCase(forkFlag)) {
147                 compilerVersionConfigured = getPluginParameter(pluginId, "compilerVersion");
148             }
149 
150             String minimumJavaVersion = compilerVersionConfigured;
151             if (targetConfigured != null) {
152                 minimumJavaVersion = targetConfigured;
153             } else if (sourceConfigured != null) {
154                 minimumJavaVersion = sourceConfigured;
155             } else {
156                 // ${maven.compiler.target} default value
157                 minimumJavaVersion = project.getProperties().getProperty("maven.compiler.target");
158 
159                 // default to 1.5 if not set?
160             }
161 
162             return minimumJavaVersion;
163         }
164 
165         private void tableRowWithLink(String[] content) {
166             sink.tableRow();
167 
168             for (int ctr = 0; ctr < content.length; ctr++) {
169                 String cell = content[ctr];
170 
171                 sink.tableCell();
172 
173                 if (cell == null || cell.isEmpty()) {
174                     sink.text("-");
175                 } else if (ctr == content.length - 1 && cell.length() > 0) {
176                     sink.link(cell);
177                     sink.text(cell);
178                     sink.link_();
179                 } else {
180                     sink.text(cell);
181                 }
182                 sink.tableCell_();
183             }
184 
185             sink.tableRow_();
186         }
187 
188         /**
189          * @param project not null
190          * @return return <code>true</code> if the Maven project sounds like a Java Project, i.e. has a java type
191          *         packaging (like jar, war...) or java files in the source directory, <code>false</code> otherwise.
192          * @since 2.3
193          */
194         private boolean isJavaProject(MavenProject project) {
195             String packaging = project.getPackaging().trim().toLowerCase(Locale.ENGLISH);
196             if (packaging.equals("pom")) {
197                 return false;
198             }
199 
200             // some commons java packaging
201             if (packaging.equals("jar")
202                     || packaging.equals("ear")
203                     || packaging.equals("war")
204                     || packaging.equals("rar")
205                     || packaging.equals("sar")
206                     || packaging.equals("har")
207                     || packaging.equals("par")
208                     || packaging.equals("ejb")) {
209                 return true;
210             }
211 
212             // java files in the source directory?
213             final File sourceDir = new File(project.getBuild().getSourceDirectory());
214             if (sourceDir.exists()) {
215                 try {
216                     if (!FileUtils.getFileNames(sourceDir, "**/*.java", null, false)
217                             .isEmpty()) {
218                         return true;
219                     }
220                 } catch (IOException e) {
221                     // ignored
222                 }
223             }
224 
225             // maven-compiler-plugin ?
226             Xpp3Dom pluginConfig =
227                     project.getGoalConfiguration("org.apache.maven.plugins", "maven-compiler-plugin", null, null);
228             return pluginConfig != null;
229         }
230     }
231 }