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