Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MBUILDCACHE-48] - Add possibility to disable caching per module in a multi-module project #51

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,17 @@ public void execute(
CacheState cacheState = DISABLED;
CacheResult result = CacheResult.empty();
boolean skipCache = cacheConfig.isSkipCache() || MavenProjectInput.isSkipCache(project);
boolean cacheIsDisabled = MavenProjectInput.isCacheDisabled(project);
if (source == Source.LIFECYCLE) {
List<MojoExecution> cleanPhase = lifecyclePhasesHelper.getCleanSegment(project, mojoExecutions);
for (MojoExecution mojoExecution : cleanPhase) {
mojoExecutionRunner.run(mojoExecution);
}
cacheState = cacheConfig.initialize();
if (!cacheIsDisabled) {
cacheState = cacheConfig.initialize();
} else {
LOGGER.info("Cache is explicitly disabled on project level for {}", getVersionlessProjectKey(project));
}
if (cacheState == INITIALIZED || skipCache) {
result = cacheController.findCachedBuild(session, project, mojoExecutions, skipCache);
}
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,17 @@ public void save(
localCache.saveArtifactFile(cacheResult, projectArtifact);
}
for (org.apache.maven.artifact.Artifact attachedArtifact : attachedArtifacts) {
if (attachedArtifact.getFile() != null
&& isOutputArtifact(attachedArtifact.getFile().getName())) {
localCache.saveArtifactFile(cacheResult, attachedArtifact);
if (attachedArtifact.getFile() != null) {
boolean storeArtifact =
isOutputArtifact(attachedArtifact.getFile().getName());
if (storeArtifact) {
localCache.saveArtifactFile(cacheResult, attachedArtifact);
} else {
LOGGER.debug(
"Skipping attached project artifact '{}' = "
+ " it is marked for exclusion from caching",
attachedArtifact.getFile().getName());
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import static org.apache.commons.lang3.StringUtils.stripToEmpty;
import static org.apache.maven.buildcache.CacheUtils.isPom;
import static org.apache.maven.buildcache.CacheUtils.isSnapshot;
import static org.apache.maven.buildcache.xml.CacheConfigImpl.CACHE_ENABLED_PROPERTY_NAME;
import static org.apache.maven.buildcache.xml.CacheConfigImpl.CACHE_SKIP;
import static org.apache.maven.buildcache.xml.CacheConfigImpl.RESTORE_GENERATED_SOURCES_PROPERTY_NAME;

Expand Down Expand Up @@ -733,6 +734,12 @@ public int compare(Path f1, Path f2) {
}
}

/**
* Skip lookup on a per-project level via a property to force module rebuild
* e.g.{@code <maven.build.cache.skipCache>true<maven.build.cache.skipCache/>}
* @param project
* @return
*/
public static boolean isSkipCache(MavenProject project) {
return Boolean.parseBoolean(project.getProperties().getProperty(CACHE_SKIP, "false"));
}
Expand All @@ -748,4 +755,15 @@ public static boolean isRestoreGeneratedSources(MavenProject project) {
return Boolean.parseBoolean(
project.getProperties().getProperty(RESTORE_GENERATED_SOURCES_PROPERTY_NAME, "true"));
}

/**
* Allow disabling caching entirely on a per-project level via a property - both artifact lookup and upload
* Defaults to false
* {@code <maven.build.cache.enabled>false<maven.build.cache.enabled/>}
* @param project
* @return
*/
public static boolean isCacheDisabled(MavenProject project) {
return !Boolean.parseBoolean(project.getProperties().getProperty(CACHE_ENABLED_PROPERTY_NAME, "true"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 org.apache.maven.buildcache.its.junit.IntegrationTest;
import org.apache.maven.it.VerificationException;
import org.apache.maven.it.Verifier;
import org.junit.jupiter.api.Test;

@IntegrationTest("src/test/projects/per-module-flags")
public class PerModuleFlagsTest {
private static final String PROJECT_NAME_MODULE1 = "org.apache.maven.caching.test.multimodule:module1";
private static final String PROJECT_NAME_MODULE2 = "org.apache.maven.caching.test.multimodule:module2";
private static final String PROJECT_NAME_MODULE3 = "org.apache.maven.caching.test.multimodule:module3";

@Test
void simple(Verifier verifier) throws VerificationException, IOException {
verifier.setAutoclean(false);

// 1st build
verifier.setLogFileName("../log-1.txt");
verifier.executeGoal("verify");
verifier.verifyErrorFreeLog();

// 2nd build
verifier.setLogFileName("../log-2.txt");
verifier.executeGoal("verify");
verifier.verifyErrorFreeLog();
verifier.verifyTextInLog("Found cached build, restoring " + PROJECT_NAME_MODULE1 + " from cache");
verifier.verifyTextInLog("Project " + PROJECT_NAME_MODULE2
+ " is marked as requiring force rebuild, will skip lookup in build cache");
verifier.verifyTextInLog("Cache is explicitly disabled on project level for " + PROJECT_NAME_MODULE3);
}
}
25 changes: 25 additions & 0 deletions src/test/projects/per-module-flags/.mvn/extensions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2021 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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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">

</cache>
33 changes: 33 additions & 0 deletions src/test/projects/per-module-flags/module1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--

Copyright 2021 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>
<artifactId>module1</artifactId>
<packaging>jar</packaging>

<parent>
<groupId>org.apache.maven.caching.test.multimodule</groupId>
<artifactId>top</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<!-- this subproject should be cached -->
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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;

class Test
{

}
38 changes: 38 additions & 0 deletions src/test/projects/per-module-flags/module2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!--

Copyright 2021 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>
<artifactId>module2</artifactId>
<packaging>jar</packaging>

<parent>
<groupId>org.apache.maven.caching.test.multimodule</groupId>
<artifactId>top</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<!-- this subproject demonstrates usage of maven.build.cache.skipCache flag -
forced rebuild with no lookup-->
<properties>
<maven.build.cache.skipCache>true</maven.build.cache.skipCache>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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;

class Test
{

}
38 changes: 38 additions & 0 deletions src/test/projects/per-module-flags/module3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!--

Copyright 2021 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>
<artifactId>module3</artifactId>
<packaging>jar</packaging>

<parent>
<groupId>org.apache.maven.caching.test.multimodule</groupId>
<artifactId>top</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<!-- this subproject demonstrates usage of maven.build.cache.enabled flag -
disabled caching on per-module level-->
<properties>
<maven.build.cache.enabled>false</maven.build.cache.enabled>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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;

class Test
{

}
Loading