Skip to content

Commit

Permalink
[MBUILDCACHE-74] Clean local cache for any artifact (#110)
Browse files Browse the repository at this point in the history
Not just the ones which have a package phase. Otherwise the cached
builds will be kept forever.
  • Loading branch information
mweirauch committed Nov 22, 2023
1 parent c872497 commit 7910490
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pom.xml
Expand Up @@ -342,6 +342,7 @@ under the License.
</configuration>
<executions>
<execution>
<?m2e execute onConfiguration,onIncremental?>
<id>modello-cache</id>
<goals>
<goal>java</goal>
Expand All @@ -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>
Expand All @@ -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>
Expand Down
Expand Up @@ -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);
Expand Down
98 changes: 98 additions & 0 deletions 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(",")));
}
}
@@ -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>
@@ -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>
60 changes: 60 additions & 0 deletions 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>

0 comments on commit 7910490

Please sign in to comment.