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 7910490 [MBUILDCACHE-74] Clean local cache for any artifact (#110) 7910490 is described below commit 7910490b92a198be784e7ed6a2f22f0419f24c45 Author: Michael Weirauch <michael.weira...@gmail.com> AuthorDate: Wed Nov 22 12:22:19 2023 +0100 [MBUILDCACHE-74] Clean local cache for any artifact (#110) Not just the ones which have a package phase. Otherwise the cached builds will be kept forever. --- pom.xml | 14 ++++ .../maven/buildcache/CacheControllerImpl.java | 3 +- .../apache/maven/buildcache/its/Issue74Test.java | 98 ++++++++++++++++++++++ .../.mvn/extensions.xml | 25 ++++++ .../.mvn/maven-build-cache-config.xml | 28 +++++++ .../pom.xml | 60 +++++++++++++ 6 files changed, 227 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ad6357a..c4ee869 100644 --- a/pom.xml +++ b/pom.xml @@ -342,6 +342,7 @@ under the License. </configuration> <executions> <execution> + <?m2e execute onConfiguration,onIncremental?> <id>modello-cache</id> <goals> <goal>java</goal> @@ -351,6 +352,7 @@ under the License. <phase>generate-sources</phase> </execution> <execution> + <?m2e execute onConfiguration,onIncremental?> <id>modello-cache-xsd</id> <goals> <goal>xsd</goal> @@ -375,6 +377,18 @@ under the License. <artifactId>build-helper-maven-plugin</artifactId> <version>3.4.0</version> <executions> + <execution> + <id>add-sources</id> + <goals> + <goal>add-source</goal> + </goals> + <phase>generate-sources</phase> + <configuration> + <sources> + <source>${basedir}/target/generated-sources/modello</source> + </sources> + </configuration> + </execution> <execution> <id>add-resources</id> <goals> diff --git a/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java b/src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java index 5cef32a..9779a48 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 class CacheControllerImpl implements CacheController { 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 0000000..d2a45d6 --- /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<Path> 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 0000000..4be880d --- /dev/null +++ b/src/test/projects/mbuildcache-74-clean-cache-any-artifact/.mvn/extensions.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Copyright 2023 the original author or authors. + + Licensed 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/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 0000000..1bbaaab --- /dev/null +++ b/src/test/projects/mbuildcache-74-clean-cache-any-artifact/.mvn/maven-build-cache-config.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. +--> + +<cache xmlns="http://maven.apache.org/CACHE-CONFIG/1.0.0"> + <configuration> + <local> + <maxBuildsCached>1</maxBuildsCached> + </local> + </configuration> +</cache> 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 0000000..73e8feb --- /dev/null +++ b/src/test/projects/mbuildcache-74-clean-cache-any-artifact/pom.xml @@ -0,0 +1,60 @@ +<!-- + + Copyright 2023 the original author or authors. + + Licensed 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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.maven.caching.test.mbuildcache-74</groupId> + <artifactId>mbuildcache-74</artifactId> + <version>0.0.1-SNAPSHOT</version> + <packaging>pom</packaging> + + <properties> + <maven.compiler.source>1.8</maven.compiler.source> + <maven.compiler.target>1.8</maven.compiler.target> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-enforcer-plugin</artifactId> + <version>3.4.1</version> + <executions> + <execution> + <id>require-property</id> + <goals> + <goal>enforce</goal> + </goals> + <configuration> + <rules> + <requireProperty> + <property>passed.by.test</property> + <message>Property passed.by.test is required</message> + <regex>\d{3}</regex> + <regexMessage>The passed.by.test property must consist of 3 digits. (actual=${passed.by.test})</regexMessage> + </requireProperty> + </rules> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project>