Skip to content

Commit b66e7ce

Browse files
committed
[PR-103] Mandatory clean option to store in cache
1 parent e85610d commit b66e7ce

File tree

13 files changed

+371
-2
lines changed

13 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
@@ -148,8 +148,15 @@ public void execute(
148148
}
149149

150150
if (cacheState == INITIALIZED && (!restorable || !restored)) {
151-
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
152-
cacheController.save(result, mojoExecutions, executionEvents);
151+
if (cacheConfig.isMandatoryClean()
152+
&& lifecyclePhasesHelper
153+
.getCleanSegment(project, mojoExecutions)
154+
.isEmpty()) {
155+
LOGGER.info("Cache storing is skipped since there was no \"clean\" phase.");
156+
} else {
157+
final Map<String, MojoExecutionEvent> executionEvents = mojoListener.getProjectExecutions(project);
158+
cacheController.save(result, mojoExecutions, executionEvents);
159+
}
153160
}
154161

155162
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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.Arrays;
22+
import java.util.List;
23+
24+
import org.apache.maven.buildcache.its.junit.IntegrationTest;
25+
import org.apache.maven.buildcache.util.LogFileUtils;
26+
import org.apache.maven.it.VerificationException;
27+
import org.apache.maven.it.Verifier;
28+
import org.junit.jupiter.api.Assertions;
29+
import org.junit.jupiter.api.Test;
30+
31+
import static org.junit.jupiter.api.Assertions.assertThrows;
32+
33+
/**
34+
* Test the "mandatoryClean" parameter : saving in cache should be done only if a clean phase has been executed.
35+
*/
36+
@IntegrationTest("src/test/projects/mandatory-clean")
37+
public class MandatoryCleanTest {
38+
39+
private static final String MODULE_NAME_1 = "org.apache.maven.caching.test.simple:non-forked-module";
40+
private static final String MODULE_NAME_2 = "org.apache.maven.caching.test.simple:forked-module";
41+
private static final String CACHE_BUILD_LOG = "Found cached build, restoring %s from cache";
42+
43+
@Test
44+
void simple(Verifier verifier) throws VerificationException {
45+
46+
verifier.setAutoclean(false);
47+
// verifier.setMavenDebug(true);
48+
49+
verifier.setLogFileName("../log-1.txt");
50+
verifier.executeGoal("verify");
51+
verifier.verifyErrorFreeLog();
52+
List<String> cacheSkippedBuild1 = LogFileUtils.findLinesContainingTextsInLogs(
53+
verifier, "Cache storing is skipped since there was no \"clean\" phase.");
54+
Assertions.assertEquals(2, cacheSkippedBuild1.size(), "Expected 2 skipped module caching");
55+
56+
assertThrows(
57+
VerificationException.class,
58+
() -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
59+
"not expected to be loaded from the cache");
60+
assertThrows(
61+
VerificationException.class,
62+
() -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
63+
"not expected to be loaded from the cache");
64+
65+
verifier.setLogFileName("../log-2.txt");
66+
verifier.executeGoals(Arrays.asList("clean", "verify"));
67+
verifier.verifyErrorFreeLog();
68+
List<String> cacheSkippedBuild2 = LogFileUtils.findLinesContainingTextsInLogs(
69+
verifier, "Cache storing is skipped since there was no \"clean\" phase.");
70+
Assertions.assertEquals(0, cacheSkippedBuild2.size(), "Expected 2 skipped module caching");
71+
List<String> notCachedBuild2 = LogFileUtils.findLinesContainingTextsInLogs(verifier, CACHE_BUILD_LOG);
72+
assertThrows(
73+
VerificationException.class,
74+
() -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1)),
75+
"not expected to be loaded from the cache");
76+
assertThrows(
77+
VerificationException.class,
78+
() -> verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2)),
79+
"not expected to be loaded from the cache");
80+
81+
verifier.setLogFileName("../log-3.txt");
82+
verifier.executeGoal("verify");
83+
verifier.verifyErrorFreeLog();
84+
List<String> cacheSkippedBuild3 = LogFileUtils.findLinesContainingTextsInLogs(
85+
verifier, "Cache storing is skipped since there was no \"clean\" phase.");
86+
Assertions.assertEquals(0, cacheSkippedBuild3.size(), "loading from cache, no more caching required");
87+
// Expect to find and restore cached project
88+
verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_1));
89+
verifier.verifyTextInLog(String.format(CACHE_BUILD_LOG, MODULE_NAME_2));
90+
}
91+
}

src/test/java/org/apache/maven/buildcache/util/LogFileUtils.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.apache.maven.buildcache.util;
2020

21+
import java.util.ArrayList;
2122
import java.util.Arrays;
2223
import java.util.Iterator;
2324
import java.util.List;
@@ -60,4 +61,32 @@ public static String findFirstLineContainingTextsInLogs(final Verifier verifier,
6061

6162
return null;
6263
}
64+
65+
/**
66+
* Find lines matching all the strings given as parameter in the log file attached to a verifier
67+
* @param verifier the maven verifier instance
68+
* @param texts all the matching strings to find
69+
* @return a list of matching strings
70+
* @throws VerificationException
71+
*/
72+
public static List<String> findLinesContainingTextsInLogs(final Verifier verifier, final String... texts)
73+
throws VerificationException {
74+
List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false);
75+
List<String> result = new ArrayList<>();
76+
Iterator it = lines.iterator();
77+
78+
while (it.hasNext()) {
79+
String line = verifier.stripAnsi((String) it.next());
80+
boolean matches = true;
81+
Iterator<String> toMatchIterator = Arrays.stream(texts).iterator();
82+
while (matches && toMatchIterator.hasNext()) {
83+
matches = line.contains(toMatchIterator.next());
84+
}
85+
if (matches) {
86+
result.add(line);
87+
}
88+
}
89+
90+
return result;
91+
}
6392
}
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+
}

0 commit comments

Comments
 (0)