From 53dc0f298ae75a2f78ca8bc437165dd6a35c64af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Thu, 28 Mar 2024 12:26:43 +0100 Subject: [PATCH] [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 ++++++++ src/it/MSOURCES-140/invoker.properties | 18 ++++++ src/it/MSOURCES-140/pom.xml | 55 +++++++++++++++++++ .../MSOURCES-140/src/main/java/MyClass.java | 23 ++++++++ .../src/main/resources/main.properties | 17 ++++++ src/it/MSOURCES-140/src/test/java/MyTest.java | 23 ++++++++ .../src/test/resources/test.properties | 17 ++++++ src/it/MSOURCES-140/verify.groovy | 22 ++++++++ .../plugins/source/AbstractSourceJarMojo.java | 23 ++++++-- 11 files changed, 218 insertions(+), 7 deletions(-) create mode 100644 src/it/MSOURCES-121/verify.groovy create mode 100644 src/it/MSOURCES-140/invoker.properties create mode 100644 src/it/MSOURCES-140/pom.xml create mode 100644 src/it/MSOURCES-140/src/main/java/MyClass.java create mode 100644 src/it/MSOURCES-140/src/main/resources/main.properties create mode 100644 src/it/MSOURCES-140/src/test/java/MyTest.java create mode 100644 src/it/MSOURCES-140/src/test/resources/test.properties create mode 100644 src/it/MSOURCES-140/verify.groovy 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 @@ jar-no-fork + + ${project.build.finalName}-secondary + 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-140/invoker.properties b/src/it/MSOURCES-140/invoker.properties new file mode 100644 index 0000000..54abe94 --- /dev/null +++ b/src/it/MSOURCES-140/invoker.properties @@ -0,0 +1,18 @@ +# 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. +invoker.buildResult = success +invoker.goals = package clean install diff --git a/src/it/MSOURCES-140/pom.xml b/src/it/MSOURCES-140/pom.xml new file mode 100644 index 0000000..c22d234 --- /dev/null +++ b/src/it/MSOURCES-140/pom.xml @@ -0,0 +1,55 @@ + + + + + 4.0.0 + + org.apache.maven.its.sources + jar-no-fork + 1.0-SNAPSHOT + + Test for multiple attachments of one file caused by multiple runs in one session + mvn package clean install should not fail + + + UTF-8 + + + + + + org.apache.maven.plugins + maven-source-plugin + @project.version@ + + + attach-sources + + jar-no-fork + + + + + + + + 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-140/src/main/resources/main.properties b/src/it/MSOURCES-140/src/main/resources/main.properties new file mode 100644 index 0000000..00ae6c0 --- /dev/null +++ b/src/it/MSOURCES-140/src/main/resources/main.properties @@ -0,0 +1,17 @@ +# 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. + 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-140/src/test/resources/test.properties b/src/it/MSOURCES-140/src/test/resources/test.properties new file mode 100644 index 0000000..00ae6c0 --- /dev/null +++ b/src/it/MSOURCES-140/src/test/resources/test.properties @@ -0,0 +1,17 @@ +# 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. + 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 @@ 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 @@ protected void packageSources(List theProjects) throws MojoExecuti 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 @@ protected void packageSources(List theProjects) throws MojoExecuti } } + 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())