1 package org.apache.maven.plugins.dependency.testUtils;
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.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.ArtifactUtils;
30 import org.apache.maven.artifact.versioning.VersionRange;
31 import org.apache.maven.plugin.testing.ArtifactStubFactory;
32 import org.apache.maven.plugins.dependency.fromConfiguration.ArtifactItem;
33 import org.codehaus.plexus.archiver.Archiver;
34 import org.codehaus.plexus.archiver.ArchiverException;
35 import org.codehaus.plexus.archiver.manager.ArchiverManager;
36 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
37 import org.codehaus.plexus.archiver.war.WarArchiver;
38
39 public class DependencyArtifactStubFactory
40 extends ArtifactStubFactory
41 {
42 private boolean flattenedPath = true;
43 private ArchiverManager archiverManager;
44
45 public DependencyArtifactStubFactory( File theWorkingDir, boolean theCreateFiles, boolean flattenedPath )
46 {
47 this( theWorkingDir, theCreateFiles );
48 this.flattenedPath = flattenedPath;
49 }
50
51
52
53
54
55 public DependencyArtifactStubFactory( File theWorkingDir, boolean theCreateFiles )
56 {
57 super( theWorkingDir, theCreateFiles );
58 }
59
60 public ArtifactItem getArtifactItem( Artifact artifact )
61 {
62 ArtifactItem item = new ArtifactItem( artifact );
63 return item;
64 }
65
66 public List<ArtifactItem> getArtifactItems( Collection<Artifact> artifacts )
67 {
68 List<ArtifactItem> list = new ArrayList<>();
69 for ( Artifact artifact : artifacts )
70 {
71 list.add( getArtifactItem( artifact ) );
72 }
73 return list;
74 }
75
76 @Override
77 public Artifact createArtifact( String groupId, String artifactId, VersionRange versionRange, String scope,
78 String type, String classifier, boolean optional )
79 throws IOException
80 {
81 File workingDir = getWorkingDir();
82
83 if ( !flattenedPath )
84 {
85 StringBuilder path = new StringBuilder( 128 );
86
87 path.append( groupId.replace( '.', '/' ) ).append( '/' );
88
89 path.append( artifactId ).append( '/' );
90
91 path.append( ArtifactUtils.toSnapshotVersion( versionRange.getRecommendedVersion().toString() ) );
92
93
94 setWorkingDir( new File( workingDir, path.toString() ) );
95 }
96
97 Artifact artifact =
98 super.createArtifact( groupId, artifactId, versionRange, scope, type, classifier, optional );
99
100 setWorkingDir( workingDir );
101
102 return artifact;
103 }
104
105
106 @Override
107 public void setUnpackableFile( ArchiverManager archiverManager )
108 {
109
110 this.archiverManager = archiverManager;
111 super.setUnpackableFile( archiverManager );
112 }
113
114
115
116
117
118
119 @Override
120 public void createUnpackableFile( Artifact artifact, File destFile )
121 throws NoSuchArchiverException, ArchiverException, IOException
122 {
123 Archiver archiver = archiverManager.getArchiver( destFile );
124
125 archiver.setDestFile( destFile );
126 archiver.addFile( getSrcFile(), getUnpackableFileName( artifact ) );
127
128 if ( archiver instanceof WarArchiver )
129 {
130 WarArchiver war = (WarArchiver) archiver;
131
132
133 war.setIgnoreWebxml( false );
134 }
135 archiver.createArchive();
136 }
137 }