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