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

hboutemy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-source-plugin.git

commit 53dc0f298ae75a2f78ca8bc437165dd6a35c64af
Author: Hervé Boutemy <hbout...@apache.org>
AuthorDate: Thu Mar 28 12:26:43 2024 +0100

    [MSOURCES-140] improve message when lifecycle run twice
---
 src/it/MSOURCES-121/invoker.properties             |  2 +-
 src/it/MSOURCES-121/pom.xml                        |  3 +++
 src/it/MSOURCES-121/verify.groovy                  | 22 +++++++++++++++++++++
 .../invoker.properties                             |  1 +
 src/it/{MSOURCES-121 => MSOURCES-140}/pom.xml      | 17 +++-------------
 src/it/MSOURCES-140/src/main/java/MyClass.java     | 23 ++++++++++++++++++++++
 .../src/main/resources/main.properties}            |  2 +-
 src/it/MSOURCES-140/src/test/java/MyTest.java      | 23 ++++++++++++++++++++++
 .../src/test/resources/test.properties}            |  2 +-
 src/it/MSOURCES-140/verify.groovy                  | 22 +++++++++++++++++++++
 .../plugins/source/AbstractSourceJarMojo.java      | 23 ++++++++++++++++------
 11 files changed, 117 insertions(+), 23 deletions(-)

diff --git a/src/it/MSOURCES-121/invoker.properties 
b/src/it/MSOURCES-121/invoker.properties
index c43b263..2668a60 100644
--- a/src/it/MSOURCES-121/invoker.properties
+++ b/src/it/MSOURCES-121/invoker.properties
@@ -14,4 +14,4 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-invoker.buildResult = success
+invoker.buildResult = failure
diff --git a/src/it/MSOURCES-121/pom.xml b/src/it/MSOURCES-121/pom.xml
index 26c61ac..975b8c2 100644
--- a/src/it/MSOURCES-121/pom.xml
+++ b/src/it/MSOURCES-121/pom.xml
@@ -57,6 +57,9 @@
             <goals>
               <goal>jar-no-fork</goal>
             </goals>
+            <configuration>
+              <finalName>${project.build.finalName}-secondary</finalName>
+            </configuration>
           </execution>
         </executions>
       </plugin>
diff --git a/src/it/MSOURCES-121/verify.groovy 
b/src/it/MSOURCES-121/verify.groovy
new file mode 100644
index 0000000..e58ef75
--- /dev/null
+++ b/src/it/MSOURCES-121/verify.groovy
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+File buildLog = new File( basedir, 'build.log' )
+
+assert buildLog.text =~ /\[ERROR\] Artifact 
org.apache.maven.its.sources:jar-no-fork:java-source:sources:1.0-SNAPSHOT 
already attached to a file target.jar-no-fork-1.0-SNAPSHOT-sources.jar: attach 
to target.jar-no-fork-1.0-SNAPSHOT-secondary-sources.jar should be done with 
another classifier/
diff --git a/src/it/MSOURCES-121/invoker.properties 
b/src/it/MSOURCES-140/invoker.properties
similarity index 95%
copy from src/it/MSOURCES-121/invoker.properties
copy to src/it/MSOURCES-140/invoker.properties
index c43b263..54abe94 100644
--- a/src/it/MSOURCES-121/invoker.properties
+++ b/src/it/MSOURCES-140/invoker.properties
@@ -15,3 +15,4 @@
 # specific language governing permissions and limitations
 # under the License.
 invoker.buildResult = success
+invoker.goals = package clean install
diff --git a/src/it/MSOURCES-121/pom.xml b/src/it/MSOURCES-140/pom.xml
similarity index 77%
copy from src/it/MSOURCES-121/pom.xml
copy to src/it/MSOURCES-140/pom.xml
index 26c61ac..c22d234 100644
--- a/src/it/MSOURCES-121/pom.xml
+++ b/src/it/MSOURCES-140/pom.xml
@@ -27,13 +27,8 @@
   <artifactId>jar-no-fork</artifactId>
   <version>1.0-SNAPSHOT</version>
 
-  <name>Test for multiple attachments of files</name>
-       <description>This build should fail based on the duplicate
-   execution with the same configuration. This will erroneously
-   add the classifier/file twice.
-   MSOURCES-121.
-   update with MSOURCES-141: do not fail but detect and not add twice
-  </description>
+  <name>Test for multiple attachments of one file caused by  multiple runs in 
one session</name>
+  <description>mvn package clean install should not fail</description>
 
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -47,13 +42,7 @@
         <version>@project.version@</version>
         <executions>
           <execution>
-            <id>attach-sources-1</id>
-            <goals>
-              <goal>jar-no-fork</goal>
-            </goals>
-          </execution>
-          <execution>
-            <id>attach-sources-2</id>
+            <id>attach-sources</id>
             <goals>
               <goal>jar-no-fork</goal>
             </goals>
diff --git a/src/it/MSOURCES-140/src/main/java/MyClass.java 
b/src/it/MSOURCES-140/src/main/java/MyClass.java
new file mode 100644
index 0000000..4945381
--- /dev/null
+++ b/src/it/MSOURCES-140/src/main/java/MyClass.java
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+public class MyClass
+{
+
+}
diff --git a/src/it/MSOURCES-121/invoker.properties 
b/src/it/MSOURCES-140/src/main/resources/main.properties
similarity index 96%
copy from src/it/MSOURCES-121/invoker.properties
copy to src/it/MSOURCES-140/src/main/resources/main.properties
index c43b263..00ae6c0 100644
--- a/src/it/MSOURCES-121/invoker.properties
+++ b/src/it/MSOURCES-140/src/main/resources/main.properties
@@ -14,4 +14,4 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-invoker.buildResult = success
+
diff --git a/src/it/MSOURCES-140/src/test/java/MyTest.java 
b/src/it/MSOURCES-140/src/test/java/MyTest.java
new file mode 100644
index 0000000..9d124be
--- /dev/null
+++ b/src/it/MSOURCES-140/src/test/java/MyTest.java
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+public class MyTest
+{
+
+}
diff --git a/src/it/MSOURCES-121/invoker.properties 
b/src/it/MSOURCES-140/src/test/resources/test.properties
similarity index 96%
copy from src/it/MSOURCES-121/invoker.properties
copy to src/it/MSOURCES-140/src/test/resources/test.properties
index c43b263..00ae6c0 100644
--- a/src/it/MSOURCES-121/invoker.properties
+++ b/src/it/MSOURCES-140/src/test/resources/test.properties
@@ -14,4 +14,4 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-invoker.buildResult = success
+
diff --git a/src/it/MSOURCES-140/verify.groovy 
b/src/it/MSOURCES-140/verify.groovy
new file mode 100644
index 0000000..8cc6491
--- /dev/null
+++ b/src/it/MSOURCES-140/verify.groovy
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+File buildLog = new File( basedir, 'build.log' )
+
+assert buildLog.text =~ /\[INFO\] Artifact 
org.apache.maven.its.sources:jar-no-fork:java-source:sources:1.0-SNAPSHOT 
already attached to target.jar-no-fork-1.0-SNAPSHOT-sources.jar: ignoring same 
re-attach \(same artifact, same file\)/
diff --git 
a/src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java 
b/src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java
index 54d11a5..284d2f1 100644
--- a/src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java
+++ b/src/main/java/org/apache/maven/plugins/source/AbstractSourceJarMojo.java
@@ -20,6 +20,7 @@ package org.apache.maven.plugins.source;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -306,17 +307,22 @@ public abstract class AbstractSourceJarMojo extends 
AbstractMojo {
             if (attach) {
                 boolean requiresAttach = true;
                 for (Artifact attachedArtifact : 
project.getAttachedArtifacts()) {
-                    Artifact previouslyAttached = 
getPreviouslyAttached(attachedArtifact, project, getClassifier());
-                    if (previouslyAttached != null) {
-                        if (!outputFile.equals(previouslyAttached.getFile())) {
-                            getLog().error("Artifact already attached to a 
file " + previouslyAttached.getFile()
-                                    + ": attach to " + outputFile + " should 
be done with another classifier");
+                    Artifact previouslyAttachedArtifact =
+                            getPreviouslyAttached(attachedArtifact, project, 
getClassifier());
+                    if (previouslyAttachedArtifact != null) {
+                        File previouslyAttachedFile = 
previouslyAttachedArtifact.getFile();
+                        // trying to attache the same file/path or not?
+                        if (!outputFile.equals(previouslyAttachedFile)) {
+                            getLog().error("Artifact " + 
previouslyAttachedArtifact.getId()
+                                    + " already attached to a file " + 
relative(previouslyAttachedFile) + ": attach to "
+                                    + relative(outputFile) + " should be done 
with another classifier");
                             throw new MojoExecutionException("Presumably you 
have configured maven-source-plugin "
                                     + "to execute twice in your build to 
different output files. "
                                     + "You have to configure a classifier for 
at least one of them.");
                         }
                         requiresAttach = false;
-                        getLog().info("Artifact already attached: ignoring 
re-attach");
+                        getLog().info("Artifact " + 
previouslyAttachedArtifact.getId() + " already attached to "
+                                + relative(outputFile) + ": ignoring same 
re-attach (same artifact, same file)");
                     }
                 }
                 if (requiresAttach) {
@@ -330,6 +336,11 @@ public abstract class AbstractSourceJarMojo extends 
AbstractMojo {
         }
     }
 
+    private String relative(File to) {
+        Path basedir = project.getBasedir().getAbsoluteFile().toPath();
+        return basedir.relativize(to.getAbsoluteFile().toPath()).toString();
+    }
+
     private Artifact getPreviouslyAttached(Artifact artifact, MavenProject 
checkProject, String classifier) {
         return artifact.getType().equals(getType())
                         && 
artifact.getGroupId().equals(checkProject.getGroupId())

Reply via email to