1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.report.projectinfo;
20
21 import java.io.BufferedReader;
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.io.OutputStream;
29 import java.util.Locale;
30
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
33 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
34 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
35 import org.apache.maven.plugins.annotations.Component;
36 import org.apache.maven.plugins.annotations.Mojo;
37 import org.apache.maven.plugins.annotations.Parameter;
38 import org.apache.maven.plugins.annotations.ResolutionScope;
39 import org.apache.maven.project.DefaultProjectBuildingRequest;
40 import org.apache.maven.project.ProjectBuildingRequest;
41 import org.apache.maven.report.projectinfo.dependencies.Dependencies;
42 import org.apache.maven.report.projectinfo.dependencies.DependenciesReportConfiguration;
43 import org.apache.maven.report.projectinfo.dependencies.RepositoryUtils;
44 import org.apache.maven.report.projectinfo.dependencies.renderer.DependenciesRenderer;
45 import org.apache.maven.reporting.MavenReportException;
46 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
47 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
48 import org.apache.maven.shared.dependency.graph.DependencyNode;
49 import org.apache.maven.shared.jar.classes.JarClassesAnalysis;
50 import org.codehaus.plexus.util.IOUtil;
51 import org.codehaus.plexus.util.ReaderFactory;
52
53
54
55
56
57
58
59
60 @Mojo(name = "dependencies", requiresDependencyResolution = ResolutionScope.TEST)
61 public class DependenciesReport extends AbstractProjectInfoReport {
62
63
64
65 private static final String RESOURCES_DIR = "org/apache/maven/report/projectinfo/resources";
66
67
68
69
70
71
72
73
74
75
76 @Component(hint = "default")
77 private DependencyGraphBuilder dependencyGraphBuilder;
78
79
80
81
82
83
84 @Component
85 private JarClassesAnalysis classesAnalyzer;
86
87
88
89
90
91
92 @Component
93 private RepositoryMetadataManager repositoryMetadataManager;
94
95
96
97
98
99
100
101
102
103
104
105 @Parameter(property = "dependency.details.enabled", defaultValue = "true")
106 private boolean dependencyDetailsEnabled;
107
108
109
110
111
112 @Override
113 public boolean canGenerateReport() throws MavenReportException {
114 boolean result = super.canGenerateReport();
115 if (result && skipEmptyReport) {
116
117 DependencyNode dependencyNode = resolveProject();
118 Dependencies dependencies = new Dependencies(project, dependencyNode, classesAnalyzer);
119 result = dependencies.hasDependencies();
120 }
121
122 return result;
123 }
124
125 @Override
126 public void executeReport(Locale locale) {
127 try {
128 copyResources(getReportOutputDirectory());
129 } catch (IOException e) {
130 getLog().error("Cannot copy resources", e);
131 }
132
133 ProjectBuildingRequest buildingRequest =
134 new DefaultProjectBuildingRequest(getSession().getProjectBuildingRequest());
135 buildingRequest.setLocalRepository(localRepository);
136 buildingRequest.setRemoteRepositories(remoteRepositories);
137 buildingRequest.setPluginArtifactRepositories(pluginRepositories);
138
139 RepositoryUtils repoUtils = new RepositoryUtils(
140 getLog(),
141 projectBuilder,
142 repositorySystem,
143 resolver,
144 remoteRepositories,
145 pluginRepositories,
146 buildingRequest,
147 repositoryMetadataManager);
148
149 DependencyNode dependencyNode = resolveProject();
150
151 Dependencies dependencies = new Dependencies(project, dependencyNode, classesAnalyzer);
152
153 DependenciesReportConfiguration config = new DependenciesReportConfiguration(dependencyDetailsEnabled);
154
155 DependenciesRenderer r = new DependenciesRenderer(
156 getSink(),
157 locale,
158 getI18N(locale),
159 getLog(),
160 dependencies,
161 dependencyNode,
162 config,
163 repoUtils,
164 repositorySystem,
165 projectBuilder,
166 buildingRequest,
167 getLicenseMappings());
168 r.render();
169 }
170
171
172
173
174 public String getOutputName() {
175 return "dependencies";
176 }
177
178 @Override
179 protected String getI18Nsection() {
180 return "dependencies";
181 }
182
183
184
185
186
187
188
189
190 private DependencyNode resolveProject() {
191 try {
192 ArtifactFilter artifactFilter = new ScopeArtifactFilter(Artifact.SCOPE_TEST);
193 ProjectBuildingRequest buildingRequest =
194 new DefaultProjectBuildingRequest(getSession().getProjectBuildingRequest());
195 buildingRequest.setProject(project);
196 return dependencyGraphBuilder.buildDependencyGraph(buildingRequest, artifactFilter);
197 } catch (DependencyGraphBuilderException e) {
198 getLog().error("Unable to build dependency tree.", e);
199 return null;
200 }
201 }
202
203
204
205
206
207 private void copyResources(File outputDirectory) throws IOException {
208 InputStream resourceList = null;
209 InputStream in = null;
210 BufferedReader reader = null;
211 OutputStream out = null;
212 try {
213 resourceList = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/resources.txt");
214
215 if (resourceList != null) {
216 reader = new LineNumberReader(new InputStreamReader(resourceList, ReaderFactory.US_ASCII));
217
218 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
219 in = getClass().getClassLoader().getResourceAsStream(RESOURCES_DIR + "/" + line);
220
221 if (in == null) {
222 throw new IOException("The resource " + line + " doesn't exist.");
223 }
224
225 File outputFile = new File(outputDirectory, line);
226
227 if (!outputFile.getParentFile().exists()) {
228 outputFile.getParentFile().mkdirs();
229 }
230
231 out = new FileOutputStream(outputFile);
232 IOUtil.copy(in, out);
233 out.close();
234 out = null;
235 in.close();
236 in = null;
237 }
238
239 reader.close();
240 reader = null;
241 }
242 } finally {
243 IOUtil.close(out);
244 IOUtil.close(reader);
245 IOUtil.close(in);
246 IOUtil.close(resourceList);
247 }
248 }
249 }