26
26
import java .nio .file .Files ;
27
27
import java .nio .file .Path ;
28
28
import java .nio .file .StandardCopyOption ;
29
+ import java .util .Objects ;
29
30
import java .util .function .Predicate ;
30
31
import java .util .jar .JarEntry ;
31
32
import java .util .jar .JarFile ;
43
44
import org .apache .maven .plugins .annotations .Mojo ;
44
45
import org .apache .maven .plugins .annotations .Parameter ;
45
46
import org .codehaus .plexus .util .FileUtils ;
47
+ import org .codehaus .plexus .util .StringUtils ;
46
48
import org .codehaus .plexus .util .xml .pull .XmlPullParserException ;
47
49
import org .eclipse .aether .DefaultRepositoryCache ;
48
50
import org .eclipse .aether .DefaultRepositorySystemSession ;
56
58
import org .eclipse .aether .repository .LocalRepository ;
57
59
import org .eclipse .aether .repository .LocalRepositoryManager ;
58
60
import org .eclipse .aether .util .artifact .SubArtifact ;
61
+ import org .slf4j .Logger ;
62
+ import org .slf4j .LoggerFactory ;
59
63
60
64
import static java .util .Objects .isNull ;
61
- import static java .util .Objects .nonNull ;
62
65
63
66
/**
64
67
* Installs a file in the local repository.
68
71
@ Mojo (name = "install-file" , requiresProject = false , aggregator = true , threadSafe = true )
69
72
public class InstallFileMojo extends AbstractMojo {
70
73
private static final String LS = System .lineSeparator ();
74
+ private final Logger log = LoggerFactory .getLogger (getClass ());
71
75
72
76
@ Component
73
77
private RepositorySystem repositorySystem ;
@@ -112,6 +116,15 @@ public class InstallFileMojo extends AbstractMojo {
112
116
@ Parameter (property = "classifier" )
113
117
private String classifier ;
114
118
119
+ /**
120
+ * Extension of the artifact to be installed. If set, will override plugin own logic to detect extension. If not set,
121
+ * as Maven expected, packaging determines the artifact extension.
122
+ *
123
+ * @since 3.1.3
124
+ */
125
+ @ Parameter (property = "extension" )
126
+ private String extension ;
127
+
115
128
/**
116
129
* The file to be installed in the local repository.
117
130
*/
@@ -169,7 +182,7 @@ public class InstallFileMojo extends AbstractMojo {
169
182
public void execute () throws MojoExecutionException , MojoFailureException {
170
183
if (!file .exists ()) {
171
184
String message = "The specified file '" + file .getPath () + "' does not exist" ;
172
- getLog () .error (message );
185
+ log .error (message );
173
186
throw new MojoFailureException (message );
174
187
}
175
188
@@ -189,8 +202,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
189
202
newSession , new LocalRepository (localRepositoryPath , contentType ));
190
203
newSession .setLocalRepositoryManager (localRepositoryManager );
191
204
repositorySystemSession = newSession ;
192
- getLog () .debug ("localRepoPath: "
193
- + localRepositoryManager .getRepository ().getBasedir ());
205
+ log .debug (
206
+ "localRepoPath: {}" , localRepositoryManager .getRepository ().getBasedir ());
194
207
}
195
208
196
209
File temporaryPom = null ;
@@ -199,7 +212,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
199
212
temporaryPom = readingPomFromJarFile ();
200
213
if (!Boolean .TRUE .equals (generatePom )) {
201
214
pomFile = temporaryPom ;
202
- getLog () .debug ("Using JAR embedded POM as pomFile" );
215
+ log .debug ("Using JAR embedded POM as pomFile" );
203
216
}
204
217
} else {
205
218
processModel (readModel (pomFile ));
@@ -216,16 +229,29 @@ public void execute() throws MojoExecutionException, MojoFailureException {
216
229
217
230
InstallRequest installRequest = new InstallRequest ();
218
231
219
- boolean isFilePom = isNull (classifier ) && IS_POM_PACKAGING .test (packaging );
220
- if (!isFilePom ) {
232
+ String mainArtifactExtension ;
233
+ if (classifier == null && "pom" .equals (packaging )) {
234
+ mainArtifactExtension = "pom" ;
235
+ } else {
221
236
ArtifactType artifactType =
222
- repositorySystemSession .getArtifactTypeRegistry ().get (packaging );
223
- if (nonNull (artifactType ) && IS_EMPTY .test (classifier ) && !IS_EMPTY .test (artifactType .getClassifier ())) {
224
- classifier = artifactType .getClassifier ();
237
+ session .getRepositorySession ().getArtifactTypeRegistry ().get (packaging );
238
+ if (artifactType != null ) {
239
+ if (StringUtils .isEmpty (classifier ) && !StringUtils .isEmpty (artifactType .getClassifier ())) {
240
+ classifier = artifactType .getClassifier ();
241
+ }
242
+ mainArtifactExtension = artifactType .getExtension ();
243
+ } else {
244
+ mainArtifactExtension = packaging ;
225
245
}
226
246
}
247
+ if (extension != null && !Objects .equals (extension , mainArtifactExtension )) {
248
+ log .warn (
249
+ "Main artifact extension should be '{}' but was overridden to '{}'" ,
250
+ mainArtifactExtension ,
251
+ extension );
252
+ }
227
253
Artifact mainArtifact = new DefaultArtifact (
228
- groupId , artifactId , classifier , isFilePom ? "pom" : getExtension ( file ) , version )
254
+ groupId , artifactId , classifier , extension != null ? extension : mainArtifactExtension , version )
229
255
.setFile (file );
230
256
installRequest .addArtifact (mainArtifact );
231
257
@@ -241,10 +267,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
241
267
if (isNull (pomFile )) {
242
268
if (Boolean .TRUE .equals (generatePom ) || (generatePom == null && !pomLocalFile .exists ())) {
243
269
temporaryPom = generatePomFile ();
244
- getLog () .debug ("Installing generated POM" );
270
+ log .debug ("Installing generated POM" );
245
271
installRequest .addArtifact (new SubArtifact (mainArtifact , "" , "pom" , temporaryPom ));
246
272
} else if (generatePom == null ) {
247
- getLog () .debug ("Skipping installation of generated POM, already present in local repository" );
273
+ log .debug ("Skipping installation of generated POM, already present in local repository" );
248
274
}
249
275
} else {
250
276
installRequest .addArtifact (new SubArtifact (mainArtifact , "" , "pom" , pomFile ));
@@ -289,15 +315,15 @@ private File readingPomFromJarFile() throws MojoExecutionException {
289
315
290
316
if (isNull (pomEntry )) {
291
317
// This means there is no entry which matches the "pom.xml"...(or in other words: not packaged by Maven)
292
- getLog () .info ("pom.xml not found in " + file .getName ());
318
+ log .info ("pom.xml not found in {}" , file .getName ());
293
319
return null ;
294
320
}
295
321
296
322
Path tempPomFile = Files .createTempFile (base , ".pom" );
297
323
298
324
Files .copy (jarFile .getInputStream (pomEntry ), tempPomFile , StandardCopyOption .REPLACE_EXISTING );
299
325
300
- getLog () .debug ("Loading " + pomEntry .getName ());
326
+ log .debug ("Loading {}" , pomEntry .getName ());
301
327
processModel (readModel (tempPomFile .toFile ()));
302
328
return tempPomFile .toFile ();
303
329
0 commit comments