From 93791d5a94cc4ef998b10d64b4af8cd009fd0629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9da=20Housni=20Alaoui?= Date: Wed, 21 Aug 2024 14:57:07 +0200 Subject: [PATCH 1/2] Executing 'clean process-test-classes' then 'clean verify' leads to empty jars --- .../maven/buildcache/CacheControllerImpl.java | 5 ++ .../maven/buildcache/its/Issue105Test.java | 73 +++++++++++++++++++ .../.mvn/maven-build-cache-config.xml | 20 +++++ src/test/projects/mbuildcache-105/pom.xml | 42 +++++++++++ .../org/apache/maven/buildcache/Test.java | 24 ++++++ 5 files changed, 164 insertions(+) create mode 100644 src/test/java/org/apache/maven/buildcache/its/Issue105Test.java create mode 100644 src/test/projects/mbuildcache-105/.mvn/maven-build-cache-config.xml create mode 100644 src/test/projects/mbuildcache-105/pom.xml create mode 100644 src/test/projects/mbuildcache-105/src/main/java/org/apache/maven/buildcache/Test.java diff --git a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java index 6cc644f9..510d2257 100644 --- a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java +++ b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java @@ -255,6 +255,11 @@ private CacheResult analyzeResult(CacheContext context, List mojo build.getCacheImplementationVersion()); } + if (lifecyclePhasesHelper.isLaterPhaseThanBuild("package", build)) { + LOGGER.warn("Cached build doesn't include phase 'package', cannot restore"); + return failure(build, context); + } + List cachedSegment = lifecyclePhasesHelper.getCachedSegment(context.getProject(), mojoExecutions, build); List missingMojos = build.getMissingExecutions(cachedSegment); diff --git a/src/test/java/org/apache/maven/buildcache/its/Issue105Test.java b/src/test/java/org/apache/maven/buildcache/its/Issue105Test.java new file mode 100644 index 00000000..b66a4238 --- /dev/null +++ b/src/test/java/org/apache/maven/buildcache/its/Issue105Test.java @@ -0,0 +1,73 @@ +/* + * 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.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; +import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; +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.Assertions; +import org.junit.jupiter.api.Test; + +/** + * @author RĂ©da Housni Alaoui + */ +@IntegrationTest("src/test/projects/mbuildcache-105") +class Issue105Test { + + public static final String GENERATED_JAR = "target/simple-0.0.1-SNAPSHOT.jar"; + + @Test + void simple(Verifier verifier) throws VerificationException, IOException { + verifier.setAutoclean(false); + + verifier.setLogFileName("../log-1.txt"); + verifier.executeGoals(Arrays.asList("clean", "process-test-classes")); + verifier.verifyErrorFreeLog(); + + verifier.setLogFileName("../log-2.txt"); + verifier.executeGoals(Arrays.asList("clean", "verify")); + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Cached build doesn't include phase 'package', cannot restore"); + verifier.verifyFilePresent(GENERATED_JAR); + + Path jarPath = Paths.get(verifier.getBasedir()).resolve(GENERATED_JAR); + assertJarEntryExists(jarPath, "org/apache/maven/buildcache/Test.class"); + } + + private void assertJarEntryExists(Path jarPath, String name) throws IOException { + try (JarArchiveInputStream inputStream = new JarArchiveInputStream(Files.newInputStream(jarPath))) { + JarArchiveEntry entry = inputStream.getNextJarEntry(); + while (entry != null) { + if (entry.getName().equals(name)) { + return; + } + entry = inputStream.getNextJarEntry(); + } + } + Assertions.fail("No JAR entry found for name '" + name + "'"); + } +} diff --git a/src/test/projects/mbuildcache-105/.mvn/maven-build-cache-config.xml b/src/test/projects/mbuildcache-105/.mvn/maven-build-cache-config.xml new file mode 100644 index 00000000..8c488100 --- /dev/null +++ b/src/test/projects/mbuildcache-105/.mvn/maven-build-cache-config.xml @@ -0,0 +1,20 @@ + + + + diff --git a/src/test/projects/mbuildcache-105/pom.xml b/src/test/projects/mbuildcache-105/pom.xml new file mode 100644 index 00000000..cfbc0681 --- /dev/null +++ b/src/test/projects/mbuildcache-105/pom.xml @@ -0,0 +1,42 @@ + + + + 4.0.0 + org.apache.maven.caching.test.mbuildcache-105 + simple + 0.0.1-SNAPSHOT + jar + + + 1.8 + 1.8 + + + + + + org.apache.maven.extensions + maven-build-cache-extension + ${projectVersion} + + + + + diff --git a/src/test/projects/mbuildcache-105/src/main/java/org/apache/maven/buildcache/Test.java b/src/test/projects/mbuildcache-105/src/main/java/org/apache/maven/buildcache/Test.java new file mode 100644 index 00000000..03f66a82 --- /dev/null +++ b/src/test/projects/mbuildcache-105/src/main/java/org/apache/maven/buildcache/Test.java @@ -0,0 +1,24 @@ +/* + * 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; + +class Test +{ + +} \ No newline at end of file From 89cdf6165406e347384c38d4b5b61f6eb3990e3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9da=20Housni=20Alaoui?= Date: Wed, 28 Aug 2024 15:48:16 +0200 Subject: [PATCH 2/2] Rename GENERATED_JAR to BUILT_JAR --- .../java/org/apache/maven/buildcache/its/Issue105Test.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/org/apache/maven/buildcache/its/Issue105Test.java b/src/test/java/org/apache/maven/buildcache/its/Issue105Test.java index b66a4238..ef2b8dd2 100644 --- a/src/test/java/org/apache/maven/buildcache/its/Issue105Test.java +++ b/src/test/java/org/apache/maven/buildcache/its/Issue105Test.java @@ -38,7 +38,7 @@ @IntegrationTest("src/test/projects/mbuildcache-105") class Issue105Test { - public static final String GENERATED_JAR = "target/simple-0.0.1-SNAPSHOT.jar"; + public static final String BUILT_JAR = "target/simple-0.0.1-SNAPSHOT.jar"; @Test void simple(Verifier verifier) throws VerificationException, IOException { @@ -52,9 +52,9 @@ void simple(Verifier verifier) throws VerificationException, IOException { verifier.executeGoals(Arrays.asList("clean", "verify")); verifier.verifyErrorFreeLog(); verifier.verifyTextInLog("Cached build doesn't include phase 'package', cannot restore"); - verifier.verifyFilePresent(GENERATED_JAR); + verifier.verifyFilePresent(BUILT_JAR); - Path jarPath = Paths.get(verifier.getBasedir()).resolve(GENERATED_JAR); + Path jarPath = Paths.get(verifier.getBasedir()).resolve(BUILT_JAR); assertJarEntryExists(jarPath, "org/apache/maven/buildcache/Test.class"); }