diff --git a/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java b/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java index 4b75dd45..b6df5ec6 100644 --- a/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java +++ b/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java @@ -180,7 +180,11 @@ public ProjectsInputInfo calculateChecksum() throws IOException { final long t1 = System.currentTimeMillis(); // hash items: effective pom + version + input files + dependencies - final int count = 2 + inputFiles.size() + dependenciesChecksum.size(); + final int count = 1 + + (config.calculateProjectVersionChecksum() ? 1 : 0) + + inputFiles.size() + + dependenciesChecksum.size(); + final List items = new ArrayList<>(count); final HashChecksum checksum = config.getHashFactory().createChecksum(count); @@ -190,13 +194,15 @@ public ProjectsInputInfo calculateChecksum() throws IOException { remoteCache.findBaselineBuild(project).map(b -> b.getDto().getProjectsInputInfo()); } - DigestItem projectVersion = new DigestItem(); - projectVersion.setType("version"); - projectVersion.setIsText("yes"); - projectVersion.setValue(project.getVersion()); - items.add(projectVersion); + if (config.calculateProjectVersionChecksum()) { + DigestItem projectVersion = new DigestItem(); + projectVersion.setType("version"); + projectVersion.setIsText("yes"); + projectVersion.setValue(project.getVersion()); + items.add(projectVersion); - checksum.update(project.getVersion().getBytes(StandardCharsets.UTF_8)); + checksum.update(project.getVersion().getBytes(StandardCharsets.UTF_8)); + } DigestItem effectivePomChecksum = DigestUtils.pom(checksum, effectivePom); items.add(effectivePomChecksum); diff --git a/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java b/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java index e0b8e63a..2569a167 100644 --- a/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java +++ b/src/main/java/org/apache/maven/buildcache/xml/CacheConfig.java @@ -107,6 +107,8 @@ public interface CacheConfig { boolean adjustMetaInfVersion(); + boolean calculateProjectVersionChecksum(); + boolean canIgnore(MojoExecution mojoExecution); @Nonnull diff --git a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java index 55f44657..75b16afc 100644 --- a/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java +++ b/src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java @@ -551,6 +551,17 @@ public boolean adjustMetaInfVersion() { } } + @Override + public boolean calculateProjectVersionChecksum() { + if (isEnabled()) { + return Optional.ofNullable(getConfiguration().getProjectVersioning()) + .map(ProjectVersioning::isCalculateProjectVersionChecksum) + .orElse(false); + } else { + return false; + } + } + @Nonnull @Override public List getExcludePatterns() { diff --git a/src/main/mdo/build-cache-config.mdo b/src/main/mdo/build-cache-config.mdo index 9f8b2ab4..65f2ec7b 100644 --- a/src/main/mdo/build-cache-config.mdo +++ b/src/main/mdo/build-cache-config.mdo @@ -325,6 +325,11 @@ under the License. boolean Determines whether version in metadata must be changed according to building project version. + + calculateProjectVersionChecksum + boolean + Determines whether project version should be included to calculate checksum input. + @@ -339,6 +344,12 @@ under the License. Determines whether version in metadata must be changed according to building project version. + + + 0.0.0+ + Determines whether project version should be included to calculate checksum input. + + --> diff --git a/src/test/java/org/apache/maven/buildcache/its/CoreExtensionTest.java b/src/test/java/org/apache/maven/buildcache/its/CoreExtensionTest.java index cdd5b832..32142666 100644 --- a/src/test/java/org/apache/maven/buildcache/its/CoreExtensionTest.java +++ b/src/test/java/org/apache/maven/buildcache/its/CoreExtensionTest.java @@ -43,17 +43,19 @@ void simple(Verifier verifier) throws VerificationException { } @Test - void simple_build_change_version_build_install_again(Verifier verifier) throws VerificationException { + void simple_build_change_version_reuse_build_cache(Verifier verifier) throws VerificationException { verifier.setAutoclean(false); verifier.setLogFileName("../log-1.txt"); verifier.executeGoal("install"); verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Saved Build to local file"); verifier.verifyArtifactPresent("org.apache.maven.caching.test.simple", "simple", "0.0.1-SNAPSHOT", "jar"); verifier.setLogFileName("../log-2.txt"); verifier.executeGoal("install"); verifier.verifyErrorFreeLog(); + verifier.verifyArtifactPresent("org.apache.maven.caching.test.simple", "simple", "0.0.1-SNAPSHOT", "jar"); verifier.verifyTextInLog("Found cached build, restoring " + PROJECT_NAME + " from cache"); verifier.setLogFileName("../log-3.txt"); @@ -64,14 +66,11 @@ void simple_build_change_version_build_install_again(Verifier verifier) throws V verifier.verifyErrorFreeLog(); verifier.getCliOptions().clear(); + verifier.addCliOption("-Dmaven.build.cache.alwaysRunPlugins=maven-install-plugin:install"); verifier.setLogFileName("../log-4.txt"); verifier.executeGoal("install"); verifier.verifyErrorFreeLog(); verifier.verifyArtifactPresent("org.apache.maven.caching.test.simple", "simple", "0.0.2-SNAPSHOT", "jar"); - - verifier.setLogFileName("../log-5.txt"); - verifier.executeGoal("install"); - verifier.verifyErrorFreeLog(); verifier.verifyTextInLog("Found cached build, restoring " + PROJECT_NAME + " from cache"); } } diff --git a/src/test/java/org/apache/maven/buildcache/its/Issue76Test.java b/src/test/java/org/apache/maven/buildcache/its/Issue76Test.java new file mode 100644 index 00000000..b6b6b302 --- /dev/null +++ b/src/test/java/org/apache/maven/buildcache/its/Issue76Test.java @@ -0,0 +1,65 @@ +/* + * 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 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/mbuildcache-76") +public class Issue76Test { + + private static final String PROJECT_NAME = "org.apache.maven.caching.test:mbuildcache-76"; + + @Test + void simple_build_change_version_build_install_again(Verifier verifier) throws VerificationException { + verifier.setAutoclean(false); + + verifier.setLogFileName("../log-1.txt"); + verifier.executeGoal("install"); + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Saved Build to local file"); + verifier.verifyArtifactPresent("org.apache.maven.caching.test", "mbuildcache-76", "0.0.1-SNAPSHOT", "jar"); + + verifier.setLogFileName("../log-2.txt"); + verifier.executeGoal("install"); + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Found cached build, restoring " + PROJECT_NAME + " from cache"); + + verifier.setLogFileName("../log-3.txt"); + verifier.getCliOptions().clear(); + verifier.addCliOption("-DoldVersion=0.0.1-SNAPSHOT"); + verifier.addCliOption("-DnewVersion=0.0.2-SNAPSHOT"); + verifier.executeGoal("versions:set"); + verifier.verifyErrorFreeLog(); + + verifier.getCliOptions().clear(); + verifier.setLogFileName("../log-4.txt"); + verifier.executeGoal("install"); + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Saved Build to local file"); + verifier.verifyArtifactPresent("org.apache.maven.caching.test", "mbuildcache-76", "0.0.2-SNAPSHOT", "jar"); + + verifier.setLogFileName("../log-5.txt"); + verifier.executeGoal("install"); + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Found cached build, restoring " + PROJECT_NAME + " from cache"); + } +} diff --git a/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java b/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java index 7d692641..8b19884c 100644 --- a/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java +++ b/src/test/java/org/apache/maven/buildcache/xml/CacheConfigImplTest.java @@ -140,6 +140,7 @@ private void assertDefaults(Pair... overrides) { private void assertDefaults(Map overrides) { Map asserts = new HashMap<>(); asserts.put("adjustMetaInfVersion", () -> assertFalse(testObject.adjustMetaInfVersion())); + asserts.put("calculateProjectVersionChecksum", () -> assertFalse(testObject.calculateProjectVersionChecksum())); asserts.put("canIgnore", () -> assertFalse(testObject.canIgnore(mock(MojoExecution.class)))); asserts.put("getAlwaysRunPlugins", () -> assertNull(testObject.getAlwaysRunPlugins())); asserts.put("getAttachedOutputs", () -> assertEquals(Collections.emptyList(), testObject.getAttachedOutputs())); diff --git a/src/test/projects/mbuildcache-76/.mvn/extensions.xml b/src/test/projects/mbuildcache-76/.mvn/extensions.xml new file mode 100644 index 00000000..8df78f8b --- /dev/null +++ b/src/test/projects/mbuildcache-76/.mvn/extensions.xml @@ -0,0 +1,25 @@ + + + + + org.apache.maven.extensions + maven-build-cache-extension + ${projectVersion} + + diff --git a/src/test/projects/mbuildcache-76/.mvn/maven-build-cache-config.xml b/src/test/projects/mbuildcache-76/.mvn/maven-build-cache-config.xml new file mode 100644 index 00000000..67b961c0 --- /dev/null +++ b/src/test/projects/mbuildcache-76/.mvn/maven-build-cache-config.xml @@ -0,0 +1,26 @@ + + + + + + + + + diff --git a/src/test/projects/mbuildcache-76/pom.xml b/src/test/projects/mbuildcache-76/pom.xml new file mode 100644 index 00000000..7c973233 --- /dev/null +++ b/src/test/projects/mbuildcache-76/pom.xml @@ -0,0 +1,48 @@ + + + + 4.0.0 + org.apache.maven.caching.test + mbuildcache-76 + 0.0.1-SNAPSHOT + jar + + + 1.8 + 1.8 + + + + + + + + org.codehaus.mojo + versions-maven-plugin + 2.16.0 + + false + + + + + + + diff --git a/src/test/projects/mbuildcache-76/src/main/java/org/apache/maven/buildcache/Test.java b/src/test/projects/mbuildcache-76/src/main/java/org/apache/maven/buildcache/Test.java new file mode 100644 index 00000000..03f66a82 --- /dev/null +++ b/src/test/projects/mbuildcache-76/src/main/java/org/apache/maven/buildcache/Test.java @@ -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 +{ + +} \ No newline at end of file