elharo commented on code in PR #1102:
URL:
https://github.com/apache/maven-compiler-plugin/pull/1102#discussion_r3835855864
##########
src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java:
##########
@@ -1605,81 +1616,6 @@ private static List<String>
removeEmptyCompileSourceRoots(List<String> compileSo
return newCompileSourceRootsList;
}
- /**
- * We just compare the timestamps of all local dependency files
(inter-module dependency classpath) and the own
- * generated classes and if we got a file which is >= the build-started
timestamp, then we caught a file which
- * got changed during this build.
- *
- * @return {@code true} if at least one single dependency has changed.
- */
- protected boolean isDependencyChanged() {
Review Comment:
This method should be retained with protected access, even if it's
implementation changes
##########
src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java:
##########
@@ -949,7 +948,19 @@ private void executeReal() throws MojoExecutionException,
CompilationFailureExce
&& !canUpdateTarget)
? "immutable single output file"
: null;
- String dependencyChanged = isDependencyChanged() ? "changed
dependency" : null;
+ String dependencyChanged = DependencyState.hasChanged(
+ incrementalBuildHelper,
+ getOutputDirectory(),
+ getClasspathElements(),
+ getModulepathElements(),
+ new DependencyState.Configuration(
+ fileExtensions,
+
getBuildStartTimeInstant().orElse(null),
+ staleMillis,
+ getLog(),
+ showCompilationChanges))
+ ? "changed dependency"
Review Comment:
This is too many lines for a ternary operator to be legible. Just use an if
##########
src/main/java/org/apache/maven/plugin/compiler/DependencyState.java:
##########
@@ -0,0 +1,306 @@
+/*
+ * 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.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.shared.incremental.IncrementalBuildHelper;
+import org.codehaus.plexus.util.FileUtils;
+
+/**
+ * Persists the class path and module path used by a compiler Mojo execution
and detects dependencies modified during
+ * the current Maven build while collecting that state.
+ */
+final class DependencyState {
+ private static final String STATE_FILE = "dependencies.lst";
+ private static final String STATE_SEPARATOR = "\t";
+ private static final Set<String> DEFAULT_FILE_EXTENSIONS =
+ Collections.unmodifiableSet(new HashSet<>(Arrays.asList("class",
"jar")));
+
+ private DependencyState() {}
+
+ static boolean hasChanged(
+ IncrementalBuildHelper incrementalBuildHelper,
+ File outputDirectory,
+ List<String> classpathElements,
+ List<String> modulepathElements,
+ Configuration configuration) {
+ Path stateFile = null;
+ try {
+ stateFile =
incrementalBuildHelper.getMojoStatusDirectory().toPath().resolve(STATE_FILE);
+ } catch (MojoExecutionException e) {
+ configuration.log.warn("Error reading mojo status directory.");
+ }
+
+ List<String> oldState = Collections.emptyList();
+ boolean hasPreviousState = stateFile != null &&
Files.isRegularFile(stateFile);
+ if (hasPreviousState) {
+ try {
+ oldState = Files.readAllLines(stateFile);
+ } catch (IOException e) {
+ configuration.log.warn("Error while reading old dependency
status: " + stateFile);
+ hasPreviousState = false;
+ }
+ }
+
+ List<String> newState = new ArrayList<>();
+ ScanContext context = new ScanContext(outputDirectory, configuration);
+ Path changedDependency = addDependencies(newState, "classpath:",
classpathElements, context, null);
+ changedDependency = addDependencies(newState, "modulepath:",
modulepathElements, context, changedDependency);
+
+ if (stateFile != null) {
+ try {
+ Files.write(stateFile, newState);
+ } catch (IOException e) {
+ configuration.log.warn("Error while writing new dependency
status: " + stateFile);
+ }
+ }
+
+ if (hasPreviousState && !oldState.equals(newState)) {
+ if (configuration.log.isDebugEnabled() ||
configuration.showChanges) {
+ logChanges(oldState, newState, configuration.log);
+ }
+ return true;
+ }
+ if (changedDependency != null) {
+ if (configuration.log.isDebugEnabled() ||
configuration.showChanges) {
+ configuration.log.info("\tNew dependency detected: " +
changedDependency.toAbsolutePath());
Review Comment:
I only see one call of this method?
--
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]