From 5e31b9933a68a44b180a499636f7f43c8521648c Mon Sep 17 00:00:00 2001 From: redzi Date: Tue, 9 Apr 2024 23:00:14 +0200 Subject: [PATCH] [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 --- src/it/MJAR-296-exclude-default/pom.xml | 58 ++++++++++++++++++ src/it/MJAR-296-exclude-default/setup.groovy | 29 +++++++++ .../src/main/java/Foo.java | 31 ++++++++++ src/it/MJAR-296-exclude-default/verify.groovy | 31 ++++++++++ .../pom.xml | 61 +++++++++++++++++++ .../setup.groovy | 29 +++++++++ .../src/main/java/Foo.java | 31 ++++++++++ .../verify.groovy | 33 ++++++++++ .../maven/plugins/jar/AbstractJarMojo.java | 45 +++++++++++++- 9 files changed, 347 insertions(+), 1 deletion(-) create mode 100644 src/it/MJAR-296-exclude-default/pom.xml create mode 100644 src/it/MJAR-296-exclude-default/setup.groovy create mode 100644 src/it/MJAR-296-exclude-default/src/main/java/Foo.java create mode 100644 src/it/MJAR-296-exclude-default/verify.groovy create mode 100644 src/it/MJAR-296-suppress-default-excludes/pom.xml create mode 100644 src/it/MJAR-296-suppress-default-excludes/setup.groovy create mode 100644 src/it/MJAR-296-suppress-default-excludes/src/main/java/Foo.java create mode 100644 src/it/MJAR-296-suppress-default-excludes/verify.groovy diff --git a/src/it/MJAR-296-exclude-default/pom.xml b/src/it/MJAR-296-exclude-default/pom.xml new file mode 100644 index 0000000..1cdf068 --- /dev/null +++ b/src/it/MJAR-296-exclude-default/pom.xml @@ -0,0 +1,58 @@ + + + + 4.0.0 + + org.apache.maven.plugins + mjar-296-exclude-default + mjar-296-suppress-default-excludes + Verifies that the resulting jar not includes files excluded by default + jar + 1.0-SNAPSHOT + + + UTF-8 + 2023-11-19T13:25:58Z + + + + + + + org.apache.maven.plugins + maven-resources-plugin + @version.maven-resources-plugin@ + + false + + + + + + + org.apache.maven.plugins + maven-jar-plugin + @project.version@ + + + + diff --git a/src/it/MJAR-296-exclude-default/setup.groovy b/src/it/MJAR-296-exclude-default/setup.groovy new file mode 100644 index 0000000..ab30042 --- /dev/null +++ b/src/it/MJAR-296-exclude-default/setup.groovy @@ -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 diff --git a/src/it/MJAR-296-exclude-default/src/main/java/Foo.java b/src/it/MJAR-296-exclude-default/src/main/java/Foo.java new file mode 100644 index 0000000..f939fa6 --- /dev/null +++ b/src/it/MJAR-296-exclude-default/src/main/java/Foo.java @@ -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!" ); + } +} diff --git a/src/it/MJAR-296-exclude-default/verify.groovy b/src/it/MJAR-296-exclude-default/verify.groovy new file mode 100644 index 0000000..9599b98 --- /dev/null +++ b/src/it/MJAR-296-exclude-default/verify.groovy @@ -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 diff --git a/src/it/MJAR-296-suppress-default-excludes/pom.xml b/src/it/MJAR-296-suppress-default-excludes/pom.xml new file mode 100644 index 0000000..9ff5b45 --- /dev/null +++ b/src/it/MJAR-296-suppress-default-excludes/pom.xml @@ -0,0 +1,61 @@ + + + + 4.0.0 + + org.apache.maven.plugins + mjar-296-suppress-default-excludes + mjar-296-suppress-default-excludes + Verifies that the resulting jar includes files excluded by default + jar + 1.0-SNAPSHOT + + + UTF-8 + 2023-11-19T13:25:58Z + + + + + + + org.apache.maven.plugins + maven-resources-plugin + @version.maven-resources-plugin@ + + false + + + + + + + org.apache.maven.plugins + maven-jar-plugin + @project.version@ + + false + + + + + diff --git a/src/it/MJAR-296-suppress-default-excludes/setup.groovy b/src/it/MJAR-296-suppress-default-excludes/setup.groovy new file mode 100644 index 0000000..324bf2b --- /dev/null +++ b/src/it/MJAR-296-suppress-default-excludes/setup.groovy @@ -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 diff --git a/src/it/MJAR-296-suppress-default-excludes/src/main/java/Foo.java b/src/it/MJAR-296-suppress-default-excludes/src/main/java/Foo.java new file mode 100644 index 0000000..f939fa6 --- /dev/null +++ b/src/it/MJAR-296-suppress-default-excludes/src/main/java/Foo.java @@ -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!" ); + } +} diff --git a/src/it/MJAR-296-suppress-default-excludes/verify.groovy b/src/it/MJAR-296-suppress-default-excludes/verify.groovy new file mode 100644 index 0000000..806c90e --- /dev/null +++ b/src/it/MJAR-296-suppress-default-excludes/verify.groovy @@ -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 + + diff --git a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java index 47b8bde..9a2bc22 100644 --- a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java +++ b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java @@ -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. @@ -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. + * + * + * @see DEFAULTEXCLUDES + * + * @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. @@ -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); @@ -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; + } }