Skip to content

Commit

Permalink
NCLSUP-631 Reimplement lockfile handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rnc committed Apr 7, 2022
1 parent 0eb7807 commit 5b92712
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jboss.gm.analyzer.alignment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Reader;
import java.net.URISyntaxException;
Expand All @@ -12,7 +11,6 @@
import org.apache.maven.settings.Repository;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
import org.assertj.core.groups.Tuple;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef;
import org.commonjava.maven.ext.common.ManipulationException;
Expand All @@ -36,10 +34,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.core.api.InstanceOfAssertFactories.STRING;
import static org.junit.Assume.assumeTrue;

public class ComplexProjectFunctionalTest extends AbstractWiremockTest {
Expand Down Expand Up @@ -92,31 +88,16 @@ public void ensureAlignmentFileCreated()
assertThat(root.getName()).isEqualTo("complex");
final Collection<ProjectVersionRef> alignedDependencies = root.getAlignedDependencies().values();
assertThat(alignedDependencies)
.hasSize(5)
.hasSize(3)
.extracting("artifactId", "versionString")
.contains(
// ensure that the aligned versions as are always used for dynamic and regular
// dependencies
tuple("undertow-core", "2.0.19.Final-redhat-00001"),
tuple("spring-boot-dependencies", "2.1.4.RELEASE.redhat-3"),
tuple("hibernate-core", "5.3.9.Final-redhat-00001"))
// we can't assert on a specific version because we have used a range as the dependency's
// version
.filteredOn(t -> !getVersion(t).contains("redhat"))
.satisfies(l -> {
// make sure the two dynamic dependencies not aligned exist and have versions similar to
// what we expect
assertThat(l).filteredOn(t -> "HdrHistogram".equals(getArtifactId(t)))
.map(ComplexProjectFunctionalTest::getVersion).singleElement(as(STRING))
.startsWith("2.");
assertThat(l).filteredOn(t -> "commons-lang3".equals(getArtifactId(t)))
.map(ComplexProjectFunctionalTest::getVersion).singleElement(as(STRING))
.startsWith("3.");
});
tuple("hibernate-core", "5.3.9.Final-redhat-00001"));

assertThat(root.getAlignedDependencies()).containsOnlyKeys(
"org.apache.commons:commons-lang3:latest.release",
"org.hdrhistogram:HdrHistogram:2.+",
"org.springframework.boot:spring-boot-dependencies:2.1.4.RELEASE",
"io.undertow:undertow-core:[2.0.0, 2.0.20)",
"org.hibernate:hibernate-core:5.3.7.Final");
Expand All @@ -135,12 +116,4 @@ public void ensureAlignmentFileCreated()
"https://dl.google.com/dl/android/maven2/");
}
}

private static String getArtifactId(Tuple tuple) {
return tuple.toList().get(0).toString();
}

private static String getVersion(Tuple tuple) {
return tuple.toList().get(1).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ public void setup() throws IOException, URISyntaxException {
}

@Test
// Note : if this test has started failing check the latest version of undertow on
// http://central.maven.org/maven2/io/undertow/undertow-core/
public void ensureAlignmentFileCreated() throws IOException, URISyntaxException, ManipulationException {
// XXX: Use of publishing.publications.MavenPublication.versionMapping{}
assumeTrue(GradleVersion.current().compareTo(GradleVersion.version("5.2")) >= 0);
Expand All @@ -86,13 +84,11 @@ public void ensureAlignmentFileCreated() throws IOException, URISyntaxException,
.containsOnly(
// ensure that the aligned versions as are always used for dynamic and regular dependencies
tuple("undertow-core", "2.0.21.Final-redhat-00002"),
tuple("commons-lang3", "3.8-redhat-00001"),
tuple("HdrHistogram", "2.1.10"));
tuple("commons-lang3", "3.8-redhat-00002"));

assertThat(root.getAlignedDependencies()).containsOnlyKeys(
"org.apache.commons:commons-lang3:latest.release",
"org.hdrhistogram:HdrHistogram:2.+",
"io.undertow:undertow-core:2.0+");
"io.undertow:undertow-core:2.0.21.Final");
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import java.util.Collection;

import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef;
import org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef;
import org.commonjava.maven.ext.common.ManipulationException;
import org.commonjava.maven.ext.io.rest.DefaultTranslator;
import org.gradle.api.Project;
import org.gradle.util.GradleVersion;
import org.jboss.gm.analyzer.alignment.TestUtils.TestManipulationModel;
import org.jboss.gm.analyzer.alignment.io.LockFileIO;
import org.jboss.gm.common.Configuration;
import org.jboss.gm.common.utils.FileUtils;
import org.junit.Before;
Expand All @@ -31,7 +33,6 @@
import static junit.framework.TestCase.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.junit.Assert.assertFalse;
import static org.junit.Assume.assumeTrue;

public class DynamicWithLocksProjectFunctionalTest extends AbstractWiremockTest {
Expand Down Expand Up @@ -62,8 +63,6 @@ public void setup() throws IOException, URISyntaxException {
}

@Test
// Note : if this test has started failing check the latest version of undertow on
// http://central.maven.org/maven2/io/undertow/undertow-core/
public void ensureAlignmentFileCreated() throws IOException, URISyntaxException, ManipulationException {
// XXX: Use of publishing.publications.MavenPublication.versionMapping{}
assumeTrue(GradleVersion.current().compareTo(GradleVersion.version("5.2")) >= 0);
Expand Down Expand Up @@ -99,21 +98,26 @@ public void ensureAlignmentFileCreated() throws IOException, URISyntaxException,
// ensure that the aligned versions as are always used for dynamic and regular dependencies
tuple("undertow-core", "2.0.21.Final-redhat-00002"),
tuple("commons-lang3", "3.8-redhat-00001"),
tuple("resteasy-jaxrs", "3.6.3.SP1-redhat-00010"),
tuple("HdrHistogram", "2.1.10"));
tuple("resteasy-jaxrs", "3.6.3.SP1-redhat-00010"));

assertThat(root.getAlignedDependencies()).containsOnlyKeys(
"org.apache.commons:commons-lang3:latest.release",
"org.hdrhistogram:HdrHistogram:2.+",
"io.undertow:undertow-core:2.0+",
"io.undertow:undertow-core:(2.0,2.0.21.Final]",
"org.jboss.resteasy:resteasy-jaxrs:3.6.3.SP1");
});
});

assertFalse(new File(projectRoot, "gradle/dependency-locks/compileClasspath.lockfile").exists());
assertTrue(new File(projectRoot, "gradle/dependency-locks/compileClasspath.lockfile.unused").exists());
assertFalse(new File(projectRoot, "gradle.lockfile").exists());
assertTrue(new File(projectRoot, "gradle.lockfile.unused").exists());
assertThat(
LockFileIO.readProjectVersionRefLocksOfFile(new File(projectRoot, "gradle/dependency-locks"
+ "/compileClasspath.lockfile"))).doesNotContain(
SimpleProjectVersionRef.parse("org.jboss.resteasy:resteasy-jaxrs:3.6.3.SP1"),
SimpleProjectVersionRef.parse("io.undertow:undertow-core:2.0.21.Final"));
assertThat(
LockFileIO.readProjectVersionRefLocksOfFile(new File(projectRoot, "gradle.lockfile"))).doesNotContain(
SimpleProjectVersionRef.parse("org.apache.commons:commons-lang3:3.8"));

assertTrue(new File(projectRoot, "gradle/dependency-locks/compileClasspath.lockfile").exists());
assertTrue(new File(projectRoot, "gradle.lockfile").exists());

// make sure the project name was added
assertEquals("rootProject.name='undertow'",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[
{
"bestMatchVersion": "2.0.21.Final-redhat-00002",
"version": "2.0.21.Final",
"version": "2.0.21.Final-redhat-00001",
"groupId": "io.undertow",
"artifactId": "undertow-core"
},
{
"bestMatchVersion": "3.8-redhat-00001",
"version": "3.8",
"bestMatchVersion": "3.8-redhat-00002",
"version": "3.8-redhat-00001",
"groupId": "org.apache.commons",
"artifactId": "commons-lang3"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ allprojects {


dependencies {
implementation group: 'io.undertow', name: 'undertow-core', version: '2.0.21.Final'
implementation "org.apache.commons:commons-lang3:latest.release"
implementation('org.hdrhistogram:HdrHistogram:2.+')
implementation('com.google.inject:guice:4.2.2')

implementation group: 'io.undertow', name: 'undertow-core', version: '2.0+'
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@
"org.apache.commons:commons-lang3:latest.release" : {
"groupId" : "org.apache.commons",
"artifactId" : "commons-lang3",
"version" : "3.8"
"version" : "3.8-redhat-00001"
},
"io.undertow:undertow-core:2.0+" : {
"io.undertow:undertow-core:2.0.21.Final" : {
"groupId" : "io.undertow",
"artifactId" : "undertow-core",
"version" : "2.0.21.Final-redhat-00002"
},
"org.hdrhistogram:HdrHistogram:2.+" : {
"groupId" : "org.hdrhistogram",
"artifactId" : "HdrHistogram",
"version" : "2.1.10"
"version" : "2.0.21.Final-redhat-00001"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {
implementation('com.google.inject:guice:4.2.2')
implementation('org.jboss.resteasy:resteasy-jaxrs:3.6.3.SP1')

implementation group: 'io.undertow', name: 'undertow-core', version: '2.0+'
implementation group: 'io.undertow', name: 'undertow-core', version: '(2.0,2.0.21.Final]'
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@

import static java.util.Comparator.comparingInt;
import static org.apache.commons.lang.StringUtils.isEmpty;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.gradle.api.Project.DEFAULT_VERSION;
import static org.jboss.gm.common.io.ManipulationIO.writeManipulationModel;
import static org.jboss.gm.common.utils.FileUtils.append;
Expand Down Expand Up @@ -221,8 +220,7 @@ public void perform() {

try {
final Set<ProjectVersionRef> lockFileDeps = LockFileIO
.allProjectVersionRefsFromLockfiles(project.getRootDir().toPath());

.allProjectVersionRefsFromLockfiles(project.getProjectDir());
final Map<RelaxedProjectVersionRef, ProjectVersionRef> dependencies = processAnyExistingManipulationFile(
project,
getDependencies(project, configuration, lockFileDeps));
Expand Down Expand Up @@ -327,10 +325,13 @@ private void align(Configuration configuration, ManipulationCache cache, Manipul
newVersion);
correspondingModule.setVersion(newVersion);
}

updateModuleDynamicDependencies(correspondingModule, value);
updateModuleDependencies(project, correspondingModule, value, alignmentResponse);
LockFileIO.updateLockfiles(logger, project.getProjectDir(), correspondingModule.getAlignedDependencies());
});
// Now need to update the historical lock file format (if it exists). This is one lockfile
// per SCM repository
LockFileIO.updateLockfiles(logger, new File(rootProject.getRootDir(), "gradle/dependency-locks"),
alignmentModel.getAllAlignedDependencies());

// artifactId / rootProject.getName
final String artifactId = SettingsFileIO.writeProjectNameIfNeeded(getProject().getRootDir());
Expand Down Expand Up @@ -379,17 +380,14 @@ private void align(Configuration configuration, ManipulationCache cache, Manipul
writeGmeReposMarkerFile();
writeRepositorySettingsFile(cache.getRepositories());

// this needs to happen for each project, not just the last one
LockFileIO.renameAllLockFiles(rootProject.getRootDir().toPath());

final Set<ProjectVersionRef> nonAligned = new LinkedHashSet<>();
processAlignmentReport(rootProject, configuration, cache, nonAligned);
}

private void processPropertiesForBuildCache(File rootProject) throws IOException {
File properties = new File(rootProject, "gradle.properties");
if (properties.exists()) {
List<String> lines = org.apache.commons.io.FileUtils.readLines(properties, Charset.defaultCharset());
List<String> lines = FileUtils.readLines(properties, Charset.defaultCharset());
if (lines.removeIf(i -> i.contains("org.gradle.caching"))) {
FileUtils.writeLines(properties, lines);
}
Expand Down Expand Up @@ -618,6 +616,7 @@ private Map<RelaxedProjectVersionRef, ProjectVersionRef> getDependencies(Project
version = lockFileDep.getVersionString();
}
}

ProjectVersionRef pvr = ProjectVersionFactory.withGAV(dep.getModuleGroup(), dep.getModuleName(),
version);

Expand Down Expand Up @@ -663,18 +662,6 @@ private Set<UnresolvedDependency> getUnresolvedDependenciesExcludingProjectDepen
return unresolvedDependencies;
}

private void updateModuleDynamicDependencies(ManipulationModel correspondingModule,
Map<RelaxedProjectVersionRef, ProjectVersionRef> allModuleDependencies) {

allModuleDependencies.forEach((d, p) -> {
// we need to make sure that dynamic dependencies are stored with their original key
// in order for the manipulation plugin to be able to look them up properly
if (isNotBlank(d.getVersionString()) && DynamicVersionParser.isDynamic(d.getVersionString())) {
correspondingModule.getAlignedDependencies().put(d.toString(), p);
}
});
}

/**
* This does the actual substitution replacing the dependencies with aligned version if it exists
*
Expand All @@ -697,6 +684,8 @@ private void updateModuleDependencies(Project project, ManipulationModel corresp
newDependencyVersion);
// we need to make sure that dynamic dependencies are stored with their original key
// in order for the manipulation plugin to be able to look them up properly
logger.debug("Mapping {} to {} (and is dynamic {})", d, newVersion,
DynamicVersionParser.isDynamic(d.getVersionString()));
correspondingModule.getAlignedDependencies().put(d.toString(), newVersion);
}
});
Expand Down Expand Up @@ -829,11 +818,8 @@ private void processAlignmentReport(Project project, Configuration configuration

for (Map.Entry<RelaxedProjectVersionRef, ProjectVersionRef> e : allModuleDependenciesEntrySet) {
final RelaxedProjectVersionRef d = e.getKey();
final ProjectVersionRef p = e.getValue();
final ProjectVersionRef newDependencyVersion = correspondingModule.getAlignedDependencies()
.get(d.toString());
logger.debug("In module {} with GAV {} found a replacement version of {}",
correspondingModule.getProjectPathName(), p, newDependencyVersion);

if (newDependencyVersion == null) {
if (reportNonAligned) {
Expand Down
Loading

0 comments on commit 5b92712

Please sign in to comment.