Skip to content

Commit 49bb464

Browse files
authored
Add new "attach" configuration parameter (#483)
This matches the `attach` parameter from the source plugin Setting this parameter to `false` will skip attaching the generated jar artifact to the build. It will not be picked up by the install and deploy goals. Omitting or setting this parameter to `true` will attach the jar to the build. This is the default behavior. The use case for this is generating test jars as part of a build to be used in integration tests while they should not actually be deployed to a repository.
1 parent 6f0ed8c commit 49bb464

6 files changed

Lines changed: 195 additions & 7 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
invoker.java.version = 10+
18+
19+
invoker.name = Test Jar Attachment
20+
invoker.description = Verifies that setting the attach property to false will not install it in the local repository
21+
invoker.goals = clean install

src/it/test-jar-attach/pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>test-jar-attach</artifactId>
27+
<name>test-jar-attach</name>
28+
<description>Verifies that setting the attach property to false will not install it in the local repository
29+
</description>
30+
<packaging>jar</packaging>
31+
<version>1.0-SNAPSHOT</version>
32+
33+
<properties>
34+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35+
<project.build.outputTimestamp>2023-11-19T13:25:58Z</project.build.outputTimestamp>
36+
</properties>
37+
38+
<build>
39+
<pluginManagement>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-install-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</pluginManagement>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-jar-plugin</artifactId>
51+
<version>@project.version@</version>
52+
<configuration>
53+
<skipIfEmpty>true</skipIfEmpty>
54+
</configuration>
55+
<executions>
56+
<execution>
57+
<id>default-test</id>
58+
<phase>package</phase>
59+
<goals>
60+
<goal>test-jar</goal>
61+
</goals>
62+
<configuration>
63+
<attach>false</attach>
64+
</configuration>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
21+
/**
22+
* Hello world!
23+
*
24+
*/
25+
public class Foo
26+
{
27+
public static void main( String[] args )
28+
{
29+
System.out.println( "Hello World!" );
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
21+
/**
22+
* Hello world!
23+
*
24+
*/
25+
public class Foo
26+
{
27+
public static void main( String[] args )
28+
{
29+
System.out.println( "Hello World!" );
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
def localRepo = basedir.getParentFile().getParentFile().getParentFile().getPath() + "/target/local-repo";
21+
22+
def artifact = new File(localRepo, "org/apache/maven/plugins/test-jar-attach/1.0-SNAPSHOT/test-jar-attach-1.0-SNAPSHOT-tests.jar")
23+
assert !artifact.exists()

src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ public abstract class AbstractJarMojo extends AbstractMojo {
202202
@Parameter(defaultValue = "true")
203203
private boolean addDefaultExcludes;
204204

205+
/**
206+
* Specifies whether to attach the jar to the project
207+
*
208+
* @since 3.5.0
209+
*/
210+
@Parameter(property = "maven.jar.attach", defaultValue = "true")
211+
protected boolean attach;
212+
205213
/**
206214
* Return the specific output directory to serve as the root for the archive.
207215
* @return get classes directory.
@@ -344,14 +352,18 @@ public void execute() throws MojoExecutionException {
344352
} else {
345353
File jarFile = createArchive();
346354

347-
if (hasClassifier()) {
348-
projectHelper.attachArtifact(getProject(), getType(), getClassifier(), jarFile);
349-
} else {
350-
if (projectHasAlreadySetAnArtifact()) {
351-
throw new MojoExecutionException("You have to use a classifier "
352-
+ "to attach supplemental artifacts to the project instead of replacing them.");
355+
if (attach) {
356+
if (hasClassifier()) {
357+
projectHelper.attachArtifact(getProject(), getType(), getClassifier(), jarFile);
358+
} else {
359+
if (projectHasAlreadySetAnArtifact()) {
360+
throw new MojoExecutionException("You have to use a classifier "
361+
+ "to attach supplemental artifacts to the project instead of replacing them.");
362+
}
363+
getProject().getArtifact().setFile(jarFile);
353364
}
354-
getProject().getArtifact().setFile(jarFile);
365+
} else {
366+
getLog().debug("Skipping attachment of the " + getType() + " artifact to the project.");
355367
}
356368
}
357369
}

0 commit comments

Comments
 (0)