Skip to content

Commit

Permalink
[MBUILDCACHE-81] - Add an option to include project version as part o…
Browse files Browse the repository at this point in the history
…f the cache hash key (#129)

* Fix MBUILDCACHE-76 regression with process version checksum calculation

* Update Issue76Test project name to mbuildcache-76
  • Loading branch information
igdianov authored Feb 2, 2024
1 parent 951a788 commit 8980885
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DigestItem> items = new ArrayList<>(count);
final HashChecksum checksum = config.getHashFactory().createChecksum(count);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public interface CacheConfig {

boolean adjustMetaInfVersion();

boolean calculateProjectVersionChecksum();

boolean canIgnore(MojoExecution mojoExecution);

@Nonnull
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/apache/maven/buildcache/xml/CacheConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pattern> getExcludePatterns() {
Expand Down
11 changes: 11 additions & 0 deletions src/main/mdo/build-cache-config.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ under the License.
<type>boolean</type>
<description>Determines whether version in metadata must be changed according to building project version.</description>
</field>
<field xml.attribute="true">
<name>calculateProjectVersionChecksum</name>
<type>boolean</type>
<description>Determines whether project version should be included to calculate checksum input.</description>
</field>
</fields>
</class>

Expand All @@ -339,6 +344,12 @@ under the License.
<xs:documentation source="description">Determines whether version in metadata must be changed according to building project version.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="calculateProjectVersionChecksum" type="xs:boolean" default="false" use="optional">
<xs:annotation>
<xs:documentation source="version">0.0.0+</xs:documentation>
<xs:documentation source="description">Determines whether project version should be included to calculate checksum input.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
-->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
}
}
65 changes: 65 additions & 0 deletions src/test/java/org/apache/maven/buildcache/its/Issue76Test.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ private void assertDefaults(Pair<String, Runnable>... overrides) {
private void assertDefaults(Map<String, Runnable> overrides) {
Map<String, Runnable> 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()));
Expand Down
25 changes: 25 additions & 0 deletions src/test/projects/mbuildcache-76/.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>
26 changes: 26 additions & 0 deletions src/test/projects/mbuildcache-76/.mvn/maven-build-cache-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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">
<configuration>
<projectVersioning calculateProjectVersionChecksum="true"/>
</configuration>
</cache>
48 changes: 48 additions & 0 deletions src/test/projects/mbuildcache-76/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--
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>
<groupId>org.apache.maven.caching.test</groupId>
<artifactId>mbuildcache-76</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>


<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.16.0</version>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

</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
{

}

0 comments on commit 8980885

Please sign in to comment.