Skip to content

Commit

Permalink
[MJAR-296] Allow including files excluded by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
redzi committed Nov 19, 2023
1 parent 0cbcfd4 commit 4e85e6c
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 1 deletion.
49 changes: 49 additions & 0 deletions src/it/MJAR-296/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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.plugins</groupId>
<artifactId>mjar-296-suppress-default-excludes</artifactId>
<name>mjar-296-suppress-default-excludes</name>
<description>Verifies that the resulting jar includes files excluded by default</description>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputTimestamp>2023-11-19T13:25:58Z</project.build.outputTimestamp>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<addDefaultExcludes>false</addDefaultExcludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
31 changes: 31 additions & 0 deletions src/it/MJAR-296/src/main/java/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

/*
* 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.
*/

/**
* Hello world!
*
*/
public class Foo
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
1 change: 1 addition & 0 deletions src/it/MJAR-296/src/main/resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# By default the file is excluded
58 changes: 58 additions & 0 deletions src/it/MJAR-296/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

/*
* 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.
*/

import java.util.jar.*;

boolean result = false;

try
{
File target = new File( basedir, "target" )
if ( !target.exists() || !target.isDirectory() ) {
System.err.println( "target file is missing or not a directory." )
return result
}

File artifact = new File( target, "mjar-296-suppress-default-excludes-1.0-SNAPSHOT.jar" )
if ( !artifact.exists() || artifact.isDirectory() ) {
System.err.println( "artifact file is missing or a directory." )
return result
}

String expectedFileName = ".gitignore"

JarFile jar = new JarFile( artifact )
Enumeration jarEntries = jar.entries()
while ( jarEntries.hasMoreElements() ) {
JarEntry entry = (JarEntry) jarEntries.nextElement();
if ( !entry.isDirectory() && expectedFileName.equals(entry.getName()) ) {
result = true
}
}

if ( !result ) {
System.err.println( "Expected file[" + expectedFileName + "] not found in the jar archive." )
}
} catch( Throwable e ) {
e.printStackTrace()
result = false
}

return result
43 changes: 42 additions & 1 deletion src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.maven.shared.model.fileset.util.FileSetManager;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.util.DefaultFileSet;

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

/**
* If set to {@code false}, the files that by default are excluded from the resulting archive,
* like {@code .gitignore}, {@code .cvsignore} etc. will be included.
* This means all files like the following will be included.
* <ul>
* <li>Misc: &#42;&#42;/&#42;~, &#42;&#42;/#&#42;#, &#42;&#42;/.#&#42;, &#42;&#42;/%&#42;%, &#42;&#42;/._&#42;</li>
* <li>CVS: &#42;&#42;/CVS, &#42;&#42;/CVS/&#42;&#42;, &#42;&#42;/.cvsignore</li>
* <li>RCS: &#42;&#42;/RCS, &#42;&#42;/RCS/&#42;&#42;</li>
* <li>SCCS: &#42;&#42;/SCCS, &#42;&#42;/SCCS/&#42;&#42;</li>
* <li>VSSercer: &#42;&#42;/vssver.scc</li>
* <li>MKS: &#42;&#42;/project.pj</li>
* <li>SVN: &#42;&#42;/.svn, &#42;&#42;/.svn/&#42;&#42;</li>
* <li>GNU: &#42;&#42;/.arch-ids, &#42;&#42;/.arch-ids/&#42;&#42;</li>
* <li>Bazaar: &#42;&#42;/.bzr, &#42;&#42;/.bzr/&#42;&#42;</li>
* <li>SurroundSCM: &#42;&#42;/.MySCMServerInfo</li>
* <li>Mac: &#42;&#42;/.DS_Store</li>
* <li>Serena Dimension: &#42;&#42;/.metadata, &#42;&#42;/.metadata/&#42;&#42;</li>
* <li>Mercurial: &#42;&#42;/.hg, &#42;&#42;/.hg/&#42;&#42;</li>
* <li>Git: &#42;&#42;/.git, &#42;&#42;/.git/&#42;&#42;</li>
* <li>Bitkeeper: &#42;&#42;/BitKeeper, &#42;&#42;/BitKeeper/&#42;&#42;, &#42;&#42;/ChangeSet,
* &#42;&#42;/ChangeSet/&#42;&#42;</li>
* <li>Darcs: &#42;&#42;/_darcs, &#42;&#42;/_darcs/&#42;&#42;, &#42;&#42;/.darcsrepo,
* &#42;&#42;/.darcsrepo/&#42;&#42;&#42;&#42;/-darcs-backup&#42;, &#42;&#42;/.darcs-temp-mail
* </ul>
*
* @since 3.4.0
*/
@Parameter(defaultValue = "true")
private boolean addDefaultExcludes;

/**
* Return the specific output directory to serve as the root for the archive.
* @return get classes directory.
Expand Down Expand Up @@ -222,6 +253,7 @@ public File createArchive() throws MojoExecutionException {
jarContentFileSet.setDirectory(getClassesDirectory().getAbsolutePath());
jarContentFileSet.setIncludes(Arrays.asList(getIncludes()));
jarContentFileSet.setExcludes(Arrays.asList(getExcludes()));
jarContentFileSet.setUseDefaultExcludes(addDefaultExcludes);

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

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

archiver.createArchive(session, project, archive);
Expand Down Expand Up @@ -334,4 +366,13 @@ private String[] getExcludes() {
}
return DEFAULT_EXCLUDES;
}

private DefaultFileSet getFileSet(File contentDirectory) {
DefaultFileSet fileSet = DefaultFileSet.fileSet(contentDirectory)
.includeExclude(getIncludes(), getExcludes())
.includeEmptyDirs(true);

fileSet.setUsingDefaultExcludes(false);
return fileSet;
}
}

0 comments on commit 4e85e6c

Please sign in to comment.