Skip to content

Commit 5e31b99

Browse files
[MJAR-296] Allow including files excluded by default. (#67)
* [MJAR-296] Allow including files excluded by default. * Improve implementation * Fix ITs * fix docs after review --------- Co-authored-by: Slawomir Jaranowski <s.jaranowski@gmail.com>
1 parent ddfb635 commit 5e31b99

File tree

9 files changed

+347
-1
lines changed

9 files changed

+347
-1
lines changed
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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>mjar-296-exclude-default</artifactId>
27+
<name>mjar-296-suppress-default-excludes</name>
28+
<description>Verifies that the resulting jar not includes files excluded by default</description>
29+
<packaging>jar</packaging>
30+
<version>1.0-SNAPSHOT</version>
31+
32+
<properties>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
<project.build.outputTimestamp>2023-11-19T13:25:58Z</project.build.outputTimestamp>
35+
</properties>
36+
37+
<build>
38+
<pluginManagement>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-resources-plugin</artifactId>
43+
<version>@version.maven-resources-plugin@</version>
44+
<configuration>
45+
<addDefaultExcludes>false</addDefaultExcludes>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</pluginManagement>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-jar-plugin</artifactId>
54+
<version>@project.version@</version>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.nio.file.Files
2+
import java.nio.file.Paths
3+
4+
/*
5+
* Licensed to the Apache Software Foundation (ASF) under one
6+
* or more contributor license agreements. See the NOTICE file
7+
* distributed with this work for additional information
8+
* regarding copyright ownership. The ASF licenses this file
9+
* to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance
11+
* with the License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing,
16+
* software distributed under the License is distributed on an
17+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18+
* KIND, either express or implied. See the License for the
19+
* specific language governing permissions and limitations
20+
* under the License.
21+
*/
22+
23+
// excluded files are not copied by m-invoker-p - so we need create one
24+
25+
def resDir = basedir.toPath().resolve("src/main/resources")
26+
Files.createDirectories(resDir)
27+
Files.createFile(resDir.resolve(".cvsignore"))
28+
29+
return true
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
import java.util.jar.*;
21+
22+
def target = new File(basedir, "target")
23+
24+
def cvsignore = new File(target, "classes/.cvsignore")
25+
assert cvsignore.exists()
26+
27+
def artifact = new File(target, "mjar-296-exclude-default-1.0-SNAPSHOT.jar")
28+
assert artifact.exists()
29+
30+
def jar = new JarFile(artifact)
31+
assert ".cvsignore" !in jar.entries()*.name
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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>mjar-296-suppress-default-excludes</artifactId>
27+
<name>mjar-296-suppress-default-excludes</name>
28+
<description>Verifies that the resulting jar includes files excluded by default</description>
29+
<packaging>jar</packaging>
30+
<version>1.0-SNAPSHOT</version>
31+
32+
<properties>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
<project.build.outputTimestamp>2023-11-19T13:25:58Z</project.build.outputTimestamp>
35+
</properties>
36+
37+
<build>
38+
<pluginManagement>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-resources-plugin</artifactId>
43+
<version>@version.maven-resources-plugin@</version>
44+
<configuration>
45+
<addDefaultExcludes>false</addDefaultExcludes>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</pluginManagement>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-jar-plugin</artifactId>
54+
<version>@project.version@</version>
55+
<configuration>
56+
<addDefaultExcludes>false</addDefaultExcludes>
57+
</configuration>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.nio.file.Files
2+
import java.nio.file.Path
3+
import java.nio.file.Paths
4+
5+
/*
6+
* Licensed to the Apache Software Foundation (ASF) under one
7+
* or more contributor license agreements. See the NOTICE file
8+
* distributed with this work for additional information
9+
* regarding copyright ownership. The ASF licenses this file
10+
* to you under the Apache License, Version 2.0 (the
11+
* "License"); you may not use this file except in compliance
12+
* with the License. You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing,
17+
* software distributed under the License is distributed on an
18+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19+
* KIND, either express or implied. See the License for the
20+
* specific language governing permissions and limitations
21+
* under the License.
22+
*/
23+
24+
// excluded files are not copied by m-invoker-p - so we need create one
25+
def resDir = basedir.toPath().resolve("src/main/resources")
26+
Files.createDirectories(resDir)
27+
Files.createFile(resDir.resolve(".cvsignore"))
28+
29+
return true
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
import java.util.jar.*;
21+
22+
def target = new File(basedir, "target")
23+
24+
def cvsignore = new File(target, "classes/.cvsignore")
25+
assert cvsignore.exists()
26+
27+
def artifact = new File(target, "mjar-296-suppress-default-excludes-1.0-SNAPSHOT.jar")
28+
assert artifact.exists()
29+
30+
def jar = new JarFile(artifact)
31+
assert ".cvsignore" in jar.entries()*.name
32+
33+

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

+44-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.maven.toolchain.ToolchainManager;
3939
import org.codehaus.plexus.archiver.Archiver;
4040
import org.codehaus.plexus.archiver.jar.JarArchiver;
41+
import org.codehaus.plexus.archiver.util.DefaultFileSet;
4142

4243
/**
4344
* Base class for creating a jar from project classes.
@@ -169,6 +170,38 @@ public abstract class AbstractJarMojo extends AbstractMojo {
169170
@Parameter(property = "maven.jar.detectMultiReleaseJar", defaultValue = "true")
170171
private boolean detectMultiReleaseJar;
171172

173+
/**
174+
* If set to {@code false}, the files and directories that by default are excluded from the resulting archive,
175+
* like {@code .gitignore}, {@code .cvsignore} etc. will be included.
176+
* This means all files like the following will be included.
177+
* <ul>
178+
* <li>Misc: &#42;&#42;/&#42;~, &#42;&#42;/#&#42;#, &#42;&#42;/.#&#42;, &#42;&#42;/%&#42;%, &#42;&#42;/._&#42;</li>
179+
* <li>CVS: &#42;&#42;/CVS, &#42;&#42;/CVS/&#42;&#42;, &#42;&#42;/.cvsignore</li>
180+
* <li>RCS: &#42;&#42;/RCS, &#42;&#42;/RCS/&#42;&#42;</li>
181+
* <li>SCCS: &#42;&#42;/SCCS, &#42;&#42;/SCCS/&#42;&#42;</li>
182+
* <li>VSSercer: &#42;&#42;/vssver.scc</li>
183+
* <li>MKS: &#42;&#42;/project.pj</li>
184+
* <li>SVN: &#42;&#42;/.svn, &#42;&#42;/.svn/&#42;&#42;</li>
185+
* <li>GNU: &#42;&#42;/.arch-ids, &#42;&#42;/.arch-ids/&#42;&#42;</li>
186+
* <li>Bazaar: &#42;&#42;/.bzr, &#42;&#42;/.bzr/&#42;&#42;</li>
187+
* <li>SurroundSCM: &#42;&#42;/.MySCMServerInfo</li>
188+
* <li>Mac: &#42;&#42;/.DS_Store</li>
189+
* <li>Serena Dimension: &#42;&#42;/.metadata, &#42;&#42;/.metadata/&#42;&#42;</li>
190+
* <li>Mercurial: &#42;&#42;/.hg, &#42;&#42;/.hg/&#42;&#42;</li>
191+
* <li>Git: &#42;&#42;/.git, &#42;&#42;/.git/&#42;&#42;</li>
192+
* <li>Bitkeeper: &#42;&#42;/BitKeeper, &#42;&#42;/BitKeeper/&#42;&#42;, &#42;&#42;/ChangeSet,
193+
* &#42;&#42;/ChangeSet/&#42;&#42;</li>
194+
* <li>Darcs: &#42;&#42;/_darcs, &#42;&#42;/_darcs/&#42;&#42;, &#42;&#42;/.darcsrepo,
195+
* &#42;&#42;/.darcsrepo/&#42;&#42;&#42;&#42;/-darcs-backup&#42;, &#42;&#42;/.darcs-temp-mail
196+
* </ul>
197+
*
198+
* @see <a href="https://codehaus-plexus.github.io/plexus-utils/apidocs/org/codehaus/plexus/util/AbstractScanner.html#DEFAULTEXCLUDES">DEFAULTEXCLUDES</a>
199+
*
200+
* @since 3.4.0
201+
*/
202+
@Parameter(defaultValue = "true")
203+
private boolean addDefaultExcludes;
204+
172205
/**
173206
* Return the specific output directory to serve as the root for the archive.
174207
* @return get classes directory.
@@ -281,7 +314,7 @@ public File createArchive() throws MojoExecutionException {
281314
getLog().warn("JAR will be empty - no content was marked for inclusion!");
282315
}
283316
} else {
284-
archiver.getArchiver().addDirectory(contentDirectory, getIncludes(), getExcludes());
317+
archiver.getArchiver().addFileSet(getFileSet(contentDirectory));
285318
}
286319

287320
archiver.createArchive(session, project, archive);
@@ -353,4 +386,14 @@ private String[] getExcludes() {
353386
}
354387
return DEFAULT_EXCLUDES;
355388
}
389+
390+
private org.codehaus.plexus.archiver.FileSet getFileSet(File contentDirectory) {
391+
DefaultFileSet fileSet = DefaultFileSet.fileSet(contentDirectory)
392+
.prefixed("")
393+
.includeExclude(getIncludes(), getExcludes())
394+
.includeEmptyDirs(true);
395+
396+
fileSet.setUsingDefaultExcludes(addDefaultExcludes);
397+
return fileSet;
398+
}
356399
}

0 commit comments

Comments
 (0)