23
23
import java .util .Properties ;
24
24
import java .util .Set ;
25
25
26
+ import org .apache .commons .io .FilenameUtils ;
26
27
import org .apache .maven .artifact .Artifact ;
27
28
import org .apache .maven .model .Dependency ;
28
29
import org .apache .maven .plugin .MojoExecutionException ;
54
55
* @author Phillip Webb
55
56
* @author Dave Syer
56
57
* @author Stephane Nicoll
58
+ * @author Björn Lindström
57
59
*/
58
60
@ Mojo (name = "repackage" , defaultPhase = LifecyclePhase .PACKAGE , requiresProject = true , threadSafe = true , requiresDependencyResolution = ResolutionScope .COMPILE_PLUS_RUNTIME , requiresDependencyCollection = ResolutionScope .COMPILE_PLUS_RUNTIME )
59
61
public class RepackageMojo extends AbstractDependencyFilterMojo {
@@ -96,7 +98,8 @@ public class RepackageMojo extends AbstractDependencyFilterMojo {
96
98
/**
97
99
* Classifier to add to the artifact generated. If given, the artifact will be
98
100
* attached with that classifier and the main artifact will be deployed as the main
99
- * artifact. If this is not given (default), it will replace the main artifact and
101
+ * artifact. If an artifact with the classifier already exists, it will be used as source.
102
+ * If a classifier is not given (default), it will replace the main artifact and
100
103
* only the repackaged artifact will be deployed. Attaching the artifact allows to
101
104
* deploy it alongside to the original one, see <a href=
102
105
* "http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html"
@@ -205,7 +208,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
205
208
}
206
209
207
210
private void repackage () throws MojoExecutionException {
208
- File source = this . project . getArtifact (). getFile ();
211
+ File source = getSourceFile ();
209
212
File target = getTargetFile ();
210
213
Repackager repackager = getRepackager (source );
211
214
Set <Artifact > artifacts = filterDependencies (this .project .getArtifacts (),
@@ -222,18 +225,35 @@ private void repackage() throws MojoExecutionException {
222
225
updateArtifact (source , target , repackager .getBackupFile ());
223
226
}
224
227
225
- private File getTargetFile () {
226
- String classifier = (this .classifier == null ? "" : this .classifier .trim ());
227
- if (classifier .length () > 0 && !classifier .startsWith ("-" )) {
228
- classifier = "-" + classifier ;
228
+ private File getSourceFile () {
229
+ File source = this .project .getArtifact ().getFile ();
230
+ if (this .classifier != null ) {
231
+ File sourceWithClassifier = new File (source .getParent (),
232
+ FilenameUtils .getBaseName (source .getName ()) + getClassifier () + "."
233
+ + FilenameUtils .getExtension (source .getName ()));
234
+ if (sourceWithClassifier .exists ()) {
235
+ source = sourceWithClassifier ;
236
+ }
229
237
}
238
+ return source ;
239
+ }
240
+
241
+ private File getTargetFile () {
230
242
if (!this .outputDirectory .exists ()) {
231
243
this .outputDirectory .mkdirs ();
232
244
}
233
- return new File (this .outputDirectory , this .finalName + classifier + "."
245
+ return new File (this .outputDirectory , this .finalName + getClassifier () + "."
234
246
+ this .project .getArtifact ().getArtifactHandler ().getExtension ());
235
247
}
236
248
249
+ private String getClassifier () {
250
+ String classifier = (this .classifier == null ? "" : this .classifier .trim ());
251
+ if (classifier .length () > 0 && !classifier .startsWith ("-" )) {
252
+ classifier = "-" + classifier ;
253
+ }
254
+ return classifier ;
255
+ }
256
+
237
257
private Repackager getRepackager (File source ) {
238
258
Repackager repackager = new Repackager (source , this .layoutFactory );
239
259
repackager .addMainClassTimeoutWarningListener (
0 commit comments