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.doxia.tools.SiteTool;
24  import org.apache.maven.model.Model;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.i18n.I18N;
28  
29  import java.util.List;
30  import java.util.Locale;
31  
32  /**
33   * Generates the Project Modules report.
34   *
35   * @author ltheussl
36   * @version $Id: ModulesReport.html 861765 2013-05-12 18:46:29Z hboutemy $
37   * @since 2.2
38   */
39  @Mojo( name = "modules" )
40  public class ModulesReport
41      extends AbstractProjectInfoReport
42  {
43      // ----------------------------------------------------------------------
44      // Public methods
45      // ----------------------------------------------------------------------
46  
47      @Override
48      public void executeReport( Locale locale )
49      {
50          new ModulesRenderer( getSink(), getProject(), getI18N( locale ), locale, siteTool ).render();
51      }
52  
53      /** {@inheritDoc} */
54      public String getOutputName()
55      {
56          return "modules";
57      }
58  
59      @Override
60      protected String getI18Nsection()
61      {
62          return "modules";
63      }
64  
65      @Override
66      public boolean canGenerateReport()
67      {
68          return ( getProject().getModel().getModules() != null && !getProject().getModel().getModules().isEmpty() );
69      }
70  
71      // ----------------------------------------------------------------------
72      // Private
73      // ----------------------------------------------------------------------
74  
75      /**
76       * Internal renderer class
77       */
78      static class ModulesRenderer
79          extends AbstractProjectInfoRenderer
80      {
81          protected MavenProject project;
82  
83          protected SiteTool siteTool;
84  
85          ModulesRenderer( Sink sink, MavenProject project, I18N i18n, Locale locale, SiteTool siteTool )
86          {
87              super( sink, i18n, locale );
88  
89              this.project = project;
90              this.siteTool = siteTool;
91          }
92  
93          @Override
94          protected String getI18Nsection()
95          {
96              return "modules";
97          }
98  
99          @Override
100         public void renderBody()
101         {
102             startSection( getTitle() );
103 
104             paragraph( getI18nString( "intro" ) );
105 
106             startTable();
107 
108             String name = getI18nString( "header.name" );
109             String description = getI18nString( "header.description" );
110             tableHeader( new String[] {name, description} );
111 
112             final String baseURL = project.getUrl();
113             
114             // before MPIR-229 this was model.getModules(), which could have uninherited/unresolved values
115             // @todo also include modules which are not part of reactor, e.g. caused by -pl 
116             List<MavenProject> modules = project.getCollectedProjects();
117             for ( MavenProject moduleProject : modules )
118             {
119                 Model moduleModel = moduleProject.getModel();
120 
121                 final String moduleName = moduleProject.getName();
122 
123                 final String moduleHref = getRelativeLink( baseURL, moduleProject.getUrl(), moduleProject.getArtifactId() );
124 
125                 tableRow( new String[] {linkedName( moduleName, moduleHref ), moduleModel.getDescription()} );
126             }
127 
128             endTable();
129 
130             endSection();
131         }
132 
133         // adapted from DefaultSiteTool#appendMenuItem
134         private String getRelativeLink( String baseUrl, String href, String defaultHref )
135         {
136             String selectedHref = href;
137 
138             if ( selectedHref == null )
139             {
140                 selectedHref = defaultHref;
141             }
142 
143             if ( baseUrl != null )
144             {
145                 selectedHref = siteTool.getRelativePath( selectedHref, baseUrl );
146             }
147 
148             if ( selectedHref.endsWith( "/" ) )
149             {
150                 selectedHref = selectedHref.concat( "index.html" );
151             }
152             else
153             {
154                 selectedHref = selectedHref.concat( "/index.html" );
155             }
156 
157             return selectedHref;
158         }
159 
160         private String linkedName( String name, String link )
161         {
162             return "{" + name + ", ./" + link + "}";
163         }
164     }
165 }