Skip to content

Commit 6461415

Browse files
committed
Mandatory clean option to store in cache
1 parent f9920a1 commit 6461415

File tree

12 files changed

+371
-2
lines changed

12 files changed

+371
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,15 @@ public void execute(
140140
}
141141

142142
if (cacheState == INITIALIZED && (!restorable || !restored)) {
143-
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
144-
cacheController.save(result, mojoExecutions, executionEvents);
143+
if (cacheConfig.isMandatoryClean()
144+
&& lifecyclePhasesHelper
145+
.getCleanSegment(project, mojoExecutions)
146+
.isEmpty()) {
147+
LOGGER.info("Cache storing is skipped since there was no \"clean\" phase.");
148+
} else {
149+
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
150+
cacheController.save(result, mojoExecutions, executionEvents);
151+
}
145152
}
146153

147154
if (cacheConfig.isFailFast() && !result.isSuccess() && !skipCache && !forkedExecution) {

src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,9 @@ public interface CacheConfig {
132132
boolean isRestoreGeneratedSources();
133133

134134
String getAlwaysRunPlugins();
135+
136+
/**
137+
* Flag to save in cache only if a build went through the clean lifecycle
138+
*/
139+
boolean isMandatoryClean();
135140
}

src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ public String getAlwaysRunPlugins() {
503503
return getProperty(ALWAYS_RUN_PLUGINS, null);
504504
}
505505

506+
@Override
507+
public boolean isMandatoryClean() {
508+
return getConfiguration().isMandatoryClean();
509+
}
510+
506511
@Override
507512
public String getId() {
508513
checkInitializedState();

src/main/mdo/build-cache-config.mdo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ under the License.
194194
TODO: not implemented
195195
</description>
196196
</field>
197+
<field>
198+
<name>mandatoryClean</name>
199+
<type>boolean</type>
200+
<defaultValue>false</defaultValue>
201+
<description>Enable the cache storing ability only if a build went through the clean lifecycle.</description>
202+
</field>
197203
<field>
198204
<name>multiModule</name>
199205
<association>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.util.ArrayList;
22+
import java.util.Arrays;
23+
import java.util.Iterator;
24+
import java.util.List;
25+
26+
import org.apache.maven.buildcache.its.junit.IntegrationTest;
27+
import org.apache.maven.it.VerificationException;
28+
import org.apache.maven.it.Verifier;
29+
import org.junit.jupiter.api.Assertions;
30+
import org.junit.jupiter.api.Test;
31+
32+
import static org.junit.jupiter.api.Assertions.assertThrows;
33+
34+
/**
35+
* Test the "mandatoryClean" parameter : saving in cache should be done only if a clean phase has been executed.
36+
*/
37+
@IntegrationTest("src/test/projects/mandatory-clean")
38+
public class MandatoryCleanTest {
39+
40+
private static final String MODULE_NAME_1 = "org.apache.maven.caching.test.simple:non-forked-module";
41+
private static final String MODULE_NAME_2 = "org.apache.maven.caching.test.simple:forked-module";
42+
private static final String CACHE_BUILD_LOG = "Found cached build, restoring %s from cache";
43+
44+
@Test
45+
void simple(Verifier verifier) throws VerificationException {
46+
47+
verifier.setAutoclean(false);
48+
// verifier.setMavenDebug(true);
49+
50+
verifier.setLogFileName("../log-1.txt");
51+
verifier.executeGoal("verify");
52+
verifier.verifyErrorFreeLog();
53+
List<String> cacheSkippedBuild1 = findLinesContainingTextsInLogs(
54+
verifier, "Cache storing is skipped since there was no \"clean\" phase.");
55+
Assertions.assertEquals(2, cacheSkippedBuild1.size(), "Expected 2 skipped module caching");
56+
57+
assertThrows(
58+
VerificationException.class,
59+
() -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
60+
"not expected to be loaded from the cache");
61+
assertThrows(
62+
VerificationException.class,
63+
() -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
64+
"not expected to be loaded from the cache");
65+
66+
verifier.setLogFileName("../log-2.txt");
67+
verifier.executeGoals(Arrays.asList("clean", "verify"));
68+
verifier.verifyErrorFreeLog();
69+
List<String> cacheSkippedBuild2 = findLinesContainingTextsInLogs(
70+
verifier, "Cache storing is skipped since there was no \"clean\" phase.");
71+
Assertions.assertEquals(0, cacheSkippedBuild2.size(), "Expected 2 skipped module caching");
72+
List<String> notCachedBuild2 = findLinesContainingTextsInLogs(verifier, CACHE_BUILD_LOG);
73+
assertThrows(
74+
VerificationException.class,
75+
() -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
76+
"not expected to be loaded from the cache");
77+
assertThrows(
78+
VerificationException.class,
79+
() -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
80+
"not expected to be loaded from the cache");
81+
82+
verifier.setLogFileName("../log-3.txt");
83+
verifier.executeGoal("verify");
84+
verifier.verifyErrorFreeLog();
85+
List<String> cacheSkippedBuild3 = findLinesContainingTextsInLogs(
86+
verifier, "Cache storing is skipped since there was no \"clean\" phase.");
87+
Assertions.assertEquals(0, cacheSkippedBuild3.size(), "loading from cache, no more caching required");
88+
// Expect to find and restore cached project
89+
verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1));
90+
verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2));
91+
}
92+
93+
/**
94+
* TODO : put this function in LogFileUtils when its merged (from another pr)
95+
* @param verifier
96+
* @param texts
97+
* @return
98+
* @throws VerificationException
99+
*/
100+
private static List<String> findLinesContainingTextsInLogs(final Verifier verifier, final String... texts)
101+
throws VerificationException {
102+
List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
103+
List<String> result = new ArrayList<>();
104+
Iterator it = lines.iterator();
105+
106+
while (it.hasNext()) {
107+
String line = verifier.stripAnsi((String) it.next());
108+
boolean matches = true;
109+
Iterator<String> toMatchIterator = Arrays.stream(texts).iterator();
110+
while (matches && toMatchIterator.hasNext()) {
111+
matches = line.contains(toMatchIterator.next());
112+
}
113+
if (matches) {
114+
result.add(line);
115+
}
116+
}
117+
118+
return result;
119+
}
120+
}
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 2021 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+
24+
<configuration>
25+
<mandatoryClean>true</mandatoryClean>
26+
</configuration>
27+
28+
</cache>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!--
2+
3+
Copyright 2021 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+
<parent>
22+
<groupId>org.apache.maven.caching.test.simple</groupId>
23+
<artifactId>parent-multi-module</artifactId>
24+
<version>0.0.1-SNAPSHOT</version>
25+
</parent>
26+
<modelVersion>4.0.0</modelVersion>
27+
<groupId>org.apache.maven.caching.test.simple</groupId>
28+
<artifactId>forked-module</artifactId>
29+
<version>0.0.1-SNAPSHOT</version>
30+
<packaging>jar</packaging>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-pmd-plugin</artifactId>
37+
<version>3.19.0</version>
38+
<executions>
39+
<execution>
40+
<goals>
41+
<goal>check</goal>
42+
<goal>pmd</goal>
43+
</goals>
44+
</execution>
45+
</executions>
46+
<dependencies>
47+
<dependency>
48+
<groupId>org.apache.maven.wagon</groupId>
49+
<artifactId>wagon-http</artifactId>
50+
<version>3.5.2</version>
51+
</dependency>
52+
</dependencies>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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;
20+
21+
class ForkedExecutionClass
22+
{
23+
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!--
2+
3+
Copyright 2021 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+
<parent>
22+
<groupId>org.apache.maven.caching.test.simple</groupId>
23+
<artifactId>parent-multi-module</artifactId>
24+
<version>0.0.1-SNAPSHOT</version>
25+
</parent>
26+
<modelVersion>4.0.0</modelVersion>
27+
<groupId>org.apache.maven.caching.test.simple</groupId>
28+
<artifactId>non-forked-module</artifactId>
29+
<version>0.0.1-SNAPSHOT</version>
30+
<packaging>jar</packaging>
31+
32+
</project>

0 commit comments

Comments
 (0)