Skip to content

Commit 14cc4c3

Browse files
authored
[MDEPLOY-311] Consider packaging in deploy-file mojo (#71)
The packaging was not really considered. Borrow UT from PR #42, thanks! 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/MDEPLOY-311
1 parent c814011 commit 14cc4c3

File tree

4 files changed

+107
-12
lines changed

4 files changed

+107
-12
lines changed

src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java

+35-12
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
import org.eclipse.aether.deployment.DeploymentException;
5656
import org.eclipse.aether.repository.RemoteRepository;
5757
import org.eclipse.aether.util.artifact.SubArtifact;
58+
import org.slf4j.Logger;
59+
import org.slf4j.LoggerFactory;
5860

5961
/**
6062
* Installs the artifact in the remote repository.
@@ -63,6 +65,7 @@
6365
*/
6466
@Mojo(name = "deploy-file", requiresProject = false, threadSafe = true)
6567
public class DeployFileMojo extends AbstractDeployMojo {
68+
private final Logger log = LoggerFactory.getLogger(getClass());
6669
/**
6770
* GroupId of the artifact to be deployed. Retrieved from POM file if specified.
6871
*/
@@ -90,6 +93,15 @@ public class DeployFileMojo extends AbstractDeployMojo {
9093
@Parameter(property = "packaging")
9194
private String packaging;
9295

96+
/**
97+
* Extension of the artifact to be deployed. If set, will override plugin own logic to detect extension. If not set,
98+
* as Maven expected, packaging determines the artifact extension.
99+
*
100+
* @since 3.1.3
101+
*/
102+
@Parameter(property = "extension")
103+
private String extension;
104+
93105
/**
94106
* Description passed to a generated POM file (in case of generatePom=true)
95107
*/
@@ -196,7 +208,7 @@ void initProperties() throws MojoExecutionException {
196208
JarEntry entry = jarEntries.nextElement();
197209

198210
if (pomEntry.matcher(entry.getName()).matches()) {
199-
getLog().debug("Using " + entry.getName() + " as pomFile");
211+
log.debug("Using {} as pomFile", entry.getName());
200212
foundPom = true;
201213
String base = file.getName();
202214
if (base.indexOf('.') > 0) {
@@ -215,7 +227,7 @@ void initProperties() throws MojoExecutionException {
215227
}
216228

217229
if (!foundPom) {
218-
getLog().info("pom.xml not found in " + file.getName());
230+
log.info("pom.xml not found in {}", file.getName());
219231
}
220232
} catch (IOException e) {
221233
// ignore, artifact not packaged by Maven
@@ -235,7 +247,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
235247
if (Boolean.parseBoolean(skip)
236248
|| ("releases".equals(skip) && !ArtifactUtils.isSnapshot(version))
237249
|| ("snapshots".equals(skip) && ArtifactUtils.isSnapshot(version))) {
238-
getLog().info("Skipping artifact deployment");
250+
log.info("Skipping artifact deployment");
239251
return;
240252
}
241253

@@ -266,18 +278,29 @@ public void execute() throws MojoExecutionException, MojoFailureException {
266278
DeployRequest deployRequest = new DeployRequest();
267279
deployRequest.setRepository(remoteRepository);
268280

269-
boolean isFilePom = classifier == null && "pom".equals(packaging);
270-
if (!isFilePom) {
281+
String mainArtifactExtension;
282+
if (classifier == null && "pom".equals(packaging)) {
283+
mainArtifactExtension = "pom";
284+
} else {
271285
ArtifactType artifactType =
272286
session.getRepositorySession().getArtifactTypeRegistry().get(packaging);
273-
if (artifactType != null
274-
&& (classifier == null || classifier.isEmpty())
275-
&& !StringUtils.isEmpty(artifactType.getClassifier())) {
276-
classifier = artifactType.getClassifier();
287+
if (artifactType != null) {
288+
if (StringUtils.isEmpty(classifier) && !StringUtils.isEmpty(artifactType.getClassifier())) {
289+
classifier = artifactType.getClassifier();
290+
}
291+
mainArtifactExtension = artifactType.getExtension();
292+
} else {
293+
mainArtifactExtension = packaging;
277294
}
278295
}
296+
if (extension != null && !Objects.equals(extension, mainArtifactExtension)) {
297+
log.warn(
298+
"Main artifact extension should be '{}' but was overridden to '{}'",
299+
mainArtifactExtension,
300+
extension);
301+
}
279302
Artifact mainArtifact = new DefaultArtifact(
280-
groupId, artifactId, classifier, isFilePom ? "pom" : getExtension(file), version)
303+
groupId, artifactId, classifier, extension != null ? extension : mainArtifactExtension, version)
281304
.setFile(file);
282305
deployRequest.addArtifact(mainArtifact);
283306

@@ -293,10 +316,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
293316
deployRequest.addArtifact(new SubArtifact(mainArtifact, "", "pom", pomFile));
294317
} else if (generatePom) {
295318
temporaryPom = generatePomFile();
296-
getLog().debug("Deploying generated POM");
319+
log.debug("Deploying generated POM");
297320
deployRequest.addArtifact(new SubArtifact(mainArtifact, "", "pom", temporaryPom));
298321
} else {
299-
getLog().debug("Skipping deploying POM");
322+
log.debug("Skipping deploying POM");
300323
}
301324
}
302325

src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,41 @@ public void testDeployIfArtifactIsNotJar() throws Exception {
276276
assertTrue(file.exists());
277277
}
278278

279+
public void testDeployFileIfPackagingIsSet() throws Exception {
280+
File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-packaging/plugin-config.xml");
281+
mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);
282+
283+
openMocks = MockitoAnnotations.openMocks(this);
284+
285+
assertNotNull(mojo);
286+
287+
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
288+
repositorySession.setLocalRepositoryManager(
289+
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
290+
.newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
291+
when(session.getRepositorySession()).thenReturn(repositorySession);
292+
293+
String packaging = (String) getVariableValueFromObject(mojo, "packaging");
294+
295+
String groupId = (String) getVariableValueFromObject(mojo, "groupId");
296+
297+
String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
298+
299+
String version = (String) getVariableValueFromObject(mojo, "version");
300+
301+
assertEquals("differentpackaging", packaging);
302+
303+
mojo.execute();
304+
305+
File deployedArtifact = new File(
306+
remoteRepo,
307+
"deploy-file-packaging/" + groupId.replace('.', '/') + "/"
308+
+ artifactId + "/" + version + "/" + artifactId + "-"
309+
+ version + "." + packaging);
310+
311+
assertTrue(deployedArtifact.exists());
312+
}
313+
279314
private void addFileToList(File file, List<String> fileList) {
280315
if (!file.isDirectory()) {
281316
fileList.add(file.getName());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing,
11+
software distributed under the License is distributed on an
12+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13+
KIND, either express or implied. See the License for the
14+
specific language governing permissions and limitations
15+
under the License.
16+
-->
17+
18+
<project>
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<artifactId>maven-deploy-plugin</artifactId>
23+
<configuration>
24+
<groupId>org.apache.maven.test</groupId>
25+
<artifactId>maven-deploy-file-test</artifactId>
26+
<version>1.0</version>
27+
<packaging>differentpackaging</packaging>
28+
<file>${basedir}/src/test/resources/unit/deploy-file-packaging/target/deploy-test-file-1.0-SNAPSHOT.jar</file>
29+
<repositoryId>deploy-test</repositoryId>
30+
<url>file://${basedir}/target/remote-repo/deploy-file-packaging</url>
31+
<generatePom>true</generatePom>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is not an actual jar

0 commit comments

Comments
 (0)