2626import java .nio .file .Files ;
2727import java .nio .file .Path ;
2828import java .nio .file .StandardCopyOption ;
29+ import java .util .Objects ;
2930import java .util .function .Predicate ;
3031import java .util .jar .JarEntry ;
3132import java .util .jar .JarFile ;
4344import org .apache .maven .plugins .annotations .Mojo ;
4445import org .apache .maven .plugins .annotations .Parameter ;
4546import org .codehaus .plexus .util .FileUtils ;
47+ import org .codehaus .plexus .util .StringUtils ;
4648import org .codehaus .plexus .util .xml .pull .XmlPullParserException ;
4749import org .eclipse .aether .DefaultRepositoryCache ;
4850import org .eclipse .aether .DefaultRepositorySystemSession ;
5658import org .eclipse .aether .repository .LocalRepository ;
5759import org .eclipse .aether .repository .LocalRepositoryManager ;
5860import org .eclipse .aether .util .artifact .SubArtifact ;
61+ import org .slf4j .Logger ;
62+ import org .slf4j .LoggerFactory ;
5963
6064import static java .util .Objects .isNull ;
61- import static java .util .Objects .nonNull ;
6265
6366/**
6467 * Installs a file in the local repository.
6871@ Mojo (name = "install-file" , requiresProject = false , aggregator = true , threadSafe = true )
6972public class InstallFileMojo extends AbstractMojo {
7073 private static final String LS = System .lineSeparator ();
74+ private final Logger log = LoggerFactory .getLogger (getClass ());
7175
7276 @ Component
7377 private RepositorySystem repositorySystem ;
@@ -112,6 +116,15 @@ public class InstallFileMojo extends AbstractMojo {
112116 @ Parameter (property = "classifier" )
113117 private String classifier ;
114118
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+
115128 /**
116129 * The file to be installed in the local repository.
117130 */
@@ -169,7 +182,7 @@ public class InstallFileMojo extends AbstractMojo {
169182 public void execute () throws MojoExecutionException , MojoFailureException {
170183 if (!file .exists ()) {
171184 String message = "The specified file '" + file .getPath () + "' does not exist" ;
172- getLog () .error (message );
185+ log .error (message );
173186 throw new MojoFailureException (message );
174187 }
175188
@@ -189,8 +202,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
189202 newSession , new LocalRepository (localRepositoryPath , contentType ));
190203 newSession .setLocalRepositoryManager (localRepositoryManager );
191204 repositorySystemSession = newSession ;
192- getLog () .debug ("localRepoPath: "
193- + localRepositoryManager .getRepository ().getBasedir ());
205+ log .debug (
206+ "localRepoPath: {}" , localRepositoryManager .getRepository ().getBasedir ());
194207 }
195208
196209 File temporaryPom = null ;
@@ -199,7 +212,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
199212 temporaryPom = readingPomFromJarFile ();
200213 if (!Boolean .TRUE .equals (generatePom )) {
201214 pomFile = temporaryPom ;
202- getLog () .debug ("Using JAR embedded POM as pomFile" );
215+ log .debug ("Using JAR embedded POM as pomFile" );
203216 }
204217 } else {
205218 processModel (readModel (pomFile ));
@@ -216,16 +229,29 @@ public void execute() throws MojoExecutionException, MojoFailureException {
216229
217230 InstallRequest installRequest = new InstallRequest ();
218231
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 {
221236 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 ;
225245 }
226246 }
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+ }
227253 Artifact mainArtifact = new DefaultArtifact (
228- groupId , artifactId , classifier , isFilePom ? "pom" : getExtension ( file ) , version )
254+ groupId , artifactId , classifier , extension != null ? extension : mainArtifactExtension , version )
229255 .setFile (file );
230256 installRequest .addArtifact (mainArtifact );
231257
@@ -241,10 +267,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
241267 if (isNull (pomFile )) {
242268 if (Boolean .TRUE .equals (generatePom ) || (generatePom == null && !pomLocalFile .exists ())) {
243269 temporaryPom = generatePomFile ();
244- getLog () .debug ("Installing generated POM" );
270+ log .debug ("Installing generated POM" );
245271 installRequest .addArtifact (new SubArtifact (mainArtifact , "" , "pom" , temporaryPom ));
246272 } 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" );
248274 }
249275 } else {
250276 installRequest .addArtifact (new SubArtifact (mainArtifact , "" , "pom" , pomFile ));
@@ -289,15 +315,15 @@ private File readingPomFromJarFile() throws MojoExecutionException {
289315
290316 if (isNull (pomEntry )) {
291317 // 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 ());
293319 return null ;
294320 }
295321
296322 Path tempPomFile = Files .createTempFile (base , ".pom" );
297323
298324 Files .copy (jarFile .getInputStream (pomEntry ), tempPomFile , StandardCopyOption .REPLACE_EXISTING );
299325
300- getLog () .debug ("Loading " + pomEntry .getName ());
326+ log .debug ("Loading {}" , pomEntry .getName ());
301327 processModel (readModel (tempPomFile .toFile ()));
302328 return tempPomFile .toFile ();
303329
0 commit comments