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

cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver-ant-tasks.git


The following commit(s) were added to refs/heads/master by this push:
     new b3def1b  Defend against NPE when no pom is registered using the pom 
task prior to using the deploy task (fix for #138) (#139)
b3def1b is described below

commit b3def1b43b405b50747dd8f1f758d705565d02fe
Author: Per Nyfelt <[email protected]>
AuthorDate: Wed Dec 10 22:59:23 2025 +0100

    Defend against NPE when no pom is registered using the pom task prior to 
using the deploy task (fix for #138) (#139)
    
    This PR fixes the #138 bug by adding a defensive action avoiding the NPE 
and instead throws a Build Exception with a clear error message that the user 
needs to register a pom using the pom task. The fix follows the suggestion of 
the bug reported (Dirk Schnelle-Walka).
    
    Fixes #138
    
    ---------
    
    Co-authored-by: per <[email protected]>
---
 .../internal/ant/tasks/AbstractDistTask.java       |  9 +++++--
 .../maven/resolver/internal/ant/DeployTest.java    | 31 ++++++++++++++++++++++
 src/test/resources/ant/Deploy/ant.xml              | 22 +++++++++++++++
 3 files changed, 60 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/apache/maven/resolver/internal/ant/tasks/AbstractDistTask.java
 
b/src/main/java/org/apache/maven/resolver/internal/ant/tasks/AbstractDistTask.java
index 84dec13..f7e1c31 100644
--- 
a/src/main/java/org/apache/maven/resolver/internal/ant/tasks/AbstractDistTask.java
+++ 
b/src/main/java/org/apache/maven/resolver/internal/ant/tasks/AbstractDistTask.java
@@ -130,13 +130,18 @@ public abstract class AbstractDistTask extends Task {
                 version = artifactPom.getVersion();
             }
 
-            final Model model = getPom().getModel(this);
+            final Pom pom = getPom();
+            if (pom == null) {
+                throw new BuildException("You must specify the <pom 
file=\"...\"> element"
+                        + " to denote the descriptor for the artifacts");
+            }
+            final Model model = pom.getModel(this);
 
             if (!(model.getGroupId().equals(gid)
                     && model.getArtifactId().equals(aid)
                     && model.getVersion().equals(version))) {
                 throw new BuildException(
-                        "Artifact references different pom than it would be 
installed with: " + artifact.toString());
+                        "Artifact references different pom than it would be 
installed with: " + artifact);
             }
         }
     }
diff --git 
a/src/test/java/org/apache/maven/resolver/internal/ant/DeployTest.java 
b/src/test/java/org/apache/maven/resolver/internal/ant/DeployTest.java
index aa7ba04..74b38ef 100644
--- a/src/test/java/org/apache/maven/resolver/internal/ant/DeployTest.java
+++ b/src/test/java/org/apache/maven/resolver/internal/ant/DeployTest.java
@@ -22,6 +22,7 @@ import java.io.File;
 import java.util.Arrays;
 
 import junit.framework.JUnit4TestAdapter;
+import org.apache.tools.ant.BuildException;
 import org.junit.Test;
 
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -30,6 +31,7 @@ import static org.hamcrest.Matchers.endsWith;
 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
 import static org.hamcrest.Matchers.hasItemInArray;
 import static org.hamcrest.Matchers.lessThanOrEqualTo;
+import static org.junit.Assert.fail;
 
 /*
  * still missing:
@@ -96,4 +98,33 @@ public class DeployTest extends AntBuildsTest {
                 file.lastModified(),
                 allOf(greaterThanOrEqualTo(min), lessThanOrEqualTo(max)));
     }
+
+    /**
+     * Reproduces an NPE when a deploy &lt;artifact&gt; contains only a nested 
&lt;pom/&gt;.
+     * <pre>{@code
+     *   <repo:deploy>
+     *     <repo:artifact file="${artifact.file}">
+     *       <repo:pom file="${project.dir}/dummy-pom.xml"/>
+     *     </repo:artifact>
+     *     <repo:snapshotrepo refid="Snapshots"/>
+     *   </repo:deploy>
+     * }</pre>
+     * Current behavior: Ant build fails with a NullPointerException at the 
root cause.
+     * Once the deploy task supports this case or throws a clearer error, 
update the assertion accordingly.
+     */
+    @Test
+    public void testDeployOnlyNestedPomException() {
+        try {
+            executeTarget("testDeployOnlyNestedPomException");
+            fail("Expected the build to fail when deploying with only a nested 
<pom/> inside <artifact>.");
+        } catch (Exception e) {
+            Throwable cause = e;
+            while (cause.getCause() != null) {
+                cause = cause.getCause();
+            }
+            if (!(cause instanceof BuildException)) {
+                fail("Expected NullPointerException as root cause, but was: " 
+ cause);
+            }
+        }
+    }
 }
diff --git a/src/test/resources/ant/Deploy/ant.xml 
b/src/test/resources/ant/Deploy/ant.xml
index 2ad2d7a..289d3a3 100644
--- a/src/test/resources/ant/Deploy/ant.xml
+++ b/src/test/resources/ant/Deploy/ant.xml
@@ -69,4 +69,26 @@
     </repo:deploy>
   </target>
 
+ <!--
+  Only a nested <pom/> inside <resolver:artifact>.
+  The DeployTest expects this target to fail with a BuildException.
+ -->
+  <target name="testDeployOnlyNestedPomException">
+    <property name="artifact.file" value="${build.dir}/dummy-artifact.jar"/>
+    <property name="temp.dir" value="${build.dir}/temp"/>
+    <property name="temp.content.file" value="${temp.dir}/content.txt"/>
+    <echo file="${temp.content.file}" append="false">
+      This is a test artifact content.
+    </echo>
+    <jar destfile="${artifact.file}">
+      <fileset dir="${temp.dir}">
+        <include name="content.txt"/>
+      </fileset>
+    </jar>
+    <repo:deploy>
+      <repo:artifact file="${artifact.file}">
+        <repo:pom file="${project.dir}/dummy-pom.xml"/>
+      </repo:artifact>
+    </repo:deploy>
+  </target>
 </project>

Reply via email to