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.fromConfiguration;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
29 import org.apache.maven.execution.MavenSession;
30 import org.apache.maven.plugin.MojoExecutionException;
31 import org.apache.maven.plugin.MojoFailureException;
32 import org.apache.maven.plugins.annotations.LifecyclePhase;
33 import org.apache.maven.plugins.annotations.Mojo;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.plugins.dependency.utils.CopyUtil;
36 import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
37 import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
38 import org.apache.maven.project.MavenProject;
39 import org.eclipse.aether.RepositorySystem;
40 import org.sonatype.plexus.build.incremental.BuildContext;
41
42
43
44
45
46
47
48 @Mojo(name = "copy", defaultPhase = LifecyclePhase.PROCESS_SOURCES, requiresProject = false, threadSafe = true)
49 public class CopyMojo extends AbstractFromConfigurationMojo {
50
51 private final CopyUtil copyUtil;
52
53
54
55
56 @Parameter(property = "mdep.stripVersion", defaultValue = "false")
57 private boolean stripVersion = false;
58
59
60
61
62 @Parameter(property = "mdep.stripClassifier", defaultValue = "false")
63 private boolean stripClassifier = false;
64
65
66
67
68
69
70 @Parameter(property = "mdep.prependGroupId", defaultValue = "false")
71 private boolean prependGroupId = false;
72
73
74
75
76
77
78 @Parameter(property = "mdep.useBaseVersion", defaultValue = "false")
79 private boolean useBaseVersion = false;
80
81
82
83
84
85 @SuppressWarnings("unused")
86 @Parameter(property = "artifact")
87 private String artifact;
88
89 @Inject
90 public CopyMojo(
91 MavenSession session,
92 BuildContext buildContext,
93 MavenProject project,
94 ArtifactHandlerManager artifactHandlerManager,
95 CopyUtil copyUtil,
96 RepositorySystem repositorySystem) {
97 super(session, buildContext, project, artifactHandlerManager, repositorySystem);
98 this.copyUtil = copyUtil;
99 }
100
101
102
103
104
105
106
107
108
109
110 @Override
111 protected void doExecute() throws MojoExecutionException, MojoFailureException {
112 verifyRequirements();
113
114 List<ArtifactItem> theArtifactItems = getProcessedArtifactItems(
115 new ProcessArtifactItemsRequest(stripVersion, prependGroupId, useBaseVersion, stripClassifier));
116
117 for (ArtifactItem artifactItem : theArtifactItems) {
118 if (artifactItem.isNeedsProcessing()) {
119 copyArtifact(artifactItem);
120 } else {
121 getLog().info(artifactItem + " already exists in " + artifactItem.getOutputDirectory());
122 }
123 }
124 }
125
126
127
128
129
130
131
132
133 protected void copyArtifact(ArtifactItem artifactItem) throws MojoExecutionException {
134 File destFile = new File(artifactItem.getOutputDirectory(), artifactItem.getDestFileName());
135 if (destFile.exists()) {
136 getLog().warn("Overwriting " + destFile);
137 }
138 try {
139 copyUtil.copyArtifactFile(artifactItem.getArtifact(), destFile);
140 } catch (IOException e) {
141 throw new MojoExecutionException(
142 "Failed to copy artifact '" + artifactItem.getArtifact() + "' ("
143 + artifactItem.getArtifact().getFile() + ") to " + destFile,
144 e);
145 }
146 }
147
148 @Override
149 protected ArtifactItemFilter getMarkedArtifactFilter(ArtifactItem item) {
150 return new DestFileFilter(
151 this.isOverWriteReleases(),
152 this.isOverWriteSnapshots(),
153 this.isOverWriteIfNewer(),
154 false,
155 false,
156 false,
157 false,
158 this.stripVersion,
159 prependGroupId,
160 useBaseVersion,
161 item.getOutputDirectory());
162 }
163
164
165
166
167 public boolean isStripVersion() {
168 return this.stripVersion;
169 }
170
171
172
173
174 public void setStripVersion(boolean stripVersion) {
175 this.stripVersion = stripVersion;
176 }
177
178
179
180
181 public boolean isStripClassifier() {
182 return this.stripClassifier;
183 }
184
185
186
187
188 public void setStripClassifier(boolean stripClassifier) {
189 this.stripClassifier = stripClassifier;
190 }
191
192
193
194
195 public void setUseBaseVersion(boolean useBaseVersion) {
196 this.useBaseVersion = useBaseVersion;
197 }
198 }