|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.plugins.jar; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.FileVisitResult; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.nio.file.PathMatcher; |
| 25 | +import java.nio.file.SimpleFileVisitor; |
| 26 | +import java.nio.file.attribute.BasicFileAttributes; |
| 27 | +import java.util.Collection; |
| 28 | + |
| 29 | +import org.apache.maven.api.annotations.Nonnull; |
| 30 | + |
| 31 | +/** |
| 32 | + * Creates the JAR file. |
| 33 | + * |
| 34 | + * TODO: this is a work in progress. |
| 35 | + */ |
| 36 | +final class Archiver extends SimpleFileVisitor<Path> { |
| 37 | + /** |
| 38 | + * Combination of includes and excludes path matchers applied on files. |
| 39 | + */ |
| 40 | + @Nonnull |
| 41 | + private final PathMatcher fileMatcher; |
| 42 | + |
| 43 | + /** |
| 44 | + * Combination of includes and excludes path matchers applied on directories. |
| 45 | + */ |
| 46 | + @Nonnull |
| 47 | + private final PathMatcher directoryMatcher; |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a new archiver. |
| 51 | + * |
| 52 | + * @param directory the base directory of the files to archive |
| 53 | + * @param includes the patterns of the files to include, or null or empty for including all files |
| 54 | + * @param excludes the patterns of the files to exclude, or null or empty for no exclusion |
| 55 | + */ |
| 56 | + Archiver(Path directory, Collection<String> includes, Collection<String> excludes) { |
| 57 | + fileMatcher = PathSelector.of(directory, includes, excludes); |
| 58 | + if (fileMatcher instanceof PathSelector ps) { |
| 59 | + if (ps.canFilterDirectories()) { |
| 60 | + directoryMatcher = (path) -> ps.couldHoldSelected(path); |
| 61 | + return; |
| 62 | + } |
| 63 | + } |
| 64 | + directoryMatcher = PathSelector.INCLUDES_ALL; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * A scanner of {@code module-info.class} file and {@code META-INF/versions/*} directories. |
| 69 | + * This is used only when we need to auto-detect whether the JAR is modular or multiè-release. |
| 70 | + * This is not needed anymore for projects using the Maven 4 {@code <source>} elements. |
| 71 | + */ |
| 72 | + final class VersionScanner extends SimpleFileVisitor<Path> { |
| 73 | + /** |
| 74 | + * The file to check for deciding whether the JAR is a modular JAR. |
| 75 | + */ |
| 76 | + private static final String MODULE_DESCRIPTOR_FILE_NAME = "module-info.class"; |
| 77 | + |
| 78 | + /** |
| 79 | + * The directory level in the {@code ./META-INF/versions/###/} path. |
| 80 | + * The root directory is at level 0 before we enter in that directory. |
| 81 | + * After the execution of {@code preVisitDirectory} (i.e., at the time of visiting files), values are: |
| 82 | + * |
| 83 | + * <ol> |
| 84 | + * <li>for any file in the {@code ./} root classes directory.</li> |
| 85 | + * <li>for any file in the {@code ./META-INF/} directory.</li> |
| 86 | + * <li>for any file in the {@code ./META-INF/versions/} directory (i.e., any version number).</li> |
| 87 | + * <li>for any file in the {@code ./META-INF/versions/###/} directory (i.e., any versioned file)</li> |
| 88 | + * </ol> |
| 89 | + */ |
| 90 | + private int level; |
| 91 | + |
| 92 | + /** |
| 93 | + * First level of versioned files in a {@code ./META-INF/versions/###/} directory. |
| 94 | + */ |
| 95 | + private static final int LEVEL_OF_VERSIONED_FILES = 4; |
| 96 | + |
| 97 | + /** |
| 98 | + * Whether a {@code META-INF/versions/} directory has been found. |
| 99 | + * The value of this flag is invalid if this scanner was constructed |
| 100 | + * with a {@code detectMultiReleaseJar} argument value of {@code true}. |
| 101 | + */ |
| 102 | + boolean detectedMultiReleaseJAR; |
| 103 | + |
| 104 | + /** |
| 105 | + * Whether a {@code module-info.class} file has been found. |
| 106 | + */ |
| 107 | + boolean containsModuleDescriptor; |
| 108 | + |
| 109 | + /** |
| 110 | + * Creates a scanner with all flags initially {@code false}. |
| 111 | + * |
| 112 | + * @param detectMultiReleaseJar whether to enable the detection of multi-release JAR |
| 113 | + */ |
| 114 | + VersionScanner(boolean detectMultiReleaseJar) { |
| 115 | + detectedMultiReleaseJAR = !detectMultiReleaseJar; // Not actually true, but will cause faster stop. |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns {@code true} if the given directory is one of the parts of {@code ./META-INF/versions/*}. |
| 120 | + * The {@code .} directory is at level 0, {@code META-INF} at level 1, {@code versions} at level 2, |
| 121 | + * and one of the versions at level 3. Files at level 4 are versioned class files. |
| 122 | + */ |
| 123 | + @Override |
| 124 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { |
| 125 | + if (detectedMultiReleaseJAR && level >= LEVEL_OF_VERSIONED_FILES) { |
| 126 | + // When searching only for `module-info.class`, we do not need to go further than that level. |
| 127 | + return (level > LEVEL_OF_VERSIONED_FILES) |
| 128 | + ? FileVisitResult.SKIP_SIBLINGS |
| 129 | + : FileVisitResult.SKIP_SUBTREE; |
| 130 | + } |
| 131 | + if (directoryMatcher.matches(dir)) { |
| 132 | + String expected = |
| 133 | + switch (level) { |
| 134 | + case 1 -> "META-INF"; |
| 135 | + case 2 -> "versions"; |
| 136 | + default -> null; |
| 137 | + }; |
| 138 | + if (expected == null || dir.endsWith(expected)) { |
| 139 | + level++; |
| 140 | + return FileVisitResult.CONTINUE; |
| 141 | + } |
| 142 | + } |
| 143 | + return FileVisitResult.SKIP_SUBTREE; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Decrements our count of directory levels after visiting a directory. |
| 148 | + */ |
| 149 | + @Override |
| 150 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 151 | + level--; |
| 152 | + return super.postVisitDirectory(dir, exc); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Updates the flags for the given files. This method terminates the |
| 157 | + * scanning if it detects that there is no new information to collect. |
| 158 | + */ |
| 159 | + @Override |
| 160 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| 161 | + if (fileMatcher.matches(file)) { |
| 162 | + if (level == 1 || level == LEVEL_OF_VERSIONED_FILES) { |
| 163 | + // Root directory or a `META-INF/versions/###/` directory. |
| 164 | + containsModuleDescriptor |= file.endsWith(MODULE_DESCRIPTOR_FILE_NAME); |
| 165 | + } |
| 166 | + detectedMultiReleaseJAR |= (level >= LEVEL_OF_VERSIONED_FILES); |
| 167 | + if (detectedMultiReleaseJAR) { |
| 168 | + if (containsModuleDescriptor) { |
| 169 | + return FileVisitResult.TERMINATE; |
| 170 | + } |
| 171 | + if (level > LEVEL_OF_VERSIONED_FILES) { |
| 172 | + return FileVisitResult.SKIP_SIBLINGS; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + return FileVisitResult.CONTINUE; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Determines if the given directory should be scanned for files to archive. |
| 182 | + */ |
| 183 | + @Override |
| 184 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { |
| 185 | + return directoryMatcher.matches(dir) ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Archives a file in a directory. |
| 190 | + */ |
| 191 | + @Override |
| 192 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| 193 | + if (fileMatcher.matches(file)) { |
| 194 | + // TODO |
| 195 | + } |
| 196 | + return FileVisitResult.CONTINUE; |
| 197 | + } |
| 198 | +} |
0 commit comments