Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 49abeeb

Browse files
authored
[MINSTALL-196] Consider packaging in install-file mojo (#81)
Bug fix and adjust some fluke ITs. Changes: * honor packaging to calculate classifier (optionally) and extension * add param "extension" to override Maven calculated extension explicitly * switch to SLF4J logging --- https://issues.apache.org/jira/browse/MINSTALL-196
1 parent 30ca28e commit 49abeeb

File tree

9 files changed

+54
-26
lines changed

9 files changed

+54
-26
lines changed

pom.xml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
<version>${mavenVersion}</version>
105105
<scope>provided</scope>
106106
</dependency>
107-
<!-- This is here to override 3.0 coming with maven-artifact-transfer -->
108107
<dependency>
109108
<groupId>org.apache.maven</groupId>
110109
<artifactId>maven-core</artifactId>
@@ -130,6 +129,12 @@
130129
<!-- To work in Maven versions older than 3.9.0 -->
131130
<scope>compile</scope>
132131
</dependency>
132+
<dependency>
133+
<groupId>org.slf4j</groupId>
134+
<artifactId>slf4j-api</artifactId>
135+
<version>${slf4jVersion}</version>
136+
<scope>provided</scope>
137+
</dependency>
133138
<dependency>
134139
<groupId>org.codehaus.plexus</groupId>
135140
<artifactId>plexus-utils</artifactId>
@@ -171,12 +176,6 @@
171176
<version>4.8.1</version>
172177
<scope>test</scope>
173178
</dependency>
174-
<dependency>
175-
<groupId>org.slf4j</groupId>
176-
<artifactId>slf4j-api</artifactId>
177-
<version>${slf4jVersion}</version>
178-
<scope>provided</scope>
179-
</dependency>
180179
<dependency>
181180
<groupId>org.slf4j</groupId>
182181
<artifactId>slf4j-nop</artifactId>

src/it/install-file-minstall-121-bundle/test.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
file = test-0.1.jar
1919
pomFile = test-0.1.pom
2020
packaging = bundle
21+
extension = jar

src/it/install-file-minstall-121-bundle/verify.bsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ String[] paths =
2424
{
2525
"org/apache/maven/its/install/121/maven-bundle/maven-metadata-local.xml",
2626
"org/apache/maven/its/install/121/maven-bundle/1.0/maven-bundle-1.0.pom",
27-
"org/apache/maven/its/install/121/maven-bundle/1.0/maven-bundle-1.0.jar",
27+
"org/apache/maven/its/install/121/maven-bundle/1.0/maven-bundle-1.0.jar", // packaging bundle is NOT in core! (explicitly set with -Dextension=jar)
2828
};
2929

3030
for ( String path : paths )

src/it/install-file-minstall-121-java-source/verify.bsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ String[] paths =
2424
{
2525
"org/apache/maven/its/install/121/test-java-source/maven-metadata-local.xml",
2626
"org/apache/maven/its/install/121/test-java-source/1.0/test-java-source-1.0.pom",
27-
"org/apache/maven/its/install/121/test-java-source/1.0/test-java-source-1.0-sources.jar",
27+
"org/apache/maven/its/install/121/test-java-source/1.0/test-java-source-1.0-sources.jar", // packaging java-source IS in core
2828
};
2929

3030
for ( String path : paths )

src/it/install-file-minstall-121-maven-archetype/test.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
file = test-0.1.jar
1919
pomFile = test-0.1.pom
2020
packaging = maven-archetype
21+
extension = jar

src/it/install-file-minstall-121-maven-archetype/verify.bsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ String[] paths =
2424
{
2525
"org/apache/maven/its/install/121/maven-archetype/maven-metadata-local.xml",
2626
"org/apache/maven/its/install/121/maven-archetype/1.0/maven-archetype-1.0.pom",
27-
"org/apache/maven/its/install/121/maven-archetype/1.0/maven-archetype-1.0.jar",
27+
"org/apache/maven/its/install/121/maven-archetype/1.0/maven-archetype-1.0.jar", // packaging maven-archetype is NOT in core! (explicitly set with -Dextension=jar)
2828
};
2929

3030
for ( String path : paths )

src/it/install-file-minstall-121-targz/test.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
file = test-0.1.tar.gz
1919
pomFile = test-0.1.pom
2020
packaging = war
21+
extension = tar.gz

src/it/install-file-minstall-121-targz/verify.bsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ String[] paths =
2424
{
2525
"org/apache/maven/its/install/121/test-targz/maven-metadata-local.xml",
2626
"org/apache/maven/its/install/121/test-targz/1.0/test-targz-1.0.pom",
27-
"org/apache/maven/its/install/121/test-targz/1.0/test-targz-1.0.tar.gz",
27+
"org/apache/maven/its/install/121/test-targz/1.0/test-targz-1.0.tar.gz", // this test is totally fluke: deploy POM.packaging=war and main artifact tar.gz
2828
};
2929

3030
for ( String path : paths )

src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.nio.file.Files;
2727
import java.nio.file.Path;
2828
import java.nio.file.StandardCopyOption;
29+
import java.util.Objects;
2930
import java.util.function.Predicate;
3031
import java.util.jar.JarEntry;
3132
import java.util.jar.JarFile;
@@ -43,6 +44,7 @@
4344
import org.apache.maven.plugins.annotations.Mojo;
4445
import org.apache.maven.plugins.annotations.Parameter;
4546
import org.codehaus.plexus.util.FileUtils;
47+
import org.codehaus.plexus.util.StringUtils;
4648
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
4749
import org.eclipse.aether.DefaultRepositoryCache;
4850
import org.eclipse.aether.DefaultRepositorySystemSession;
@@ -56,9 +58,10 @@
5658
import org.eclipse.aether.repository.LocalRepository;
5759
import org.eclipse.aether.repository.LocalRepositoryManager;
5860
import org.eclipse.aether.util.artifact.SubArtifact;
61+
import org.slf4j.Logger;
62+
import org.slf4j.LoggerFactory;
5963

6064
import static java.util.Objects.isNull;
61-
import static java.util.Objects.nonNull;
6265

6366
/**
6467
* Installs a file in the local repository.
@@ -68,6 +71,7 @@
6871
@Mojo(name = "install-file", requiresProject = false, aggregator = true, threadSafe = true)
6972
public 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

Comments
 (0)