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