Skip to content

Commit

Permalink
[MDEPLOY-311] Consider packaging in deploy-file mojo (#71)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cstamas authored Aug 15, 2024
1 parent c814011 commit 14cc4c3
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 12 deletions.
47 changes: 35 additions & 12 deletions src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import org.eclipse.aether.deployment.DeploymentException;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.util.artifact.SubArtifact;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Installs the artifact in the remote repository.
Expand All @@ -63,6 +65,7 @@
*/
@Mojo(name = "deploy-file", requiresProject = false, threadSafe = true)
public class DeployFileMojo extends AbstractDeployMojo {
private final Logger log = LoggerFactory.getLogger(getClass());
/**
* GroupId of the artifact to be deployed. Retrieved from POM file if specified.
*/
Expand Down Expand Up @@ -90,6 +93,15 @@ public class DeployFileMojo extends AbstractDeployMojo {
@Parameter(property = "packaging")
private String packaging;

/**
* Extension of the artifact to be deployed. If set, will override plugin own logic to detect extension. If not set,
* as Maven expected, packaging determines the artifact extension.
*
* @since 3.1.3
*/
@Parameter(property = "extension")
private String extension;

/**
* Description passed to a generated POM file (in case of generatePom=true)
*/
Expand Down Expand Up @@ -196,7 +208,7 @@ void initProperties() throws MojoExecutionException {
JarEntry entry = jarEntries.nextElement();

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

if (!foundPom) {
getLog().info("pom.xml not found in " + file.getName());
log.info("pom.xml not found in {}", file.getName());
}
} catch (IOException e) {
// ignore, artifact not packaged by Maven
Expand All @@ -235,7 +247,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (Boolean.parseBoolean(skip)
|| ("releases".equals(skip) && !ArtifactUtils.isSnapshot(version))
|| ("snapshots".equals(skip) && ArtifactUtils.isSnapshot(version))) {
getLog().info("Skipping artifact deployment");
log.info("Skipping artifact deployment");
return;
}

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

boolean isFilePom = classifier == null && "pom".equals(packaging);
if (!isFilePom) {
String mainArtifactExtension;
if (classifier == null && "pom".equals(packaging)) {
mainArtifactExtension = "pom";
} else {
ArtifactType artifactType =
session.getRepositorySession().getArtifactTypeRegistry().get(packaging);
if (artifactType != null
&& (classifier == null || classifier.isEmpty())
&& !StringUtils.isEmpty(artifactType.getClassifier())) {
classifier = artifactType.getClassifier();
if (artifactType != null) {
if (StringUtils.isEmpty(classifier) && !StringUtils.isEmpty(artifactType.getClassifier())) {
classifier = artifactType.getClassifier();
}
mainArtifactExtension = artifactType.getExtension();
} else {
mainArtifactExtension = packaging;
}
}
if (extension != null && !Objects.equals(extension, mainArtifactExtension)) {
log.warn(
"Main artifact extension should be '{}' but was overridden to '{}'",
mainArtifactExtension,
extension);
}
Artifact mainArtifact = new DefaultArtifact(
groupId, artifactId, classifier, isFilePom ? "pom" : getExtension(file), version)
groupId, artifactId, classifier, extension != null ? extension : mainArtifactExtension, version)
.setFile(file);
deployRequest.addArtifact(mainArtifact);

Expand All @@ -293,10 +316,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
deployRequest.addArtifact(new SubArtifact(mainArtifact, "", "pom", pomFile));
} else if (generatePom) {
temporaryPom = generatePomFile();
getLog().debug("Deploying generated POM");
log.debug("Deploying generated POM");
deployRequest.addArtifact(new SubArtifact(mainArtifact, "", "pom", temporaryPom));
} else {
getLog().debug("Skipping deploying POM");
log.debug("Skipping deploying POM");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,41 @@ public void testDeployIfArtifactIsNotJar() throws Exception {
assertTrue(file.exists());
}

public void testDeployFileIfPackagingIsSet() throws Exception {
File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-packaging/plugin-config.xml");
mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);

openMocks = MockitoAnnotations.openMocks(this);

assertNotNull(mojo);

DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(
new SimpleLocalRepositoryManagerFactory(new DefaultLocalPathComposer())
.newInstance(repositorySession, new LocalRepository(LOCAL_REPO)));
when(session.getRepositorySession()).thenReturn(repositorySession);

String packaging = (String) getVariableValueFromObject(mojo, "packaging");

String groupId = (String) getVariableValueFromObject(mojo, "groupId");

String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");

String version = (String) getVariableValueFromObject(mojo, "version");

assertEquals("differentpackaging", packaging);

mojo.execute();

File deployedArtifact = new File(
remoteRepo,
"deploy-file-packaging/" + groupId.replace('.', '/') + "/"
+ artifactId + "/" + version + "/" + artifactId + "-"
+ version + "." + packaging);

assertTrue(deployedArtifact.exists());
}

private void addFileToList(File file, List<String> fileList) {
if (!file.isDirectory()) {
fileList.add(file.getName());
Expand Down
36 changes: 36 additions & 0 deletions src/test/resources/unit/deploy-file-packaging/plugin-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<project>
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<groupId>org.apache.maven.test</groupId>
<artifactId>maven-deploy-file-test</artifactId>
<version>1.0</version>
<packaging>differentpackaging</packaging>
<file>${basedir}/src/test/resources/unit/deploy-file-packaging/target/deploy-test-file-1.0-SNAPSHOT.jar</file>
<repositoryId>deploy-test</repositoryId>
<url>file://${basedir}/target/remote-repo/deploy-file-packaging</url>
<generatePom>true</generatePom>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is not an actual jar

0 comments on commit 14cc4c3

Please sign in to comment.