desruisseaux commented on code in PR #1091: URL: https://github.com/apache/maven-compiler-plugin/pull/1091#discussion_r3673295149
########## src/main/java/org/apache/maven/plugin/compiler/CompilationOutputRegistry.java: ########## @@ -0,0 +1,141 @@ +/* + * 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.plugin.compiler; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.codehaus.plexus.compiler.util.scan.InclusionScanException; +import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping; + +/** + * Tracks output files processed by compiler executions for the current project and Maven session. + * + * <p>Multiple compiler executions may share an output directory while using different compiler options. + * {@link org.apache.maven.shared.incremental.IncrementalBuildHelper} compares output file names before and after + * compilation, so it cannot detect an existing output overwritten by another execution. This registry lets a later + * execution detect that overlap without relying on file timestamps, whose precision varies between file systems.</p> + * + * <p>The registry is stored in Maven's plugin context, which is shared by all executions of this plugin for one + * project and discarded after the Maven session.</p> + */ +final class CompilationOutputRegistry { + private static final String KEY = CompilationOutputRegistry.class.getName() + ".compiledOutputs"; + + /** + * Prevents instantiation. + */ + private CompilationOutputRegistry() {} + + /** + * Maps sources to normalized absolute output paths. + * + * @param mapping mapping from source paths to output paths + * @param outputDirectory compiler output directory + * @param sourceRoots configured source roots + * @param sources sources selected by the compiler execution + * @return expected output paths + * @throws InclusionScanException if a source cannot be mapped + */ + static Set<Path> mapOutputs( + SourceMapping mapping, File outputDirectory, List<String> sourceRoots, Set<File> sources) + throws InclusionScanException { + Set<Path> outputs = new HashSet<>(); + for (String sourceRoot : sourceRoots) { + Path root = Paths.get(sourceRoot).toAbsolutePath().normalize(); + for (File source : sources) { + Path path = source.toPath().toAbsolutePath().normalize(); + if (path.startsWith(root)) { + for (File output : mapping.getTargetFiles( + outputDirectory, root.relativize(path).toString())) { + outputs.add(output.toPath().toAbsolutePath().normalize()); + } + } + } + } Review Comment: Indeed, don't think that peoples declare both `src/main` and `src/main/java` directories in same time. -- 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]
