diff --git a/pom.xml b/pom.xml index 2aa53eda..f3c7abcb 100644 --- a/pom.xml +++ b/pom.xml @@ -342,6 +342,7 @@ under the License. + modello-cache java @@ -351,6 +352,7 @@ under the License. generate-sources + modello-cache-xsd xsd @@ -375,6 +377,18 @@ under the License. build-helper-maven-plugin 3.4.0 + + add-sources + + add-source + + generate-sources + + + ${basedir}/target/generated-sources/modello + + + add-resources diff --git a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java index 5cef32ac..9779a482 100644 --- a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java +++ b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java @@ -481,9 +481,10 @@ public void save( build.getDto().set_final(cacheConfig.isSaveToRemoteFinal()); cacheResults.put(getVersionlessProjectKey(project), rebuilded(cacheResult, build)); + localCache.beforeSave(context); + // if package phase presence means new artifacts were packaged if (project.hasLifecyclePhase("package")) { - localCache.beforeSave(context); localCache.saveBuildInfo(cacheResult, build); if (projectArtifact.getFile() != null) { localCache.saveArtifactFile(cacheResult, projectArtifact); diff --git a/src/test/java/org/apache/maven/buildcache/its/Issue74Test.java b/src/test/java/org/apache/maven/buildcache/its/Issue74Test.java new file mode 100644 index 00000000..d2a45d6c --- /dev/null +++ b/src/test/java/org/apache/maven/buildcache/its/Issue74Test.java @@ -0,0 +1,98 @@ +/* + * 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.List; +import java.util.stream.Collectors; + +import org.apache.maven.buildcache.its.junit.IntegrationTest; +import org.apache.maven.buildcache.util.LogFileUtils; +import org.apache.maven.it.VerificationException; +import org.apache.maven.it.Verifier; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Check if cached builds are cleaned up correctly also for projects + * which don't contain a package phase. + */ +@IntegrationTest("src/test/projects/mbuildcache-74-clean-cache-any-artifact") +public class Issue74Test { + + private static final Logger logger = LoggerFactory.getLogger(Issue74Test.class); + + @Test + void simple(Verifier verifier) throws VerificationException, IOException { + verifier.setAutoclean(false); + verifier.setMavenDebug(true); + + // first run - uncached + verifier.setLogFileName("../log-1.txt"); + verifier.setSystemProperty("passed.by.test", "123"); + + verifier.executeGoal("verify"); + + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Local build was not found"); + verifyBuildCacheEntries(verifier, 1); + + // second run - modified + verifier.setLogFileName("../log-2.txt"); + verifier.setSystemProperty("passed.by.test", "456"); + + verifier.executeGoal("verify"); + + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Local build was not found"); + verifyBuildCacheEntries(verifier, 1); + } + + private static void verifyBuildCacheEntries(final Verifier verifier, long expectedBuilds) + throws VerificationException, IOException { + String buildInfoXmlLog = + LogFileUtils.findFirstLineContainingTextsInLogs(verifier, "Saved Build to local file: "); + String buildInfoXmlLocation = buildInfoXmlLog.split(":\\s")[1]; + + Path buildInfoXmlPath = Paths.get(buildInfoXmlLocation); + // buildinfo.xml -> local -> hash -> project + Path projectPathInCache = buildInfoXmlPath.getParent().getParent().getParent(); + + logger.info("Checking '{}' for cached builds ...", projectPathInCache); + + if (!Files.exists(projectPathInCache)) { + throw new VerificationException( + String.format("Project directory in build cache doesn't exist: '%s'", projectPathInCache)); + } + + List entries = + Files.list(projectPathInCache).filter(p -> Files.isDirectory(p)).collect(Collectors.toList()); + + Assertions.assertEquals( + expectedBuilds, + entries.size(), + "Expected amount of cached builds not satisfied. Found: " + + entries.stream().map(p -> p.getFileName().toString()).collect(Collectors.joining(","))); + } +} diff --git a/src/test/projects/mbuildcache-74-clean-cache-any-artifact/.mvn/extensions.xml b/src/test/projects/mbuildcache-74-clean-cache-any-artifact/.mvn/extensions.xml new file mode 100644 index 00000000..4be880d4 --- /dev/null +++ b/src/test/projects/mbuildcache-74-clean-cache-any-artifact/.mvn/extensions.xml @@ -0,0 +1,25 @@ + + + + + org.apache.maven.extensions + maven-build-cache-extension + ${projectVersion} + + diff --git a/src/test/projects/mbuildcache-74-clean-cache-any-artifact/.mvn/maven-build-cache-config.xml b/src/test/projects/mbuildcache-74-clean-cache-any-artifact/.mvn/maven-build-cache-config.xml new file mode 100644 index 00000000..1bbaaaba --- /dev/null +++ b/src/test/projects/mbuildcache-74-clean-cache-any-artifact/.mvn/maven-build-cache-config.xml @@ -0,0 +1,28 @@ + + + + + + + + 1 + + + diff --git a/src/test/projects/mbuildcache-74-clean-cache-any-artifact/pom.xml b/src/test/projects/mbuildcache-74-clean-cache-any-artifact/pom.xml new file mode 100644 index 00000000..73e8feb3 --- /dev/null +++ b/src/test/projects/mbuildcache-74-clean-cache-any-artifact/pom.xml @@ -0,0 +1,60 @@ + + + + 4.0.0 + org.apache.maven.caching.test.mbuildcache-74 + mbuildcache-74 + 0.0.1-SNAPSHOT + pom + + + 1.8 + 1.8 + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + 3.4.1 + + + require-property + + enforce + + + + + passed.by.test + Property passed.by.test is required + \d{3} + The passed.by.test property must consist of 3 digits. (actual=${passed.by.test}) + + + + + + + + + +