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