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