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