1 | |
package org.apache.maven.report.projectinfo; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileOutputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.io.LineNumberReader; |
28 | |
import java.util.List; |
29 | |
import java.util.Locale; |
30 | |
|
31 | |
import org.apache.maven.artifact.Artifact; |
32 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
33 | |
import org.apache.maven.artifact.manager.WagonManager; |
34 | |
import org.apache.maven.artifact.metadata.ArtifactMetadataSource; |
35 | |
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager; |
36 | |
import org.apache.maven.artifact.resolver.ArtifactCollector; |
37 | |
import org.apache.maven.artifact.resolver.filter.ArtifactFilter; |
38 | |
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; |
39 | |
import org.apache.maven.project.MavenProjectBuilder; |
40 | |
import org.apache.maven.report.projectinfo.dependencies.Dependencies; |
41 | |
import org.apache.maven.report.projectinfo.dependencies.DependenciesReportConfiguration; |
42 | |
import org.apache.maven.report.projectinfo.dependencies.RepositoryUtils; |
43 | |
import org.apache.maven.report.projectinfo.dependencies.renderer.DependenciesRenderer; |
44 | |
import org.apache.maven.settings.Settings; |
45 | |
import org.apache.maven.shared.dependency.tree.DependencyNode; |
46 | |
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder; |
47 | |
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException; |
48 | |
import org.apache.maven.shared.jar.classes.JarClassesAnalysis; |
49 | |
import org.codehaus.plexus.PlexusConstants; |
50 | |
import org.codehaus.plexus.PlexusContainer; |
51 | |
import org.codehaus.plexus.context.Context; |
52 | |
import org.codehaus.plexus.context.ContextException; |
53 | |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable; |
54 | |
import org.codehaus.plexus.util.IOUtil; |
55 | |
import org.codehaus.plexus.util.ReaderFactory; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 1 | public class DependenciesReport |
68 | |
extends AbstractProjectInfoReport |
69 | |
implements Contextualizable |
70 | |
{ |
71 | |
|
72 | |
private static final String RESOURCES_DIR = "org/apache/maven/report/projectinfo/resources"; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
private MavenProjectBuilder mavenProjectBuilder; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
protected ArtifactMetadataSource artifactMetadataSource; |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private ArtifactCollector collector; |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private WagonManager wagonManager; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
private DependencyTreeBuilder dependencyTreeBuilder; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
private JarClassesAnalysis classesAnalyzer; |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
private RepositoryMetadataManager repositoryMetadataManager; |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
private ArtifactFactory artifactFactory; |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
private Settings settings; |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
private List remoteRepositories; |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
private boolean dependencyDetailsEnabled; |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
private boolean dependencyLocationsEnabled; |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
private PlexusContainer container; |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public String getName( Locale locale ) |
194 | |
{ |
195 | 1 | return i18n.getString( "project-info-report", locale, "report.dependencies.name" ); |
196 | |
} |
197 | |
|
198 | |
|
199 | |
public String getDescription( Locale locale ) |
200 | |
{ |
201 | 0 | return i18n.getString( "project-info-report", locale, "report.dependencies.description" ); |
202 | |
} |
203 | |
|
204 | |
|
205 | |
public void executeReport( Locale locale ) |
206 | |
{ |
207 | 1 | if ( settings.isOffline() && dependencyLocationsEnabled ) |
208 | |
{ |
209 | 0 | getLog().warn( "The parameter 'dependencyLocationsEnabled' is ignored in offline mode." ); |
210 | 0 | dependencyLocationsEnabled = false; |
211 | |
} |
212 | |
|
213 | |
try |
214 | |
{ |
215 | 1 | copyResources( new File( getOutputDirectory() ) ); |
216 | |
} |
217 | 0 | catch ( IOException e ) |
218 | |
{ |
219 | 0 | getLog().error( "Cannot copy ressources", e ); |
220 | 1 | } |
221 | |
|
222 | 1 | RepositoryUtils repoUtils = |
223 | |
new RepositoryUtils( getLog(), container.getLoggerManager(), wagonManager, settings, |
224 | |
mavenProjectBuilder, factory, resolver, project.getRemoteArtifactRepositories(), |
225 | |
project.getPluginArtifactRepositories(), localRepository, |
226 | |
repositoryMetadataManager ); |
227 | |
|
228 | 1 | DependencyNode dependencyTreeNode = resolveProject(); |
229 | |
|
230 | 1 | Dependencies dependencies = new Dependencies( project, dependencyTreeNode, classesAnalyzer ); |
231 | |
|
232 | 1 | DependenciesReportConfiguration config = |
233 | |
new DependenciesReportConfiguration( dependencyDetailsEnabled, dependencyLocationsEnabled ); |
234 | |
|
235 | 1 | DependenciesRenderer r = |
236 | |
new DependenciesRenderer( getSink(), locale, i18n, getLog(), settings, dependencies, |
237 | |
dependencyTreeNode, config, repoUtils, artifactFactory, mavenProjectBuilder, |
238 | |
remoteRepositories, localRepository ); |
239 | 1 | r.render(); |
240 | 1 | } |
241 | |
|
242 | |
|
243 | |
public String getOutputName() |
244 | |
{ |
245 | 3 | return "dependencies"; |
246 | |
} |
247 | |
|
248 | |
|
249 | |
public void contextualize( Context context ) |
250 | |
throws ContextException |
251 | |
{ |
252 | 1 | this.container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY ); |
253 | 1 | } |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
private DependencyNode resolveProject() |
263 | |
{ |
264 | |
try |
265 | |
{ |
266 | 1 | ArtifactFilter artifactFilter = new ScopeArtifactFilter( Artifact.SCOPE_TEST ); |
267 | 1 | return dependencyTreeBuilder.buildDependencyTree( project, localRepository, factory, |
268 | |
artifactMetadataSource, artifactFilter, collector ); |
269 | |
} |
270 | 0 | catch ( DependencyTreeBuilderException e ) |
271 | |
{ |
272 | 0 | getLog().error( "Unable to build dependency tree.", e ); |
273 | 0 | return null; |
274 | |
} |
275 | |
} |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
private void copyResources( File outputDirectory ) |
282 | |
throws IOException |
283 | |
{ |
284 | 1 | InputStream resourceList = |
285 | |
getClass().getClassLoader().getResourceAsStream( RESOURCES_DIR + "/resources.txt" ); |
286 | |
|
287 | 1 | if ( resourceList != null ) |
288 | |
{ |
289 | 1 | LineNumberReader reader = |
290 | |
new LineNumberReader( new InputStreamReader( resourceList, ReaderFactory.US_ASCII ) ); |
291 | |
|
292 | 1 | String line = reader.readLine(); |
293 | |
|
294 | 2 | while ( line != null ) |
295 | |
{ |
296 | 1 | InputStream is = getClass().getClassLoader().getResourceAsStream( RESOURCES_DIR + "/" + line ); |
297 | |
|
298 | 1 | if ( is == null ) |
299 | |
{ |
300 | 0 | throw new IOException( "The resource " + line + " doesn't exist." ); |
301 | |
} |
302 | |
|
303 | 1 | File outputFile = new File( outputDirectory, line ); |
304 | |
|
305 | 1 | if ( !outputFile.getParentFile().exists() ) |
306 | |
{ |
307 | 0 | outputFile.getParentFile().mkdirs(); |
308 | |
} |
309 | |
|
310 | 1 | FileOutputStream w = new FileOutputStream( outputFile ); |
311 | |
|
312 | 1 | IOUtil.copy( is, w ); |
313 | |
|
314 | 1 | IOUtil.close( is ); |
315 | |
|
316 | 1 | IOUtil.close( w ); |
317 | |
|
318 | 1 | line = reader.readLine(); |
319 | 1 | } |
320 | |
} |
321 | 1 | } |
322 | |
} |