Skip to content

Commit

Permalink
[MSOURCES-140] improve message when lifecycle run twice
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Apr 25, 2024
1 parent 7c92bd7 commit 53dc0f2
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/it/MSOURCES-121/invoker.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions src/it/MSOURCES-121/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
<goals>
<goal>jar-no-fork</goal>
</goals>
<configuration>
<finalName>${project.build.finalName}-secondary</finalName>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
22 changes: 22 additions & 0 deletions src/it/MSOURCES-121/verify.groovy
Original file line number Diff line number Diff line change
@@ -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/
18 changes: 18 additions & 0 deletions src/it/MSOURCES-140/invoker.properties
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions src/it/MSOURCES-140/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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 xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.sources</groupId>
<artifactId>jar-no-fork</artifactId>
<version>1.0-SNAPSHOT</version>

<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>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
23 changes: 23 additions & 0 deletions src/it/MSOURCES-140/src/main/java/MyClass.java
Original file line number Diff line number Diff line change
@@ -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
{

}
17 changes: 17 additions & 0 deletions src/it/MSOURCES-140/src/main/resources/main.properties
Original file line number Diff line number Diff line change
@@ -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.

23 changes: 23 additions & 0 deletions src/it/MSOURCES-140/src/test/java/MyTest.java
Original file line number Diff line number Diff line change
@@ -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
{

}
17 changes: 17 additions & 0 deletions src/it/MSOURCES-140/src/test/resources/test.properties
Original file line number Diff line number Diff line change
@@ -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.

22 changes: 22 additions & 0 deletions src/it/MSOURCES-140/verify.groovy
Original file line number Diff line number Diff line change
@@ -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\)/
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -306,17 +307,22 @@ protected void packageSources(List<MavenProject> 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) {
Expand All @@ -330,6 +336,11 @@ protected void packageSources(List<MavenProject> 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())
Expand Down

0 comments on commit 53dc0f2

Please sign in to comment.