Skip to content

Commit

Permalink
[MBUILDCACHE-76] add project version as part of hash key (#117)
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Lamy <[email protected]>
  • Loading branch information
olamy authored Dec 23, 2023
1 parent fe301b9 commit fded5cc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ public DefaultProjectInputCalculator(

@Override
public ProjectsInputInfo calculateInput(MavenProject project) {
LOGGER.info("Going to calculate checksum for project [groupId=" + project.getGroupId() + ", artifactId="
+ project.getArtifactId() + "]");
LOGGER.info(
"Going to calculate checksum for project [groupId={}, artifactId={}, version={}]",
project.getGroupId(),
project.getArtifactId(),
project.getVersion());

String key = BuilderCommon.getKey(project);
// NOTE: Do not use ConcurrentHashMap.computeIfAbsent() here because of recursive calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
Expand Down Expand Up @@ -178,8 +179,8 @@ public ProjectsInputInfo calculateChecksum() throws IOException {

final long t1 = System.currentTimeMillis();

// hash items: effective pom + input files + dependencies
final int count = 1 + inputFiles.size() + dependenciesChecksum.size();
// hash items: effective pom + version + input files + dependencies
final int count = 2 + inputFiles.size() + dependenciesChecksum.size();
final List<DigestItem> items = new ArrayList<>(count);
final HashChecksum checksum = config.getHashFactory().createChecksum(count);

Expand All @@ -189,6 +190,14 @@ 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);

checksum.update(project.getVersion().getBytes(StandardCharsets.UTF_8));

DigestItem effectivePomChecksum = DigestUtils.pom(checksum, effectivePom);
items.add(effectivePomChecksum);
final boolean compareWithBaseline = config.isBaselineDiffEnabled() && baselineHolder.isPresent();
Expand Down Expand Up @@ -227,9 +236,12 @@ public ProjectsInputInfo calculateChecksum() throws IOException {

final long t2 = System.currentTimeMillis();

for (DigestItem item : projectsInputInfoType.getItems()) {
LOGGER.debug("Hash calculated, item: {}, hash: {}", item.getType(), item.getHash());
if (LOGGER.isDebugEnabled()) {
for (DigestItem item : projectsInputInfoType.getItems()) {
LOGGER.debug("Hash calculated, item: {}, hash: {}", item.getType(), item.getHash());
}
}

LOGGER.info(
"Project inputs calculated in {} ms. {} checksum [{}] calculated in {} ms.",
t1 - t0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,37 @@ void simple(Verifier verifier) throws VerificationException {
verifier.verifyErrorFreeLog();
verifier.verifyTextInLog("Found cached build, restoring " + PROJECT_NAME + " from cache");
}

@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.verifyArtifactPresent("org.apache.maven.caching.test.simple", "simple", "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.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");
}
}
16 changes: 16 additions & 0 deletions src/test/projects/core-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,20 @@
<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>

0 comments on commit fded5cc

Please sign in to comment.