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