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.artifact.Artifact;
23  import org.apache.maven.artifact.factory.ArtifactFactory;
24  import org.apache.maven.artifact.repository.ArtifactRepository;
25  import org.apache.maven.artifact.versioning.VersionRange;
26  import org.apache.maven.doxia.sink.Sink;
27  import org.apache.maven.plugin.logging.Log;
28  import org.apache.maven.plugins.annotations.Component;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.ResolutionScope;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.project.MavenProjectBuilder;
33  import org.apache.maven.project.ProjectBuildingException;
34  import org.codehaus.plexus.i18n.I18N;
35  import org.codehaus.plexus.util.StringUtils;
36  
37  import java.util.ArrayList;
38  import java.util.Collections;
39  import java.util.Comparator;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.Set;
43  
44  /**
45   * Generates the Project Plugins report.
46   *
47   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
48   * @version $Id: PluginsReport.java 1367255 2012-07-30 20:01:21Z hboutemy $
49   * @since 2.1
50   */
51  @Mojo( name = "plugins", requiresDependencyResolution = ResolutionScope.TEST )
52  public class PluginsReport
53      extends AbstractProjectInfoReport
54  {
55      // ----------------------------------------------------------------------
56      // Mojo components
57      // ----------------------------------------------------------------------
58  
59      /**
60       * Maven Project Builder component.
61       */
62      @Component
63      private MavenProjectBuilder mavenProjectBuilder;
64  
65      /**
66       * Maven Artifact Factory component.
67       */
68      @Component
69      private ArtifactFactory artifactFactory;
70  
71      // ----------------------------------------------------------------------
72      // Public methods
73      // ----------------------------------------------------------------------
74  
75      @Override
76      public void executeReport( Locale locale )
77      {
78          @SuppressWarnings( "unchecked" )
79          PluginsRenderer r =
80              new PluginsRenderer( getLog(), getSink(), locale, getI18N( locale ), project.getPluginArtifacts(),
81                                   project.getReportArtifacts(), project, mavenProjectBuilder, artifactFactory,
82                                   localRepository );
83          r.render();
84      }
85  
86      /** {@inheritDoc} */
87      public String getOutputName()
88      {
89          return "plugins";
90      }
91  
92      @Override
93      protected String getI18Nsection()
94      {
95          return "plugins";
96      }
97  
98      @Override
99      public boolean canGenerateReport()
100     {
101         return ( project.getPluginArtifacts() != null && !project.getPluginArtifacts().isEmpty() )
102             || ( project.getReportArtifacts() != null && !project.getReportArtifacts().isEmpty() );
103     }
104 
105     // ----------------------------------------------------------------------
106     // Private
107     // ----------------------------------------------------------------------
108 
109     /**
110      * Internal renderer class
111      */
112     protected static class PluginsRenderer
113         extends AbstractProjectInfoRenderer
114     {
115         private final Log log;
116 
117         private final List<Artifact> plugins;
118 
119         private final List<Artifact> reports;
120 
121         private final MavenProject project;
122 
123         private final MavenProjectBuilder mavenProjectBuilder;
124 
125         private final ArtifactFactory artifactFactory;
126 
127         private final ArtifactRepository localRepository;
128 
129         /**
130          * @param log
131          * @param sink
132          * @param locale
133          * @param i18n
134          * @param plugins
135          * @param reports
136          * @param project
137          * @param mavenProjectBuilder
138          * @param artifactFactory
139          * @param localRepository
140          */
141         public PluginsRenderer( Log log, Sink sink, Locale locale, I18N i18n, Set<Artifact> plugins,
142                                 Set<Artifact> reports, MavenProject project, MavenProjectBuilder mavenProjectBuilder,
143                                 ArtifactFactory artifactFactory, ArtifactRepository localRepository )
144         {
145             super( sink, i18n, locale );
146 
147             this.log = log;
148 
149             this.plugins = new ArrayList<Artifact>( plugins );
150 
151             this.reports = new ArrayList<Artifact>( reports );
152 
153             this.project = project;
154 
155             this.mavenProjectBuilder = mavenProjectBuilder;
156 
157             this.artifactFactory = artifactFactory;
158 
159             this.localRepository = localRepository;
160         }
161 
162         @Override
163         protected String getI18Nsection()
164         {
165             return "plugins";
166         }
167 
168         @Override
169         public void renderBody()
170         {
171             // === Section: Project Plugins.
172             renderSectionPlugins( true );
173 
174             // === Section: Project Reports.
175             renderSectionPlugins( false );
176         }
177 
178         /**
179          * @param isPlugins <code>true</code> to use <code>plugins</code> variable, <code>false</code> to use
180          * <code>reports</code> variable.
181          */
182         private void renderSectionPlugins( boolean isPlugins )
183         {
184             List<Artifact> list = ( isPlugins ? plugins : reports );
185             String[] tableHeader = getPluginTableHeader();
186 
187             startSection( ( isPlugins ? getI18nString( "title" )
188                                      : getI18nString( "report.title" ) ) );
189 
190             if ( list == null || list.isEmpty() )
191             {
192 
193                 paragraph(  ( isPlugins ? getI18nString( "nolist" )
194                                         : getI18nString( "report.nolist" ) ) );
195 
196                 endSection();
197 
198                 return;
199             }
200 
201             Collections.sort( list, getArtifactComparator() );
202 
203             startTable();
204             tableHeader( tableHeader );
205 
206             for ( Artifact artifact : list )
207             {
208                 VersionRange versionRange;
209                 if ( StringUtils.isEmpty( artifact.getVersion() ) )
210                 {
211                     versionRange = VersionRange.createFromVersion( Artifact.RELEASE_VERSION );
212                 }
213                 else
214                 {
215                     versionRange = VersionRange.createFromVersion( artifact.getVersion() );
216                 }
217 
218                 Artifact pluginArtifact = artifactFactory.createParentArtifact( artifact.getGroupId(), artifact
219                     .getArtifactId(), versionRange.toString() );
220                 @SuppressWarnings( "unchecked" )
221                 List<ArtifactRepository> artifactRepositories = project.getPluginArtifactRepositories();
222                 if ( artifactRepositories == null )
223                 {
224                     artifactRepositories = new ArrayList<ArtifactRepository>();
225                 }
226                 try
227                 {
228                     MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( pluginArtifact,
229                                                                                           artifactRepositories,
230                                                                                           localRepository );
231                     tableRow( getPluginRow( pluginProject.getGroupId(), pluginProject.getArtifactId(), pluginProject
232                                             .getVersion(), pluginProject.getUrl() ) );
233                 }
234                 catch ( ProjectBuildingException e )
235                 {
236                     log.info( "Could not build project for: " + artifact.getArtifactId() + ":" + e.getMessage(), e );
237                     tableRow( getPluginRow( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
238                                             null ) );
239                 }
240 
241             }
242             endTable();
243 
244             endSection();
245         }
246 
247         // ----------------------------------------------------------------------
248         // Private methods
249         // ----------------------------------------------------------------------
250 
251         private String[] getPluginTableHeader()
252         {
253             // reused key...
254             String groupId = getI18nString( "dependencyManagement", "column.groupId" );
255             String artifactId = getI18nString( "dependencyManagement", "column.artifactId" );
256             String version = getI18nString( "dependencyManagement", "column.version" );
257             return new String[] { groupId, artifactId, version };
258         }
259 
260         private String[] getPluginRow( String groupId, String artifactId, String version, String link )
261         {
262             artifactId = ProjectInfoReportUtils.getArtifactIdCell( artifactId, link );
263             return new String[] { groupId, artifactId, version };
264         }
265 
266         private Comparator<Artifact> getArtifactComparator()
267         {
268             return new Comparator<Artifact>()
269             {
270                 /** {@inheritDoc} */
271                 public int compare( Artifact a1, Artifact a2 )
272                 {
273                     int result = a1.getGroupId().compareTo( a2.getGroupId() );
274                     if ( result == 0 )
275                     {
276                         result = a1.getArtifactId().compareTo( a2.getArtifactId() );
277                     }
278                     return result;
279                 }
280             };
281         }
282     }
283 }