You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/10/16 09:00:00 UTC

(maven) branch master updated: Report an invalid parent of an attached artifact instead of a NullPointerException (fix #8393)

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

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 50d17ebe4a Report an invalid parent of an attached artifact instead of a NullPointerException (fix #8393)
50d17ebe4a is described below

commit 50d17ebe4a6d97579e7bb29c7fd9c867f75ad275
Author: Arturo Bernal <ab...@apache.org>
AuthorDate: Fri Oct 16 10:59:54 2026 +0200

    Report an invalid parent of an attached artifact instead of a NullPointerException (fix #8393)
    
    AttachedArtifact.getVersion() returned the version of the parent, but the superclass constructor validates its arguments before the parent is set. When the validation failed, building the error message called getVersion() on a null parent and a NullPointerException hid the real error. Fall back to the version of the superclass until the parent is set.
    
    Fixes #8393
---
 .../maven/project/artifact/AttachedArtifact.java   |  3 +-
 .../project/artifact/AttachedArtifactTest.java     | 61 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/impl/maven-core/src/main/java/org/apache/maven/project/artifact/AttachedArtifact.java b/impl/maven-core/src/main/java/org/apache/maven/project/artifact/AttachedArtifact.java
index b3938f4b85..5dab7e4c00 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/project/artifact/AttachedArtifact.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/project/artifact/AttachedArtifact.java
@@ -139,7 +139,8 @@ public void setScope(String scope) {
 
     @Override
     public String getVersion() {
-        return parent.getVersion();
+        // the superclass constructor validates its arguments, and builds the error message, before the parent is set
+        return parent != null ? parent.getVersion() : super.getVersion();
     }
 
     @Override
diff --git a/impl/maven-core/src/test/java/org/apache/maven/project/artifact/AttachedArtifactTest.java b/impl/maven-core/src/test/java/org/apache/maven/project/artifact/AttachedArtifactTest.java
new file mode 100644
index 0000000000..93adfce831
--- /dev/null
+++ b/impl/maven-core/src/test/java/org/apache/maven/project/artifact/AttachedArtifactTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.
+ */
+package org.apache.maven.project.artifact;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.InvalidArtifactRTException;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.DefaultArtifactHandler;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@SuppressWarnings("deprecation")
+class AttachedArtifactTest {
+
+    private final ArtifactHandler handler = new DefaultArtifactHandler("jar");
+
+    @Test
+    void testVersionIsTheOneOfTheParent() {
+        Artifact parent = new DefaultArtifact("g", "a", "1.0", "compile", "jar", null, handler);
+
+        Artifact attached = new AttachedArtifact(parent, "jar", "sources", handler);
+
+        assertEquals("1.0", attached.getVersion());
+    }
+
+    /**
+     * A parent which has a version but no version range, as after {@link Artifact#setVersion(String)}, is not a
+     * valid base for an attached artifact. This is reported as such, not as a NullPointerException raised while
+     * the error message is built.
+     */
+    @Test
+    void testParentWithoutVersionRangeIsRejected() {
+        Artifact parent = new DefaultArtifact("g", "a", "1.0", "compile", "jar", null, handler);
+        parent.setVersion("1.0");
+
+        InvalidArtifactRTException e = assertThrows(
+                InvalidArtifactRTException.class, () -> new AttachedArtifact(parent, "jar", "sources", handler));
+
+        assertTrue(e.getMessage().contains("The version cannot be empty."), e.getMessage());
+    }
+}