This is an automated email from the ASF dual-hosted git repository.
olamy pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/maven-build-cache-extension.git
The following commit(s) were added to refs/heads/master by this push:
new dc66364 Issue #492 restore compile-only reactor module so downstream
resoluti… (#493)
dc66364 is described below
commit dc66364f00955c511f305dcac936104fedebe6fd
Author: Corey <[email protected]>
AuthorDate: Thu Nov 12 05:08:15 2026 -0600
Issue #492 restore compile-only reactor module so downstream resoluti…
(#493)
* Issue #492 restore compile-only reactor module so downstream resolution
works
When a cached build did not reach the package phase the cache entry's
primary artifact is the target/classes directory rather than a jar.
restoreProjectArtifacts only set project.getArtifact() to the restored
artifact when the cached file was a regular jar, leaving directory
artifacts unbound. In a multi-module reactor that meant downstream
modules could not find the upstream's classes on the second
`mvn clean compile` invocation and fell through to remote repositories
with "Could not find artifact ...:jar:...-SNAPSHOT".
For directory artifacts, set the file on the existing project artifact
instead of replacing it with RestoredArtifact. RestoredArtifact.getFile
always re-routes through its restoreToDiskConsumer, which would later
break package-phase plugins (e.g. jar:jar) that update the artifact's
file via setFile.
Adds CompileOnlyReactorRestoreTest covering this case.
* Issue #492 only point the project artifact at a restored classes directory
With restoreOnDiskArtifacts=false the restored file is the cached archive
rather than target/classes. Setting it as the project artifact file made
jar:jar treat the module as already packaged and fail with "You have to
use a classifier to attach supplemental artifacts to the project instead
of replacing them."
Only set the file when the restored artifact is a directory on disk.
Adds CompileOnlyRestoreWithoutDiskArtifactsTest from #563.
Co-authored-by: Olivier Lamy <[email protected]>
* Issue #492 stage target/classes before rebuilding a module
stagePreExistingArtifacts is meant to move a module's previous outputs
aside so the compiler cannot keep stale classes, but it found
target/classes only through the project artifact file, which is not set
yet at the start of a session. Without clean, a module downstream of one
restored from a compile-only cache entry reported "Nothing to compile"
and kept classes compiled against a different version of the upstream,
which then got cached.
When cacheCompile is enabled, stage the compile output directory too.
Adds CompileOnlyRestoreStaleDownstreamTest from #563.
Co-authored-by: Olivier Lamy <[email protected]>
* Issue #492 record the lifecycle phases of cached mojos
Maven's ReactorReader resolves an unpackaged reactor module to
target/classes only when its compile phase is recorded, and to
target/test-classes only when test-compile is. Maven records a phase
when it runs a mojo, so mojos skipped because of a cache hit left
neither recorded, and downstream modules could not resolve the module
or its test-jar.
Record the phase of each skipped cached mojo, as is already done for
package after restoring a packaged artifact. This replaces pointing the
project artifact at the restored classes directory, which also covered
the main artifact but not test-jar dependencies.
Adds CompileOnlyRestoreTestJarDependencyTest from #563.
Co-authored-by: Olivier Lamy <[email protected]>
* Issue #492 cover compile-only restore in a forked lifecycle
A forked lifecycle such as dependency:analyze runs on a clone of the
project, but MavenProject.clone() shares the lifecycle phase set with
the original, so the phases recorded for cached mojos in the fork reach
reactor resolution too.
Adds CompileOnlyRestoreForkedLifecycleTest from #563.
Co-authored-by: Olivier Lamy <[email protected]>
* Issue #492 cover a downstream module compiling against restored classes
The other compile-only restore tests restore both modules from the
cache, so downstream only has to resolve upstream. Change only the
downstream module so it is rebuilt and has to compile against the
upstream classes restored from the cache.
* Issue #492 restore compile output with fresh timestamps
A module restored from a compile-only cache entry got its target/classes
back with the timestamps of the build that produced it. A downstream module
rebuilt in the same session compared those old timestamps against its own
classes and reported "Nothing to compile", keeping classes compiled against
a different version of the upstream, which then got cached. A restored jar
never had this problem because it is copied with a fresh timestamp.
Restore directory artifacts with fresh timestamps too, matching the jar.
attachedOutputs still honour preserveTimestamps.
This replaces staging target/classes on every cache miss, which removed
the main classes a partial invocation such as compiler:testCompile needs.
Co-authored-by: Olivier Lamy <[email protected]>
---------
Co-authored-by: Corey Puffalt <[email protected]>
Co-authored-by: yeroc <[email protected]>
Co-authored-by: Olivier Lamy <[email protected]>
---
.../BuildCacheMojosExecutionStrategy.java | 5 +
.../maven/buildcache/CacheControllerImpl.java | 9 +-
.../its/CompileOnlyReactorRestoreTest.java | 57 +++++++++
.../CompileOnlyRestoreDownstreamChangeTest.java | 85 ++++++++++++++
.../its/CompileOnlyRestoreForkedLifecycleTest.java | 52 +++++++++
.../its/CompileOnlyRestoreStaleDownstreamTest.java | 129 +++++++++++++++++++++
.../CompileOnlyRestoreTestJarDependencyTest.java | 54 +++++++++
...CompileOnlyRestoreWithoutDiskArtifactsTest.java | 55 +++++++++
.../.mvn/extensions.xml | 28 +++++
.../.mvn/maven-build-cache-config.xml | 34 ++++++
.../downstream/pom.xml | 50 ++++++++
.../test/testjar/downstream/Downstream.java | 30 +++++
.../testjar/downstream/DownstreamFixtures.java | 30 +++++
.../projects/compile-only-restore-test-jar/pom.xml | 41 +++++++
.../compile-only-restore-test-jar/upstream/pom.xml | 51 ++++++++
.../caching/test/testjar/upstream/Upstream.java | 28 +++++
.../test/testjar/upstream/UpstreamFixtures.java | 28 +++++
17 files changed, 761 insertions(+), 5 deletions(-)
diff --git
a/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java
b/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java
index 3f77ee5..464d416 100644
---
a/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java
+++
b/src/main/java/org/apache/maven/buildcache/BuildCacheMojosExecutionStrategy.java
@@ -447,6 +447,11 @@ private CacheRestorationStatus restoreProject(
LOGGER.info(
"Skipping plugin execution (cached): {}",
cacheCandidate.getMojoDescriptor().getFullGoalName());
+ // Record the phase as Maven does for executed mojos. The
reactor only resolves a module's
+ // target/classes or target/test-classes when its compile or
test-compile phase is recorded.
+ if (cacheCandidate.getLifecyclePhase() != null) {
+
project.addLifecyclePhase(cacheCandidate.getLifecyclePhase());
+ }
// Need to populate cached candidate executions for the build
cache save result
Mojo mojo = null;
mojoExecutionScope.enter();
diff --git a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java
b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java
index f33f514..6402317 100644
--- a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java
+++ b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java
@@ -374,11 +374,10 @@ private void restoreDirectoryArtifact(File cachedZip,
Artifact artifact, Path re
if (!Files.exists(restorationPath)) {
Files.createDirectories(restorationPath);
}
- CacheUtils.unzip(
- cachedZip.toPath(),
- restorationPath,
- cacheConfig.isPreservePermissions(),
- cacheConfig.isPreserveTimestamps());
+ // Give restored files fresh timestamps, as restoreRegularFileArtifact
does. Downstream compilers decide
+ // whether to recompile by comparing timestamps, so restoring an older
version of these classes with its
+ // original timestamps would leave modules compiled against a
different version of them.
+ CacheUtils.unzip(cachedZip.toPath(), restorationPath,
cacheConfig.isPreservePermissions(), false);
LOGGER.debug("Restored directory artifact by unzipping: {} -> {}",
artifact.getFileName(), restorationPath);
}
diff --git
a/src/test/java/org/apache/maven/buildcache/its/CompileOnlyReactorRestoreTest.java
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyReactorRestoreTest.java
new file mode 100644
index 0000000..c5abf80
--- /dev/null
+++
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyReactorRestoreTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.buildcache.its;
+
+import java.util.Arrays;
+
+import org.apache.maven.buildcache.its.junit.IntegrationTest;
+import org.apache.maven.it.VerificationException;
+import org.apache.maven.it.Verifier;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Reproducer for <a
href="https://github.com/apache/maven-build-cache-extension/issues/492">Issue
#492</a>:
+ * {@code clean compile} twice in a multi-module reactor fails to resolve the
upstream module.
+ *
+ * <p>On the second run the cache extension restores the upstream module's
compiled
+ * output but, because no jar was produced in the original cached build,
downstream
+ * dependency resolution falls through to remote repositories with
+ * {@code Could not find artifact ...:jar:...-SNAPSHOT}.
+ */
+@IntegrationTest("src/test/projects/issue-393-compile-restore")
+class CompileOnlyReactorRestoreTest {
+
+ @Test
+ void cleanCompileTwiceResolvesUpstreamFromReactorCache(Verifier verifier)
throws VerificationException {
+ verifier.setAutoclean(false);
+
+ verifier.setLogFileName("../log-compile-1.txt");
+ verifier.executeGoals(Arrays.asList("clean", "compile"));
+ verifier.verifyErrorFreeLog();
+
+ verifier.setLogFileName("../log-compile-2.txt");
+ verifier.executeGoals(Arrays.asList("clean", "compile"));
+ verifier.verifyErrorFreeLog();
+ verifier.verifyTextInLog(
+ "Found cached build, restoring
org.apache.maven.caching.test.jpms:issue-393-app from cache");
+ verifier.verifyTextInLog("Skipping plugin execution (cached):
compiler:compile");
+ verifier.verifyFilePresent("app/target/classes/module-info.class");
+
verifier.verifyFilePresent("consumer/target/classes/module-info.class");
+ }
+}
diff --git
a/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreDownstreamChangeTest.java
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreDownstreamChangeTest.java
new file mode 100644
index 0000000..e05a365
--- /dev/null
+++
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreDownstreamChangeTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.buildcache.its;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+
+import org.apache.maven.buildcache.its.junit.IntegrationTest;
+import org.apache.maven.it.VerificationException;
+import org.apache.maven.it.Verifier;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.maven.buildcache.its.CacheITUtils.assertNotInLog;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Like <a
href="https://github.com/apache/maven-build-cache-extension/issues/492">#492</a>,
but only the
+ * downstream module changes: it is rebuilt and must compile against the
upstream classes restored from the cache.
+ */
+@IntegrationTest("src/test/projects/issue-393-compile-restore")
+class CompileOnlyRestoreDownstreamChangeTest {
+
+ private static final String CONSUMER =
+
"consumer/src/main/java/org/apache/maven/caching/test/jpms/consumer/Consumer.java";
+ private static final String CONSUMER_CLASS =
+
"consumer/target/classes/org/apache/maven/caching/test/jpms/consumer/Consumer.class";
+
+ @Test
+ void downstreamCompilesAgainstRestoredUpstream(Verifier verifier) throws
VerificationException, IOException {
+ verifier.setAutoclean(false);
+
+ verifier.setLogFileName("../log-compile-1.txt");
+ verifier.executeGoals(Arrays.asList("clean", "compile"));
+ verifier.verifyErrorFreeLog();
+
+ // consumer's new method uses app, so consumer can only compile if
app's restored classes are on its classpath
+ verifier.writeFile(
+ CONSUMER,
+ "package org.apache.maven.caching.test.jpms.consumer;\n"
+ + "\n"
+ + "import
org.apache.maven.caching.test.jpms.app.Greeting;\n"
+ + "\n"
+ + "public final class Consumer {\n"
+ + "\n"
+ + " private Consumer() {}\n"
+ + "\n"
+ + " public static String message() {\n"
+ + " return Greeting.message();\n"
+ + " }\n"
+ + "\n"
+ + " public static String loudMessage() {\n"
+ + " return Greeting.message().toUpperCase();\n"
+ + " }\n"
+ + "}\n");
+
+ verifier.setLogFileName("../log-compile-2.txt");
+ verifier.executeGoals(Arrays.asList("clean", "compile"));
+ verifier.verifyErrorFreeLog();
+ verifier.verifyTextInLog(
+ "Found cached build, restoring
org.apache.maven.caching.test.jpms:issue-393-app from cache");
+ assertNotInLog(verifier, "Found cached build, restoring
org.apache.maven.caching.test.jpms:issue-393-consumer");
+ String consumerClass = new String(
+ Files.readAllBytes(Paths.get(verifier.getBasedir(),
CONSUMER_CLASS)), StandardCharsets.ISO_8859_1);
+ assertTrue(consumerClass.contains("loudMessage"), "consumer should be
compiled with its new method");
+ }
+}
diff --git
a/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreForkedLifecycleTest.java
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreForkedLifecycleTest.java
new file mode 100644
index 0000000..2e7d7d2
--- /dev/null
+++
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreForkedLifecycleTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.buildcache.its;
+
+import org.apache.maven.buildcache.its.junit.IntegrationTest;
+import org.apache.maven.it.VerificationException;
+import org.apache.maven.it.Verifier;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Like <a
href="https://github.com/apache/maven-build-cache-extension/issues/492">#492</a>,
but for a goal that
+ * forks the lifecycle: {@code dependency:analyze} forks {@code test-compile}
in each module.
+ *
+ * <p>On the second run both forks come from the cache, and consumer must
still find app. The fork works on a
+ * copy of the project, so anything the restore sets there never reaches the
reactor.
+ */
+@IntegrationTest("src/test/projects/issue-393-compile-restore")
+class CompileOnlyRestoreForkedLifecycleTest {
+
+ private static final String ANALYZE =
"org.apache.maven.plugins:maven-dependency-plugin:3.8.1:analyze";
+
+ @Test
+ void forkedLifecycleTwiceResolvesUpstreamFromReactor(Verifier verifier)
throws VerificationException {
+ verifier.setAutoclean(false);
+
+ verifier.setLogFileName("../log-analyze-1.txt");
+ verifier.executeGoal(ANALYZE);
+ verifier.verifyErrorFreeLog();
+
+ verifier.setLogFileName("../log-analyze-2.txt");
+ verifier.executeGoal(ANALYZE);
+ verifier.verifyErrorFreeLog();
+ verifier.verifyTextInLog(
+ "Found cached build, restoring
org.apache.maven.caching.test.jpms:issue-393-app from cache");
+ }
+}
diff --git
a/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreStaleDownstreamTest.java
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreStaleDownstreamTest.java
new file mode 100644
index 0000000..b18a94c
--- /dev/null
+++
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreStaleDownstreamTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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.buildcache.its;
+
+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 org.apache.maven.buildcache.its.junit.IntegrationTest;
+import org.apache.maven.it.VerificationException;
+import org.apache.maven.it.Verifier;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * In a build without {@code clean}, when app is restored from the cache,
consumer must be recompiled against it.
+ *
+ * <p>Restored class files keep their old timestamps, so the compiler thinks
consumer is up to date and keeps
+ * classes built against another version of app.
+ *
+ * <p>Uses the default cache config. With {@code attachedOutputs}, consumer's
classes are moved aside first and
+ * fully recompiled, which hides the bug.
+ */
+@IntegrationTest("src/test/projects/issue-393-compile-restore")
+class CompileOnlyRestoreStaleDownstreamTest {
+
+ private static final String GREETING =
"app/src/main/java/org/apache/maven/caching/test/jpms/app/Greeting.java";
+ private static final String CONSUMER =
+
"consumer/src/main/java/org/apache/maven/caching/test/jpms/consumer/Consumer.java";
+ private static final String CONSUMER_CLASS =
+
"consumer/target/classes/org/apache/maven/caching/test/jpms/consumer/Consumer.class";
+
+ @Test
+ void downstreamIsRecompiledAgainstRestoredUpstream(Verifier verifier)
throws VerificationException, IOException {
+ verifier.setAutoclean(false);
+ Path basedir = Paths.get(verifier.getBasedir());
+ Files.delete(basedir.resolve(".mvn/maven-build-cache-config.xml"));
+
+ // Build 1: app v1 gets cached
+ writeGreeting(basedir, "greeting-v1");
+ writeConsumer(basedir, false);
+ verifier.setLogFileName("../log-compile-1.txt");
+ verifier.executeGoal("compile");
+ verifier.verifyErrorFreeLog();
+
+ // Build 2: app v2, and consumer gets a new method
+ writeGreeting(basedir, "greeting-v2");
+ writeConsumer(basedir, true);
+ verifier.setLogFileName("../log-compile-2.txt");
+ verifier.executeGoal("compile");
+ verifier.verifyErrorFreeLog();
+ assertTrue(consumerClass(basedir).contains("greeting-v2"), "consumer
should be compiled against app v2");
+
+ // Build 3: app is back to v1 and comes from the cache. consumer still
has its new method, so it
+ // isn't cached and must be compiled against v1.
+ writeGreeting(basedir, "greeting-v1");
+ verifier.setLogFileName("../log-compile-3.txt");
+ verifier.executeGoal("compile");
+ verifier.verifyErrorFreeLog();
+ verifier.verifyTextInLog(
+ "Found cached build, restoring
org.apache.maven.caching.test.jpms:issue-393-app from cache");
+
+ String consumerClass = consumerClass(basedir);
+ assertTrue(consumerClass.contains("greeting-v1"), "consumer should be
recompiled against restored app v1");
+ assertFalse(consumerClass.contains("greeting-v2"), "consumer still
holds classes compiled against app v2");
+ }
+
+ private static void writeGreeting(Path basedir, String version) throws
IOException {
+ String source = "package org.apache.maven.caching.test.jpms.app;\n"
+ + "\n"
+ + "public final class Greeting {\n"
+ + "\n"
+ + " public static final String VERSION = \"" + version +
"\";\n"
+ + "\n"
+ + " private Greeting() {}\n"
+ + "\n"
+ + " public static String message() {\n"
+ + " return \"hello from module\";\n"
+ + " }\n"
+ + "}\n";
+ Files.write(basedir.resolve(GREETING),
source.getBytes(StandardCharsets.UTF_8));
+ }
+
+ private static void writeConsumer(Path basedir, boolean withName) throws
IOException {
+ // VERSION is a constant, so javac copies its value into Consumer.class
+ String source = "package
org.apache.maven.caching.test.jpms.consumer;\n"
+ + "\n"
+ + "import org.apache.maven.caching.test.jpms.app.Greeting;\n"
+ + "\n"
+ + "public final class Consumer {\n"
+ + "\n"
+ + " private Consumer() {}\n"
+ + "\n"
+ + " public static String message() {\n"
+ + " return Greeting.message();\n"
+ + " }\n"
+ + "\n"
+ + " public static String version() {\n"
+ + " return Greeting.VERSION;\n"
+ + " }\n"
+ + (withName ? "\n public static String name() {\n
return \"consumer\";\n }\n" : "")
+ + "}\n";
+ Files.write(basedir.resolve(CONSUMER),
source.getBytes(StandardCharsets.UTF_8));
+ }
+
+ private static String consumerClass(Path basedir) throws IOException {
+ return new String(Files.readAllBytes(basedir.resolve(CONSUMER_CLASS)),
StandardCharsets.ISO_8859_1);
+ }
+}
diff --git
a/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreTestJarDependencyTest.java
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreTestJarDependencyTest.java
new file mode 100644
index 0000000..18152c1
--- /dev/null
+++
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreTestJarDependencyTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.buildcache.its;
+
+import java.util.Arrays;
+
+import org.apache.maven.buildcache.its.junit.IntegrationTest;
+import org.apache.maven.it.VerificationException;
+import org.apache.maven.it.Verifier;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Like <a
href="https://github.com/apache/maven-build-cache-extension/issues/492">#492</a>,
but with a
+ * {@code test-jar} dependency: when upstream comes from the cache, downstream
must still find its test classes.
+ *
+ * <p>Without a jar, Maven only uses {@code target/test-classes} if upstream
ran {@code test-compile} in this
+ * build, and cached mojos never tell it that.
+ */
+@IntegrationTest("src/test/projects/compile-only-restore-test-jar")
+class CompileOnlyRestoreTestJarDependencyTest {
+
+ @Test
+ void testCompileTwiceResolvesUpstreamTestJar(Verifier verifier) throws
VerificationException {
+ verifier.setAutoclean(false);
+
+ verifier.setLogFileName("../log-test-compile-1.txt");
+ verifier.executeGoals(Arrays.asList("clean", "test-compile"));
+ verifier.verifyErrorFreeLog();
+
+ verifier.setLogFileName("../log-test-compile-2.txt");
+ verifier.executeGoals(Arrays.asList("clean", "test-compile"));
+ verifier.verifyErrorFreeLog();
+ verifier.verifyTextInLog(
+ "Found cached build, restoring
org.apache.maven.caching.test.testjar:test-jar-upstream from cache");
+ verifier.verifyFilePresent(
+
"downstream/target/test-classes/org/apache/maven/caching/test/testjar/downstream/DownstreamFixtures.class");
+ }
+}
diff --git
a/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreWithoutDiskArtifactsTest.java
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreWithoutDiskArtifactsTest.java
new file mode 100644
index 0000000..7772d38
--- /dev/null
+++
b/src/test/java/org/apache/maven/buildcache/its/CompileOnlyRestoreWithoutDiskArtifactsTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.buildcache.its;
+
+import java.util.Arrays;
+
+import org.apache.maven.buildcache.its.junit.IntegrationTest;
+import org.apache.maven.it.VerificationException;
+import org.apache.maven.it.Verifier;
+import org.junit.jupiter.api.Test;
+
+/**
+ * After a compile-only build, packaging must still work when {@code
restoreOnDiskArtifacts} is off.
+ *
+ * <p>With that flag off, the restored file stays inside the cache. If app's
artifact points at it,
+ * {@code jar:jar} thinks a jar is already there and fails.
+ */
+@IntegrationTest("src/test/projects/issue-393-compile-restore")
+class CompileOnlyRestoreWithoutDiskArtifactsTest {
+
+ @Test
+ void packageAfterCompileOnlyBuildWithoutDiskArtifacts(Verifier verifier)
throws VerificationException {
+ verifier.setAutoclean(false);
+
+ verifier.setLogFileName("../log-compile.txt");
+ verifier.executeGoals(Arrays.asList("clean", "compile"));
+ verifier.verifyErrorFreeLog();
+
+ // app comes from the compile-only cache entry, then gets packaged
+ verifier.setLogFileName("../log-verify.txt");
+
verifier.addCliOption("-Dmaven.build.cache.restoreOnDiskArtifacts=false");
+ verifier.executeGoals(Arrays.asList("clean", "verify"));
+ verifier.verifyErrorFreeLog();
+ verifier.verifyTextInLog(
+ "Found cached build, restoring
org.apache.maven.caching.test.jpms:issue-393-app from cache");
+
verifier.verifyFilePresent("app/target/issue-393-app-0.0.1-SNAPSHOT.jar");
+
verifier.verifyFilePresent("consumer/target/issue-393-consumer-0.0.1-SNAPSHOT.jar");
+ }
+}
diff --git
a/src/test/projects/compile-only-restore-test-jar/.mvn/extensions.xml
b/src/test/projects/compile-only-restore-test-jar/.mvn/extensions.xml
new file mode 100644
index 0000000..8568c4d
--- /dev/null
+++ b/src/test/projects/compile-only-restore-test-jar/.mvn/extensions.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<extensions>
+ <extension>
+ <groupId>org.apache.maven.extensions</groupId>
+ <artifactId>maven-build-cache-extension</artifactId>
+ <version>${projectVersion}</version>
+ </extension>
+</extensions>
diff --git
a/src/test/projects/compile-only-restore-test-jar/.mvn/maven-build-cache-config.xml
b/src/test/projects/compile-only-restore-test-jar/.mvn/maven-build-cache-config.xml
new file mode 100644
index 0000000..6a91d57
--- /dev/null
+++
b/src/test/projects/compile-only-restore-test-jar/.mvn/maven-build-cache-config.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<cache xmlns="http://maven.apache.org/BUILD-CACHE-CONFIG/1.2.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/BUILD-CACHE-CONFIG/1.2.0
https://maven.apache.org/xsd/build-cache-config-1.2.0.xsd">
+ <configuration>
+ <attachedOutputs>
+ <dirNames>
+ <dirName>classes</dirName>
+ <dirName>test-classes</dirName>
+ <dirName>maven-status</dirName>
+ </dirNames>
+ </attachedOutputs>
+ </configuration>
+</cache>
diff --git a/src/test/projects/compile-only-restore-test-jar/downstream/pom.xml
b/src/test/projects/compile-only-restore-test-jar/downstream/pom.xml
new file mode 100644
index 0000000..3fd9b77
--- /dev/null
+++ b/src/test/projects/compile-only-restore-test-jar/downstream/pom.xml
@@ -0,0 +1,50 @@
+<!--
+
+ 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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.maven.caching.test.testjar</groupId>
+ <artifactId>compile-only-restore-test-jar</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>test-jar-downstream</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>test-jar-upstream</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>test-jar-upstream</artifactId>
+ <version>${project.version}</version>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
diff --git
a/src/test/projects/compile-only-restore-test-jar/downstream/src/main/java/org/apache/maven/caching/test/testjar/downstream/Downstream.java
b/src/test/projects/compile-only-restore-test-jar/downstream/src/main/java/org/apache/maven/caching/test/testjar/downstream/Downstream.java
new file mode 100644
index 0000000..3aa0d81
--- /dev/null
+++
b/src/test/projects/compile-only-restore-test-jar/downstream/src/main/java/org/apache/maven/caching/test/testjar/downstream/Downstream.java
@@ -0,0 +1,30 @@
+/*
+ * 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.caching.test.testjar.downstream;
+
+import org.apache.maven.caching.test.testjar.upstream.Upstream;
+
+public final class Downstream {
+
+ private Downstream() {}
+
+ public static String name() {
+ return "downstream of " + Upstream.name();
+ }
+}
diff --git
a/src/test/projects/compile-only-restore-test-jar/downstream/src/test/java/org/apache/maven/caching/test/testjar/downstream/DownstreamFixtures.java
b/src/test/projects/compile-only-restore-test-jar/downstream/src/test/java/org/apache/maven/caching/test/testjar/downstream/DownstreamFixtures.java
new file mode 100644
index 0000000..f2e359e
--- /dev/null
+++
b/src/test/projects/compile-only-restore-test-jar/downstream/src/test/java/org/apache/maven/caching/test/testjar/downstream/DownstreamFixtures.java
@@ -0,0 +1,30 @@
+/*
+ * 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.caching.test.testjar.downstream;
+
+import org.apache.maven.caching.test.testjar.upstream.UpstreamFixtures;
+
+public final class DownstreamFixtures {
+
+ private DownstreamFixtures() {}
+
+ public static String sampleName() {
+ return "downstream of " + UpstreamFixtures.sampleName();
+ }
+}
diff --git a/src/test/projects/compile-only-restore-test-jar/pom.xml
b/src/test/projects/compile-only-restore-test-jar/pom.xml
new file mode 100644
index 0000000..a1ab572
--- /dev/null
+++ b/src/test/projects/compile-only-restore-test-jar/pom.xml
@@ -0,0 +1,41 @@
+<!--
+
+ 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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.apache.maven.caching.test.testjar</groupId>
+ <artifactId>compile-only-restore-test-jar</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <packaging>pom</packaging>
+
+ <modules>
+ <module>upstream</module>
+ <module>downstream</module>
+ </modules>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <maven.compiler.release>11</maven.compiler.release>
+ </properties>
+
+</project>
diff --git a/src/test/projects/compile-only-restore-test-jar/upstream/pom.xml
b/src/test/projects/compile-only-restore-test-jar/upstream/pom.xml
new file mode 100644
index 0000000..a7d4a53
--- /dev/null
+++ b/src/test/projects/compile-only-restore-test-jar/upstream/pom.xml
@@ -0,0 +1,51 @@
+<!--
+
+ 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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.maven.caching.test.testjar</groupId>
+ <artifactId>compile-only-restore-test-jar</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>test-jar-upstream</artifactId>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>test-jar</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
diff --git
a/src/test/projects/compile-only-restore-test-jar/upstream/src/main/java/org/apache/maven/caching/test/testjar/upstream/Upstream.java
b/src/test/projects/compile-only-restore-test-jar/upstream/src/main/java/org/apache/maven/caching/test/testjar/upstream/Upstream.java
new file mode 100644
index 0000000..bc9bda0
--- /dev/null
+++
b/src/test/projects/compile-only-restore-test-jar/upstream/src/main/java/org/apache/maven/caching/test/testjar/upstream/Upstream.java
@@ -0,0 +1,28 @@
+/*
+ * 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.caching.test.testjar.upstream;
+
+public final class Upstream {
+
+ private Upstream() {}
+
+ public static String name() {
+ return "upstream";
+ }
+}
diff --git
a/src/test/projects/compile-only-restore-test-jar/upstream/src/test/java/org/apache/maven/caching/test/testjar/upstream/UpstreamFixtures.java
b/src/test/projects/compile-only-restore-test-jar/upstream/src/test/java/org/apache/maven/caching/test/testjar/upstream/UpstreamFixtures.java
new file mode 100644
index 0000000..83188ca
--- /dev/null
+++
b/src/test/projects/compile-only-restore-test-jar/upstream/src/test/java/org/apache/maven/caching/test/testjar/upstream/UpstreamFixtures.java
@@ -0,0 +1,28 @@
+/*
+ * 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.caching.test.testjar.upstream;
+
+public final class UpstreamFixtures {
+
+ private UpstreamFixtures() {}
+
+ public static String sampleName() {
+ return Upstream.name() + "-sample";
+ }
+}