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