Skip to content

Commit

Permalink
[MJAR-296] Allow including files excluded by default. (#67)
Browse files Browse the repository at this point in the history
* [MJAR-296] Allow including files excluded by default.

* Improve implementation

* Fix ITs

* fix docs after review

---------

Co-authored-by: Slawomir Jaranowski <[email protected]>
  • Loading branch information
redzi and slawekjaranowski authored Apr 9, 2024
1 parent ddfb635 commit 5e31b99
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 1 deletion.
58 changes: 58 additions & 0 deletions src/it/MJAR-296-exclude-default/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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-exclude-default</artifactId>
<name>mjar-296-suppress-default-excludes</name>
<description>Verifies that the resulting jar not 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>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>@version.maven-resources-plugin@</version>
<configuration>
<addDefaultExcludes>false</addDefaultExcludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>@project.version@</version>
</plugin>
</plugins>
</build>
</project>
29 changes: 29 additions & 0 deletions src/it/MJAR-296-exclude-default/setup.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.nio.file.Files
import java.nio.file.Paths

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

// excluded files are not copied by m-invoker-p - so we need create one

def resDir = basedir.toPath().resolve("src/main/resources")
Files.createDirectories(resDir)
Files.createFile(resDir.resolve(".cvsignore"))

return true
31 changes: 31 additions & 0 deletions src/it/MJAR-296-exclude-default/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!" );
}
}
31 changes: 31 additions & 0 deletions src/it/MJAR-296-exclude-default/verify.groovy
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.
*/

import java.util.jar.*;

def target = new File(basedir, "target")

def cvsignore = new File(target, "classes/.cvsignore")
assert cvsignore.exists()

def artifact = new File(target, "mjar-296-exclude-default-1.0-SNAPSHOT.jar")
assert artifact.exists()

def jar = new JarFile(artifact)
assert ".cvsignore" !in jar.entries()*.name
61 changes: 61 additions & 0 deletions src/it/MJAR-296-suppress-default-excludes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>@version.maven-resources-plugin@</version>
<configuration>
<addDefaultExcludes>false</addDefaultExcludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<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>
29 changes: 29 additions & 0 deletions src/it/MJAR-296-suppress-default-excludes/setup.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

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

// excluded files are not copied by m-invoker-p - so we need create one
def resDir = basedir.toPath().resolve("src/main/resources")
Files.createDirectories(resDir)
Files.createFile(resDir.resolve(".cvsignore"))

return true
31 changes: 31 additions & 0 deletions src/it/MJAR-296-suppress-default-excludes/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!" );
}
}
33 changes: 33 additions & 0 deletions src/it/MJAR-296-suppress-default-excludes/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.*;

def target = new File(basedir, "target")

def cvsignore = new File(target, "classes/.cvsignore")
assert cvsignore.exists()

def artifact = new File(target, "mjar-296-suppress-default-excludes-1.0-SNAPSHOT.jar")
assert artifact.exists()

def jar = new JarFile(artifact)
assert ".cvsignore" in jar.entries()*.name


45 changes: 44 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 @@ -38,6 +38,7 @@
import org.apache.maven.toolchain.ToolchainManager;
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 @@ -169,6 +170,38 @@ public abstract class AbstractJarMojo extends AbstractMojo {
@Parameter(property = "maven.jar.detectMultiReleaseJar", defaultValue = "true")
private boolean detectMultiReleaseJar;

/**
* If set to {@code false}, the files and directories 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>
*
* @see <a href="https://codehaus-plexus.github.io/plexus-utils/apidocs/org/codehaus/plexus/util/AbstractScanner.html#DEFAULTEXCLUDES">DEFAULTEXCLUDES</a>
*
* @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 @@ -281,7 +314,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 @@ -353,4 +386,14 @@ private String[] getExcludes() {
}
return DEFAULT_EXCLUDES;
}

private org.codehaus.plexus.archiver.FileSet getFileSet(File contentDirectory) {
DefaultFileSet fileSet = DefaultFileSet.fileSet(contentDirectory)
.prefixed("")
.includeExclude(getIncludes(), getExcludes())
.includeEmptyDirs(true);

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

0 comments on commit 5e31b99

Please sign in to comment.