You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2024/08/15 11:40:52 UTC

(maven-deploy-plugin) branch maven-deploy-plugin-3.x updated: [MDEPLOY-311] Consider packaging in deploy-file mojo (#71)

This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch maven-deploy-plugin-3.x
in repository https://gitbox.apache.org/repos/asf/maven-deploy-plugin.git


The following commit(s) were added to refs/heads/maven-deploy-plugin-3.x by this push:
     new 14cc4c3  [MDEPLOY-311] Consider packaging in deploy-file mojo (#71)
14cc4c3 is described below

commit 14cc4c305af2fc53571afc8ef127d7bb23155ce5
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Thu Aug 15 13:40:47 2024 +0200

    [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
---
 .../maven/plugins/deploy/DeployFileMojo.java       | 47 ++++++++++++++++------
 .../maven/plugins/deploy/DeployFileMojoTest.java   | 35 ++++++++++++++++
 .../unit/deploy-file-packaging/plugin-config.xml   | 36 +++++++++++++++++
 .../target/deploy-test-file-1.0-SNAPSHOT.jar       |  1 +
 4 files changed, 107 insertions(+), 12 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java b/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java
index 66bf21f..0dfc8c3 100644
--- a/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java
+++ b/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java
@@ -55,6 +55,8 @@ import org.eclipse.aether.deployment.DeployRequest;
 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.
@@ -63,6 +65,7 @@ import org.eclipse.aether.util.artifact.SubArtifact;
  */
 @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.
      */
@@ -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)
      */
@@ -196,7 +208,7 @@ public class DeployFileMojo extends AbstractDeployMojo {
                     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) {
@@ -215,7 +227,7 @@ public class DeployFileMojo extends AbstractDeployMojo {
                 }
 
                 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
@@ -235,7 +247,7 @@ public class DeployFileMojo extends AbstractDeployMojo {
         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;
         }
 
@@ -266,18 +278,29 @@ public class DeployFileMojo extends AbstractDeployMojo {
         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);
 
@@ -293,10 +316,10 @@ public class DeployFileMojo extends AbstractDeployMojo {
                 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");
             }
         }
 
diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java
index b1083cb..1374881 100644
--- a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java
@@ -276,6 +276,41 @@ public class DeployFileMojoTest extends AbstractMojoTestCase {
         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());
diff --git a/src/test/resources/unit/deploy-file-packaging/plugin-config.xml b/src/test/resources/unit/deploy-file-packaging/plugin-config.xml
new file mode 100644
index 0000000..170cd1c
--- /dev/null
+++ b/src/test/resources/unit/deploy-file-packaging/plugin-config.xml
@@ -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>
\ No newline at end of file
diff --git a/src/test/resources/unit/deploy-file-packaging/target/deploy-test-file-1.0-SNAPSHOT.jar b/src/test/resources/unit/deploy-file-packaging/target/deploy-test-file-1.0-SNAPSHOT.jar
new file mode 100644
index 0000000..6f5f2f8
--- /dev/null
+++ b/src/test/resources/unit/deploy-file-packaging/target/deploy-test-file-1.0-SNAPSHOT.jar
@@ -0,0 +1 @@
+This is not an actual jar
\ No newline at end of file