1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.dependency.fromDependencies;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.Collection;
27 import java.util.HashSet;
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.metadata.ArtifactMetadata;
33 import org.apache.maven.artifact.repository.ArtifactRepository;
34 import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
35 import org.apache.maven.artifact.repository.MavenArtifactRepository;
36 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
37 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
38 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
39 import org.apache.maven.artifact.repository.metadata.Snapshot;
40 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
41 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
42 import org.apache.maven.artifact.versioning.VersionRange;
43 import org.apache.maven.execution.MavenSession;
44 import org.apache.maven.plugin.LegacySupport;
45 import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
46 import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
47 import org.apache.maven.plugins.dependency.utils.DependencyUtil;
48 import org.apache.maven.plugins.dependency.utils.ResolverUtil;
49 import org.apache.maven.project.MavenProject;
50 import org.eclipse.aether.RepositorySystem;
51
52 public class TestCopyDependenciesMojo2 extends AbstractDependencyMojoTestCase {
53
54 private CopyDependenciesMojo mojo;
55
56 protected void setUp() throws Exception {
57
58 super.setUp("copy-dependencies", true);
59 MavenProject project = new DependencyProjectStub();
60 getContainer().addComponent(project, MavenProject.class.getName());
61
62 MavenSession session = newMavenSession(project);
63 getContainer().addComponent(session, MavenSession.class.getName());
64
65 RepositorySystem repositorySystem = lookup(RepositorySystem.class);
66 ResolverUtil resolverUtil = new ResolverUtil(repositorySystem, () -> session);
67 getContainer().addComponent(resolverUtil, ResolverUtil.class.getName());
68
69 File testPom = new File(getBasedir(), "target/test-classes/unit/copy-dependencies-test/plugin-config.xml");
70 mojo = (CopyDependenciesMojo) lookupMojo("copy-dependencies", testPom);
71 mojo.outputDirectory = new File(this.testDir, "outputDirectory");
72
73
74 assertNotNull(mojo);
75 assertNotNull(mojo.getProject());
76
77 Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
78 Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
79 artifacts.addAll(directArtifacts);
80
81 project.setArtifacts(artifacts);
82 project.setDependencyArtifacts(directArtifacts);
83 mojo.markersDirectory = new File(this.testDir, "markers");
84
85 LegacySupport legacySupport = lookup(LegacySupport.class);
86 legacySupport.setSession(session);
87 installLocalRepository(legacySupport);
88 }
89
90 public void testCopyDependenciesMojoIncludeCompileScope() throws Exception {
91 mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
92 mojo.getProject().setDependencyArtifacts(new HashSet<>());
93 mojo.includeScope = "compile";
94
95 mojo.execute();
96
97 ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.includeScope);
98
99 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
100 for (Artifact artifact : artifacts) {
101 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
102 File file = new File(mojo.outputDirectory, fileName);
103
104 assertEquals(saf.include(artifact), file.exists());
105 }
106 }
107
108 public void testCopyDependenciesMojoIncludeTestScope() throws Exception {
109 mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
110 mojo.getProject().setDependencyArtifacts(new HashSet<>());
111 mojo.includeScope = "test";
112
113 mojo.execute();
114
115 ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.includeScope);
116
117 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
118 for (Artifact artifact : artifacts) {
119 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
120 File file = new File(mojo.outputDirectory, fileName);
121
122 assertEquals(saf.include(artifact), file.exists());
123 }
124 }
125
126 public void testCopyDependenciesMojoIncludeRuntimeScope() throws Exception {
127 mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
128 mojo.getProject().setDependencyArtifacts(new HashSet<>());
129 mojo.includeScope = "runtime";
130
131 mojo.execute();
132
133 ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.includeScope);
134
135 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
136 for (Artifact artifact : artifacts) {
137 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
138 File file = new File(mojo.outputDirectory, fileName);
139
140 assertEquals(saf.include(artifact), file.exists());
141 }
142 }
143
144 public void testCopyDependenciesMojoIncludeprovidedScope() throws Exception {
145 mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
146 mojo.getProject().setDependencyArtifacts(new HashSet<>());
147 mojo.includeScope = "provided";
148
149 mojo.execute();
150
151 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
152 for (Artifact artifact : artifacts) {
153 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
154 File file = new File(mojo.outputDirectory, fileName);
155
156 assertEquals(Artifact.SCOPE_PROVIDED.equals(artifact.getScope()), file.exists());
157 }
158 }
159
160 public void testCopyDependenciesMojoIncludesystemScope() throws Exception {
161 mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
162 mojo.getProject().setDependencyArtifacts(new HashSet<>());
163 mojo.includeScope = "system";
164
165 mojo.execute();
166
167 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
168 for (Artifact artifact : artifacts) {
169 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
170 File file = new File(mojo.outputDirectory, fileName);
171
172 assertEquals(Artifact.SCOPE_SYSTEM.equals(artifact.getScope()), file.exists());
173 }
174 }
175
176 public void testSubPerArtifact() throws Exception {
177 mojo.useSubDirectoryPerArtifact = true;
178
179 mojo.execute();
180
181 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
182 for (Artifact artifact : artifacts) {
183 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
184 File folder = DependencyUtil.getFormattedOutputDirectory(
185 false, false, true, false, false, false, mojo.outputDirectory, artifact);
186 File file = new File(folder, fileName);
187 assertTrue(file.exists());
188 }
189 }
190
191 public void testSubPerArtifactAndType() throws Exception {
192 mojo.getProject().setArtifacts(stubFactory.getTypedArtifacts());
193 mojo.getProject().setDependencyArtifacts(new HashSet<>());
194 mojo.useSubDirectoryPerArtifact = true;
195 mojo.useSubDirectoryPerType = true;
196
197 mojo.execute();
198
199 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
200 for (Artifact artifact : artifacts) {
201 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
202 File folder = DependencyUtil.getFormattedOutputDirectory(
203 false, true, true, false, false, false, mojo.outputDirectory, artifact);
204 File file = new File(folder, fileName);
205 assertTrue(file.exists());
206 }
207 }
208
209 public void testSubPerArtifactAndScope() throws Exception {
210 mojo.getProject().setArtifacts(stubFactory.getTypedArtifacts());
211 mojo.getProject().setDependencyArtifacts(new HashSet<>());
212 mojo.useSubDirectoryPerArtifact = true;
213 mojo.useSubDirectoryPerScope = true;
214
215 mojo.execute();
216
217 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
218 for (Artifact artifact : artifacts) {
219 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
220 File folder = DependencyUtil.getFormattedOutputDirectory(
221 true, false, true, false, false, false, mojo.outputDirectory, artifact);
222 File file = new File(folder, fileName);
223 assertTrue(file.exists());
224 }
225 }
226
227 public void testRepositoryLayout() throws Exception {
228 String baseVersion = "2.0-SNAPSHOT";
229 String groupId = "testGroupId";
230 String artifactId = "expanded-snapshot";
231
232 Artifact expandedSnapshot =
233 createExpandedVersionArtifact(baseVersion, groupId, artifactId, "compile", "jar", null);
234
235 mojo.getProject().getArtifacts().add(expandedSnapshot);
236 mojo.getProject().getDependencyArtifacts().add(expandedSnapshot);
237
238 Artifact pomExpandedSnapshot =
239 createExpandedVersionArtifact(baseVersion, groupId, artifactId, "compile", "pom", null);
240 mojo.getProject().getArtifacts().add(pomExpandedSnapshot);
241 mojo.getProject().getDependencyArtifacts().add(pomExpandedSnapshot);
242
243 mojo.useRepositoryLayout = true;
244 mojo.execute();
245
246 ArtifactFactory artifactFactory = lookup(ArtifactFactory.class);
247
248 File outputDirectory = mojo.outputDirectory;
249 ArtifactRepository targetRepository = new MavenArtifactRepository(
250 "local",
251 outputDirectory.toURI().toURL().toExternalForm(),
252 new DefaultRepositoryLayout(),
253 new ArtifactRepositoryPolicy(),
254 new ArtifactRepositoryPolicy());
255
256 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
257 File baseDirectory = Paths.get(targetRepository.getBasedir()).toFile();
258 assertTrue(baseDirectory.isDirectory());
259
260 for (Artifact artifact : artifacts) {
261 assertArtifactExists(artifact, targetRepository);
262
263 if (!artifact.getBaseVersion().equals(artifact.getVersion())) {
264 Artifact baseArtifact = artifactFactory.createArtifact(
265 artifact.getGroupId(),
266 artifact.getArtifactId(),
267 artifact.getBaseVersion(),
268 artifact.getScope(),
269 artifact.getType());
270 assertArtifactExists(baseArtifact, targetRepository);
271 }
272 }
273 }
274
275 private Artifact createExpandedVersionArtifact(
276 String baseVersion, String groupId, String artifactId, String scope, String type, String classifier)
277 throws IOException {
278 Artifact expandedSnapshot = this.stubFactory.createArtifact(
279 groupId, artifactId, VersionRange.createFromVersion(baseVersion), scope, type, classifier, false);
280
281 Snapshot snapshot = new Snapshot();
282 snapshot.setTimestamp("20130710.122148");
283 snapshot.setBuildNumber(1);
284 RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata(expandedSnapshot, snapshot);
285 String newVersion = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber();
286 expandedSnapshot.setResolvedVersion(baseVersion.replace(Artifact.SNAPSHOT_VERSION, newVersion));
287 expandedSnapshot.addMetadata(metadata);
288 return expandedSnapshot;
289 }
290
291 private void assertArtifactExists(Artifact artifact, ArtifactRepository targetRepository) {
292
293 ArtifactRepositoryLayout layout = targetRepository.getLayout();
294 String pathOf = layout.pathOf(artifact);
295
296
297 pathOf = pathOf.replace("20130710.122148-1", "SNAPSHOT");
298
299 File file = new File(targetRepository.getBasedir(), pathOf);
300
301 Path targetPath = Paths.get(file.getParent());
302 assertTrue("Target path doesn't exist: " + targetPath, Files.isDirectory(targetPath));
303
304 assertTrue("File doesn't exist: " + file.getAbsolutePath(), file.exists());
305
306 Collection<ArtifactMetadata> metas = artifact.getMetadataList();
307 for (ArtifactMetadata meta : metas) {
308 File metaFile = new File(
309 targetRepository.getBasedir(), layout.pathOfLocalRepositoryMetadata(meta, targetRepository));
310 assertTrue(metaFile.exists());
311 }
312 }
313
314 public void testSubPerArtifactRemoveVersion() throws Exception {
315 mojo.useSubDirectoryPerArtifact = true;
316 mojo.stripVersion = true;
317
318 mojo.execute();
319
320 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
321 for (Artifact artifact : artifacts) {
322 String fileName = DependencyUtil.getFormattedFileName(artifact, true);
323 File folder = DependencyUtil.getFormattedOutputDirectory(
324 false, false, true, false, true, false, mojo.outputDirectory, artifact);
325 File file = new File(folder, fileName);
326 assertTrue(file.exists());
327 }
328 }
329
330 public void testSubPerArtifactAndTypeRemoveVersion() throws Exception {
331 mojo.getProject().setArtifacts(stubFactory.getTypedArtifacts());
332 mojo.getProject().setDependencyArtifacts(new HashSet<>());
333 mojo.useSubDirectoryPerArtifact = true;
334 mojo.useSubDirectoryPerType = true;
335 mojo.stripVersion = true;
336
337 mojo.execute();
338
339 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
340 for (Artifact artifact : artifacts) {
341 String fileName = DependencyUtil.getFormattedFileName(artifact, true);
342 File folder = DependencyUtil.getFormattedOutputDirectory(
343 false, true, true, false, true, false, mojo.outputDirectory, artifact);
344 File file = new File(folder, fileName);
345 assertTrue(file.exists());
346 }
347 }
348
349 public void testSubPerArtifactRemoveType() throws Exception {
350 mojo.useSubDirectoryPerArtifact = true;
351 mojo.stripType = true;
352
353 mojo.execute();
354
355 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
356 for (Artifact artifact : artifacts) {
357 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
358 File folder = DependencyUtil.getFormattedOutputDirectory(
359 false, false, true, false, false, true, mojo.outputDirectory, artifact);
360 File file = new File(folder, fileName);
361 assertTrue(file.exists());
362 }
363 }
364
365 public void testSubPerArtifactAndTypeRemoveType() throws Exception {
366 mojo.getProject().setArtifacts(stubFactory.getTypedArtifacts());
367 mojo.getProject().setDependencyArtifacts(new HashSet<>());
368 mojo.useSubDirectoryPerArtifact = true;
369 mojo.useSubDirectoryPerType = true;
370 mojo.stripType = true;
371
372 mojo.execute();
373
374 Set<Artifact> artifacts = mojo.getProject().getArtifacts();
375 for (Artifact artifact : artifacts) {
376 String fileName = DependencyUtil.getFormattedFileName(artifact, false);
377 File folder = DependencyUtil.getFormattedOutputDirectory(
378 false, true, true, false, false, true, mojo.outputDirectory, artifact);
379 File file = new File(folder, fileName);
380 assertTrue(file.exists());
381 }
382 }
383 }