Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSOURCES-140] fail only if re-attach different files #24

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/it/MSOURCES-121/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@

<name>Test for multiple attachments of files</name>
<description>This build should fail based on the duplicate
execution with the same configuration. This will errornously
add the classifier/file twice times.
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>

<properties>
Expand All @@ -56,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,10 +20,12 @@

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;
import java.util.List;
import java.util.Objects;

import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
Expand Down Expand Up @@ -303,15 +305,29 @@ protected void packageSources(List<MavenProject> theProjects) throws MojoExecuti
}

if (attach) {
boolean requiresAttach = true;
for (Artifact attachedArtifact : project.getAttachedArtifacts()) {
if (isAlreadyAttached(attachedArtifact, project, getClassifier())) {
getLog().error("We have duplicated artifacts attached.");
throw new MojoExecutionException("Presumably you have configured maven-source-plugin "
+ "to execute twice in your build. You have to configure a classifier "
+ "for at least one of them.");
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 " + previouslyAttachedArtifact.getId() + " already attached to "
+ relative(outputFile) + ": ignoring same re-attach (same artifact, same file)");
}
}
projectHelper.attachArtifact(project, getType(), getClassifier(), outputFile);
if (requiresAttach) {
projectHelper.attachArtifact(project, getType(), getClassifier(), outputFile);
}
} else {
getLog().info("NOT adding java-sources to attached artifacts list.");
}
Expand All @@ -320,12 +336,19 @@ protected void packageSources(List<MavenProject> theProjects) throws MojoExecuti
}
}

private boolean isAlreadyAttached(Artifact artifact, MavenProject checkProject, String classifier) {
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())
&& artifact.getArtifactId().equals(checkProject.getArtifactId())
&& artifact.getVersion().equals(checkProject.getVersion())
&& (artifact.getClassifier() != null ? artifact.getClassifier().equals(classifier) : false);
&& artifact.getGroupId().equals(checkProject.getGroupId())
&& artifact.getArtifactId().equals(checkProject.getArtifactId())
&& artifact.getVersion().equals(checkProject.getVersion())
&& Objects.equals(artifact.getClassifier(), classifier)
? artifact
: null;
}

/**
Expand Down