desruisseaux commented on code in PR #508: URL: https://github.com/apache/maven-jar-plugin/pull/508#discussion_r3950091334
########## src/main/java/org/apache/maven/plugins/jar/ExcludedFiles.java: ########## @@ -0,0 +1,146 @@ +/* + * 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. + */ +package org.apache.maven.plugins.jar; + +import java.io.Closeable; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.util.List; + +/** + * A list of files to temporarily move outside the directory to package in a <abbr>JAR</abbr> archive. + * This class is needed when the {@link AbstractJarMojo} configuration has include or exclude filters. + * Excluded files are temporarily moved outside the directory to archive. + * We move these files for making possible to specify the whole directory to the {@code jar} tool. + * This approach is used instead of enumerating files in arguments given to the {@code jar} tool because + * such enumeration cannot contain directory entries (otherwise the whole directory would be included). + * Some software such as Spring applications component scan relies on the presence of directory entries. + */ +final class ExcludedFiles implements Closeable { + /** + * The paths of files or directories to temporarily move in another directory. + */ + private final Path[] original; + + /** + * The paths where files or directories were moved. + * For each index <var>i</var>, the original path of {@code moved[i]} was {@code original[i]}. + */ + private final Path[] moved; + + /** + * Temporary directory which will contain the {@code moved} files. + * It should be a parent directory of {@link #original} files for + * increasing the chances that it is on the same file system. + */ + private final Path temporaryDirectory; + + /** + * Index of the first path which is a directory instead of a file. + * All paths before this index in the {@link #original} and {@link #moved} arrays are files. + * All paths at this index and after this index are directories. + */ + private final int indexOfFirstDirectory; + + /** + * Creates a new list of files to move in a temporary directory. + * + * @param directory the directory which was scanned for files to include in the <abbr>JAR</abbr> + * @param excludedFiles paths of files to temporarily move in another directory + * @param excludedDirectories paths of directories to temporarily move in another directory + * @throws IOException if an error occurred while creating the temporary directory. + */ + ExcludedFiles(Path directory, List<Path> excludedFiles, List<Path> excludedDirectories) throws IOException { + indexOfFirstDirectory = excludedFiles.size(); Review Comment: There is no special treatment for `.git` and `.DS_Store`. If users want to exclude them, they need an explicit `excludes` configuration. These directories would then be handled as any other files or directories to exclude, by temporarily moving them. However, a presence of `.git` would be surprising since it would imply the versioning of the `target` directory, and I would not expect a `.DS_Store` to be created after a `mvn clean`, unless user navigates in `target` through the MacOS UI before the JAR plugin had the time to execute. When moving, we can move a full directory in one operation. When copying, we need to walk through the whole directory tree. This is more costly. Even if we move instead of copying, moving the files to include would be costly compared to the current approach if 95% of the files are included. I don't know if the impact would be noticeable however, but I believe that the cumulative effect in millions of projects may save a couple of CO₂ tons. P.S. if we wanted to exclude the `.git` and `.DS_Store` directories by default, it would be just one `boolean` argument value to change from `false` to `true` at line 186 of `FileCollector`. The current value is `false` because it matches the behaviour of version 3.x of the plugin. We could change this behaviour, but I suggest to do that in a separated commit as a way to document behavioural change when we can do that easily. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
