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"
@@ -206,7 +209,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
206
209
}
207
210
208
211
private void repackage () throws MojoExecutionException {
209
- File source = this . project . getArtifact (). getFile ();
212
+ File source = getSourceFile ();
210
213
File target = getTargetFile ();
211
214
Repackager repackager = getRepackager (source );
212
215
Set <Artifact > artifacts = filterDependencies (this .project .getArtifacts (),
@@ -223,18 +226,35 @@ private void repackage() throws MojoExecutionException {
223
226
updateArtifact (source , target , repackager .getBackupFile ());
224
227
}
225
228
226
- private File getTargetFile () {
227
- String classifier = (this .classifier == null ? "" : this .classifier .trim ());
228
- if (classifier .length () > 0 && !classifier .startsWith ("-" )) {
229
- classifier = "-" + classifier ;
229
+ private File getSourceFile () {
230
+ File source = this .project .getArtifact ().getFile ();
231
+ if (this .classifier != null ) {
232
+ File sourceWithClassifier = new File (source .getParent (),
233
+ FilenameUtils .getBaseName (source .getName ()) + getClassifier () + "."
234
+ + FilenameUtils .getExtension (source .getName ()));
235
+ if (sourceWithClassifier .exists ()) {
236
+ source = sourceWithClassifier ;
237
+ }
230
238
}
239
+ return source ;
240
+ }
241
+
242
+ private File getTargetFile () {
231
243
if (!this .outputDirectory .exists ()) {
232
244
this .outputDirectory .mkdirs ();
233
245
}
234
- return new File (this .outputDirectory , this .finalName + classifier + "."
246
+ return new File (this .outputDirectory , this .finalName + getClassifier () + "."
235
247
+ this .project .getArtifact ().getArtifactHandler ().getExtension ());
236
248
}
237
249
250
+ private String getClassifier () {
251
+ String classifier = (this .classifier == null ? "" : this .classifier .trim ());
252
+ if (classifier .length () > 0 && !classifier .startsWith ("-" )) {
253
+ classifier = "-" + classifier ;
254
+ }
255
+ return classifier ;
256
+ }
257
+
238
258
private Repackager getRepackager (File source ) {
239
259
Repackager repackager = new Repackager (source , this .layoutFactory );
240
260
repackager .addMainClassTimeoutWarningListener (
0 commit comments