Skip to content

Commit f811a85

Browse files
committed
[MJAR-296] Allow including files excluded by default.
1 parent 0cbcfd4 commit f811a85

5 files changed

Lines changed: 181 additions & 1 deletion

File tree

src/it/MJAR-296/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-jar-plugin</artifactId>
42+
<version>@project.version@</version>
43+
<configuration>
44+
<addDefaultExcludes>false</addDefaultExcludes>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
</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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# By default the file is excluded

src/it/MJAR-296/verify.groovy

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
import java.util.jar.*;
22+
23+
boolean result = false;
24+
25+
try
26+
{
27+
File target = new File( basedir, "target" )
28+
if ( !target.exists() || !target.isDirectory() ) {
29+
System.err.println( "target file is missing or not a directory." )
30+
return result
31+
}
32+
33+
File artifact = new File( target, "mjar-296-suppress-default-excludes-1.0-SNAPSHOT.jar" )
34+
if ( !artifact.exists() || artifact.isDirectory() ) {
35+
System.err.println( "artifact file is missing or a directory." )
36+
return result
37+
}
38+
39+
String expectedFileName = ".gitignore"
40+
41+
JarFile jar = new JarFile( artifact )
42+
Enumeration jarEntries = jar.entries()
43+
while ( jarEntries.hasMoreElements() ) {
44+
JarEntry entry = (JarEntry) jarEntries.nextElement();
45+
if ( !entry.isDirectory() && expectedFileName.equals(entry.getName()) ) {
46+
result = true
47+
}
48+
}
49+
50+
if ( !result ) {
51+
System.err.println( "Expected file[" + expectedFileName + "] not found in the jar archive." )
52+
}
53+
} catch( Throwable e ) {
54+
e.printStackTrace()
55+
result = false
56+
}
57+
58+
return result

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.maven.shared.model.fileset.util.FileSetManager;
3737
import org.codehaus.plexus.archiver.Archiver;
3838
import org.codehaus.plexus.archiver.jar.JarArchiver;
39+
import org.codehaus.plexus.archiver.util.DefaultFileSet;
3940

4041
/**
4142
* Base class for creating a jar from project classes.
@@ -161,6 +162,36 @@ public abstract class AbstractJarMojo extends AbstractMojo {
161162
@Parameter(property = "maven.jar.detectMultiReleaseJar", defaultValue = "true")
162163
private boolean detectMultiReleaseJar;
163164

165+
/**
166+
* If set to {@code false}, the files that by default are excluded from the resulting archive,
167+
* like {@code .gitignore}, {@code .cvsignore} etc. will be included.
168+
* This means all files like the following will be included.
169+
* <ul>
170+
* <li>Misc: &#42;&#42;/&#42;~, &#42;&#42;/#&#42;#, &#42;&#42;/.#&#42;, &#42;&#42;/%&#42;%, &#42;&#42;/._&#42;</li>
171+
* <li>CVS: &#42;&#42;/CVS, &#42;&#42;/CVS/&#42;&#42;, &#42;&#42;/.cvsignore</li>
172+
* <li>RCS: &#42;&#42;/RCS, &#42;&#42;/RCS/&#42;&#42;</li>
173+
* <li>SCCS: &#42;&#42;/SCCS, &#42;&#42;/SCCS/&#42;&#42;</li>
174+
* <li>VSSercer: &#42;&#42;/vssver.scc</li>
175+
* <li>MKS: &#42;&#42;/project.pj</li>
176+
* <li>SVN: &#42;&#42;/.svn, &#42;&#42;/.svn/&#42;&#42;</li>
177+
* <li>GNU: &#42;&#42;/.arch-ids, &#42;&#42;/.arch-ids/&#42;&#42;</li>
178+
* <li>Bazaar: &#42;&#42;/.bzr, &#42;&#42;/.bzr/&#42;&#42;</li>
179+
* <li>SurroundSCM: &#42;&#42;/.MySCMServerInfo</li>
180+
* <li>Mac: &#42;&#42;/.DS_Store</li>
181+
* <li>Serena Dimension: &#42;&#42;/.metadata, &#42;&#42;/.metadata/&#42;&#42;</li>
182+
* <li>Mercurial: &#42;&#42;/.hg, &#42;&#42;/.hg/&#42;&#42;</li>
183+
* <li>Git: &#42;&#42;/.git, &#42;&#42;/.git/&#42;&#42;</li>
184+
* <li>Bitkeeper: &#42;&#42;/BitKeeper, &#42;&#42;/BitKeeper/&#42;&#42;, &#42;&#42;/ChangeSet,
185+
* &#42;&#42;/ChangeSet/&#42;&#42;</li>
186+
* <li>Darcs: &#42;&#42;/_darcs, &#42;&#42;/_darcs/&#42;&#42;, &#42;&#42;/.darcsrepo,
187+
* &#42;&#42;/.darcsrepo/&#42;&#42;&#42;&#42;/-darcs-backup&#42;, &#42;&#42;/.darcs-temp-mail
188+
* </ul>
189+
*
190+
* @since 3.4.0
191+
*/
192+
@Parameter(defaultValue = "true")
193+
private boolean addDefaultExcludes;
194+
164195
/**
165196
* Return the specific output directory to serve as the root for the archive.
166197
* @return get classes directory.
@@ -222,6 +253,7 @@ public File createArchive() throws MojoExecutionException {
222253
jarContentFileSet.setDirectory(getClassesDirectory().getAbsolutePath());
223254
jarContentFileSet.setIncludes(Arrays.asList(getIncludes()));
224255
jarContentFileSet.setExcludes(Arrays.asList(getExcludes()));
256+
jarContentFileSet.setUseDefaultExcludes(addDefaultExcludes);
225257

226258
String[] includedFiles = fileSetManager.getIncludedFiles(jarContentFileSet);
227259

@@ -262,7 +294,7 @@ public File createArchive() throws MojoExecutionException {
262294
getLog().warn("JAR will be empty - no content was marked for inclusion!");
263295
}
264296
} else {
265-
archiver.getArchiver().addDirectory(contentDirectory, getIncludes(), getExcludes());
297+
archiver.getArchiver().addFileSet(getFileSet(contentDirectory));
266298
}
267299

268300
archiver.createArchive(session, project, archive);
@@ -334,4 +366,13 @@ private String[] getExcludes() {
334366
}
335367
return DEFAULT_EXCLUDES;
336368
}
369+
370+
private org.codehaus.plexus.archiver.FileSet getFileSet(File contentDirectory) {
371+
DefaultFileSet fileSet = DefaultFileSet.fileSet(contentDirectory)
372+
.includeExclude(getIncludes(), getExcludes())
373+
.includeEmptyDirs(true);
374+
375+
fileSet.setUsingDefaultExcludes(false);
376+
return fileSet;
377+
}
337378
}

0 commit comments

Comments
 (0)