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 java.io.File;
23  import java.net.MalformedURLException;
24  import java.util.List;
25  import java.util.Locale;
26  
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.doxia.sink.Sink;
29  import org.apache.maven.doxia.tools.SiteTool;
30  import org.apache.maven.model.DistributionManagement;
31  import org.apache.maven.model.Model;
32  import org.apache.maven.model.Site;
33  import org.apache.maven.plugins.annotations.Mojo;
34  import org.apache.maven.project.MavenProject;
35  import org.apache.maven.project.MavenProjectBuilder;
36  import org.apache.maven.project.ProjectBuildingException;
37  import org.codehaus.plexus.i18n.I18N;
38  
39  /**
40   * Generates the Project Modules report.
41   *
42   * @author ltheussl
43   * @version $Id: ModulesReport.html 935177 2015-01-05 21:05:55Z michaelo $
44   * @since 2.2
45   */
46  @Mojo( name = "modules" )
47  public class ModulesReport
48      extends AbstractProjectInfoReport
49  {
50      // ----------------------------------------------------------------------
51      // Public methods
52      // ----------------------------------------------------------------------
53  
54      @Override
55      public boolean canGenerateReport()
56      {
57          boolean result = super.canGenerateReport();
58          if ( result && skipEmptyReport )
59          {
60              result = !isEmpty( getProject().getModel().getModules() ) ;
61          }
62  
63          return result;
64      }
65  
66      @Override
67      public void executeReport( Locale locale )
68      {
69          new ModulesRenderer( getSink(), getProject(), mavenProjectBuilder, localRepository,
70                               getI18N( locale ), locale, siteTool ).render();
71      }
72  
73      /** {@inheritDoc} */
74      public String getOutputName()
75      {
76          return "modules";
77      }
78  
79      @Override
80      protected String getI18Nsection()
81      {
82          return "modules";
83      }
84  
85      // ----------------------------------------------------------------------
86      // Private
87      // ----------------------------------------------------------------------
88  
89      /**
90       * Internal renderer class
91       */
92      static class ModulesRenderer
93          extends AbstractProjectInfoRenderer
94      {
95          protected MavenProject project;
96  
97          protected MavenProjectBuilder mavenProjectBuilder;
98  
99          protected ArtifactRepository localRepository;
100 
101         protected SiteTool siteTool;
102 
103         ModulesRenderer( Sink sink, MavenProject project, MavenProjectBuilder mavenProjectBuilder,
104                          ArtifactRepository localRepository, I18N i18n, Locale locale,
105                          SiteTool siteTool )
106         {
107             super( sink, i18n, locale );
108 
109             this.project = project;
110             this.mavenProjectBuilder = mavenProjectBuilder;
111             this.localRepository = localRepository;
112             this.siteTool = siteTool;
113         }
114 
115         @Override
116         protected String getI18Nsection()
117         {
118             return "modules";
119         }
120 
121         @Override
122         public void renderBody()
123         {
124             List<String> modules = project.getModel().getModules();
125 
126             if ( modules == null || modules.isEmpty() )
127              {
128                  startSection( getTitle() );
129 
130                  paragraph( getI18nString( "nolist" ) );
131 
132                  endSection();
133 
134                  return;
135              }
136 
137             startSection( getTitle() );
138 
139             paragraph( getI18nString( "intro" ) );
140 
141             startTable();
142 
143             String name = getI18nString( "header.name" );
144             String description = getI18nString( "header.description" );
145             tableHeader( new String[] { name, description } );
146 
147             final String baseUrl = getDistMgmntSiteUrl( project );
148 
149             for ( String module : modules )
150             {
151                 Model moduleModel;
152                 File f = new File( project.getBasedir(), module + "/pom.xml" );
153                 if ( f.exists() )
154                 {
155                     try
156                     {
157                         moduleModel = mavenProjectBuilder.build( f, localRepository, null ).getModel();
158                     }
159                     catch ( ProjectBuildingException e )
160                     {
161                        throw new IllegalStateException( "Unable to read local module POM", e );
162                     }
163                 }
164                 else
165                 {
166                     moduleModel = new Model();
167                     moduleModel.setName( module );
168                     setDistMgmntSiteUrl( moduleModel, module );
169                 }
170 
171                 final String moduleName =
172                     ( moduleModel.getName() == null ) ? moduleModel.getArtifactId() : moduleModel.getName();
173                 final String moduleHref = getRelativeLink( baseUrl, getDistMgmntSiteUrl( moduleModel ),
174                                             moduleModel.getArtifactId() );
175 
176                 tableRow( new String[] { linkedName( moduleName, moduleHref ), moduleModel.getDescription() } );
177             }
178 
179             endTable();
180 
181             endSection();
182         }
183 
184         private static void setDistMgmntSiteUrl( Model model, String url )
185         {
186             if ( model.getDistributionManagement() == null )
187             {
188                 model.setDistributionManagement( new DistributionManagement() );
189             }
190 
191             if ( model.getDistributionManagement().getSite() == null )
192             {
193                 model.getDistributionManagement().setSite( new Site() );
194             }
195 
196             model.getDistributionManagement().getSite().setUrl( url );
197         }
198 
199         /**
200          * Return distributionManagement.site.url if defined, null otherwise.
201          *
202          * @param project not null
203          * @return could be null
204          */
205         private static String getDistMgmntSiteUrl( MavenProject project )
206         {
207             return getDistMgmntSiteUrl( project.getDistributionManagement() );
208         }
209 
210         /**
211          * Return distributionManagement.site.url if defined, null otherwise.
212          *
213          * @param model not null
214          * @return could be null
215          */
216         private static String getDistMgmntSiteUrl( Model model )
217         {
218             return getDistMgmntSiteUrl( model.getDistributionManagement() );
219         }
220 
221         private static String getDistMgmntSiteUrl( DistributionManagement distMgmnt )
222         {
223             if ( distMgmnt != null && distMgmnt.getSite() != null && distMgmnt.getSite().getUrl() != null )
224             {
225                 return urlEncode( distMgmnt.getSite().getUrl() );
226             }
227 
228             return null;
229         }
230 
231         private static String urlEncode( final String url )
232         {
233             if ( url == null )
234             {
235                 return null;
236             }
237 
238             try
239             {
240                 return new File( url ).toURI().toURL().toExternalForm();
241             }
242             catch ( MalformedURLException ex )
243             {
244                 return url; // this will then throw somewhere else
245             }
246         }
247 
248         // adapted from DefaultSiteTool#appendMenuItem
249         private String getRelativeLink( String baseUrl, String href, String defaultHref )
250         {
251             String selectedHref = href;
252 
253             if ( selectedHref == null )
254             {
255                 selectedHref = defaultHref;
256             }
257 
258             if ( baseUrl != null )
259             {
260                 selectedHref = siteTool.getRelativePath( selectedHref, baseUrl );
261             }
262 
263             if ( selectedHref.endsWith( "/" ) )
264             {
265                 selectedHref = selectedHref.concat( "index.html" );
266             }
267             else
268             {
269                 selectedHref = selectedHref.concat( "/index.html" );
270             }
271 
272             return selectedHref;
273         }
274 
275         private String linkedName( String name, String link )
276         {
277             return "{" + name + ", ./" + link + "}";
278         }
279     }
280 }