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