Skip to content

Commit 7910490

Browse files
authored
[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.
1 parent c872497 commit 7910490

File tree

6 files changed

+227
-1
lines changed

6 files changed

+227
-1
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ under the License.
342342
</configuration>
343343
<executions>
344344
<execution>
345+
<?m2e execute onConfiguration,onIncremental?>
345346
<id>modello-cache</id>
346347
<goals>
347348
<goal>java</goal>
@@ -351,6 +352,7 @@ under the License.
351352
<phase>generate-sources</phase>
352353
</execution>
353354
<execution>
355+
<?m2e execute onConfiguration,onIncremental?>
354356
<id>modello-cache-xsd</id>
355357
<goals>
356358
<goal>xsd</goal>
@@ -375,6 +377,18 @@ under the License.
375377
<artifactId>build-helper-maven-plugin</artifactId>
376378
<version>3.4.0</version>
377379
<executions>
380+
<execution>
381+
<id>add-sources</id>
382+
<goals>
383+
<goal>add-source</goal>
384+
</goals>
385+
<phase>generate-sources</phase>
386+
<configuration>
387+
<sources>
388+
<source>${basedir}/target/generated-sources/modello</source>
389+
</sources>
390+
</configuration>
391+
</execution>
378392
<execution>
379393
<id>add-resources</id>
380394
<goals>

src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,10 @@ public void save(
481481
build.getDto().set_final(cacheConfig.isSaveToRemoteFinal());
482482
cacheResults.put(getVersionlessProjectKey(project), rebuilded(cacheResult, build));
483483

484+
localCache.beforeSave(context);
485+
484486
// if package phase presence means new artifacts were packaged
485487
if (project.hasLifecyclePhase("package")) {
486-
localCache.beforeSave(context);
487488
localCache.saveBuildInfo(cacheResult, build);
488489
if (projectArtifact.getFile() != null) {
489490
localCache.saveArtifactFile(cacheResult, projectArtifact);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.buildcache.its;
20+
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.util.List;
26+
import java.util.stream.Collectors;
27+
28+
import org.apache.maven.buildcache.its.junit.IntegrationTest;
29+
import org.apache.maven.buildcache.util.LogFileUtils;
30+
import org.apache.maven.it.VerificationException;
31+
import org.apache.maven.it.Verifier;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.Test;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
36+
37+
/**
38+
* Check if cached builds are cleaned up correctly also for projects
39+
* which don't contain a package phase.
40+
*/
41+
@IntegrationTest("src/test/projects/mbuildcache-74-clean-cache-any-artifact")
42+
public class Issue74Test {
43+
44+
private static final Logger logger = LoggerFactory.getLogger(Issue74Test.class);
45+
46+
@Test
47+
void simple(Verifier verifier) throws VerificationException, IOException {
48+
verifier.setAutoclean(false);
49+
verifier.setMavenDebug(true);
50+
51+
// first run - uncached
52+
verifier.setLogFileName("../log-1.txt");
53+
verifier.setSystemProperty("passed.by.test", "123");
54+
55+
verifier.executeGoal("verify");
56+
57+
verifier.verifyErrorFreeLog();
58+
verifier.verifyTextInLog("Local build was not found");
59+
verifyBuildCacheEntries(verifier, 1);
60+
61+
// second run - modified
62+
verifier.setLogFileName("../log-2.txt");
63+
verifier.setSystemProperty("passed.by.test", "456");
64+
65+
verifier.executeGoal("verify");
66+
67+
verifier.verifyErrorFreeLog();
68+
verifier.verifyTextInLog("Local build was not found");
69+
verifyBuildCacheEntries(verifier, 1);
70+
}
71+
72+
private static void verifyBuildCacheEntries(final Verifier verifier, long expectedBuilds)
73+
throws VerificationException, IOException {
74+
String buildInfoXmlLog =
75+
LogFileUtils.findFirstLineContainingTextsInLogs(verifier, "Saved Build to local file: ");
76+
String buildInfoXmlLocation = buildInfoXmlLog.split(":\\s")[1];
77+
78+
Path buildInfoXmlPath = Paths.get(buildInfoXmlLocation);
79+
// buildinfo.xml -> local -> hash -> project
80+
Path projectPathInCache = buildInfoXmlPath.getParent().getParent().getParent();
81+
82+
logger.info("Checking '{}' for cached builds ...", projectPathInCache);
83+
84+
if (!Files.exists(projectPathInCache)) {
85+
throw new VerificationException(
86+
String.format("Project directory in build cache doesn't exist: '%s'", projectPathInCache));
87+
}
88+
89+
List<Path> entries =
90+
Files.list(projectPathInCache).filter(p -> Files.isDirectory(p)).collect(Collectors.toList());
91+
92+
Assertions.assertEquals(
93+
expectedBuilds,
94+
entries.size(),
95+
"Expected amount of cached builds not satisfied. Found: "
96+
+ entries.stream().map(p -> p.getFileName().toString()).collect(Collectors.joining(",")));
97+
}
98+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2023 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<extensions>
20+
<extension>
21+
<groupId>org.apache.maven.extensions</groupId>
22+
<artifactId>maven-build-cache-extension</artifactId>
23+
<version>${projectVersion}</version>
24+
</extension>
25+
</extensions>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<cache xmlns="http://maven.apache.org/CACHE-CONFIG/1.0.0">
23+
<configuration>
24+
<local>
25+
<maxBuildsCached>1</maxBuildsCached>
26+
</local>
27+
</configuration>
28+
</cache>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!--
2+
3+
Copyright 2023 the original author or authors.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
-->
18+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20+
21+
<modelVersion>4.0.0</modelVersion>
22+
<groupId>org.apache.maven.caching.test.mbuildcache-74</groupId>
23+
<artifactId>mbuildcache-74</artifactId>
24+
<version>0.0.1-SNAPSHOT</version>
25+
<packaging>pom</packaging>
26+
27+
<properties>
28+
<maven.compiler.source>1.8</maven.compiler.source>
29+
<maven.compiler.target>1.8</maven.compiler.target>
30+
</properties>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-enforcer-plugin</artifactId>
37+
<version>3.4.1</version>
38+
<executions>
39+
<execution>
40+
<id>require-property</id>
41+
<goals>
42+
<goal>enforce</goal>
43+
</goals>
44+
<configuration>
45+
<rules>
46+
<requireProperty>
47+
<property>passed.by.test</property>
48+
<message>Property passed.by.test is required</message>
49+
<regex>\d{3}</regex>
50+
<regexMessage>The passed.by.test property must consist of 3 digits. (actual=${passed.by.test})</regexMessage>
51+
</requireProperty>
52+
</rules>
53+
</configuration>
54+
</execution>
55+
</executions>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>

0 commit comments

Comments
 (0)