diff --git a/analyzer/src/functTest/java/org/jboss/gm/analyzer/alignment/DynamicWithLocksProjectFunctionalTest.java b/analyzer/src/functTest/java/org/jboss/gm/analyzer/alignment/DynamicWithLocksProjectFunctionalTest.java index f43e9574..5764fdd0 100644 --- a/analyzer/src/functTest/java/org/jboss/gm/analyzer/alignment/DynamicWithLocksProjectFunctionalTest.java +++ b/analyzer/src/functTest/java/org/jboss/gm/analyzer/alignment/DynamicWithLocksProjectFunctionalTest.java @@ -31,6 +31,7 @@ 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 { @@ -75,6 +76,9 @@ public void ensureAlignmentFileCreated() throws IOException, URISyntaxException, final File gitDir = new File(projectRoot, "dotgit"); Files.move(gitDir.toPath(), projectRoot.toPath().resolve(".git")); + + assertTrue(new File(projectRoot, "gradle/dependency-locks/compileClasspath.lockfile").exists()); + final TestManipulationModel alignmentModel = TestUtils.align(projectRoot, projectRoot.getName(), false); assertTrue(new File(projectRoot, AlignmentTask.GME).exists()); @@ -106,6 +110,11 @@ public void ensureAlignmentFileCreated() throws IOException, URISyntaxException, }); }); + 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()); + // make sure the project name was added assertEquals("rootProject.name='undertow'", FileUtils.getLastLine(new File(projectRoot, "settings.gradle"))); diff --git a/analyzer/src/functTest/resources/dynamic-project-with-locks/gradle.lockfile b/analyzer/src/functTest/resources/dynamic-project-with-locks/gradle.lockfile new file mode 100644 index 00000000..c13764f0 --- /dev/null +++ b/analyzer/src/functTest/resources/dynamic-project-with-locks/gradle.lockfile @@ -0,0 +1,2 @@ +# Dummy similar to what gradle writes when --write-locks is enabled +org.apache.commons:commons-lang3:3.8=classpath diff --git a/analyzer/src/functTest/resources/dynamic-project-with-locks/gradle/dependency-locks/compileClasspath.lockfile b/analyzer/src/functTest/resources/dynamic-project-with-locks/gradle/dependency-locks/compileClasspath.lockfile index 3a0d3850..81a6ee0b 100644 --- a/analyzer/src/functTest/resources/dynamic-project-with-locks/gradle/dependency-locks/compileClasspath.lockfile +++ b/analyzer/src/functTest/resources/dynamic-project-with-locks/gradle/dependency-locks/compileClasspath.lockfile @@ -1,7 +1,6 @@ # Dummy similar to what gradle writes when --write-locks is enabled io.undertow:undertow-core:2.0.21.Final -org.apache.commons:commons-lang3:3.8 -org.hdrhistogram:HdrHistogram:2.1.10 +org.hdrhistogram:HdrHistogram:2.1.10=compileClasspath,classpath # there are more transitives added by gradle, we only add one in order to not distract com.google.guava:guava:25.1-android org.jboss.resteasy:resteasy-jaxrs:3.6.3.SP1 diff --git a/analyzer/src/main/java/org/jboss/gm/analyzer/alignment/AlignmentTask.java b/analyzer/src/main/java/org/jboss/gm/analyzer/alignment/AlignmentTask.java index 0f797419..d4695d42 100644 --- a/analyzer/src/main/java/org/jboss/gm/analyzer/alignment/AlignmentTask.java +++ b/analyzer/src/main/java/org/jboss/gm/analyzer/alignment/AlignmentTask.java @@ -187,6 +187,7 @@ public void perform() { // If processing the root project _and_ we have a Maven publication configured then verify artifactId / groupId. Project rootProject = project.getRootProject(); + if (project.equals(rootProject)) { logger.debug("Processing root project in directory {}", root); PublishingExtension extension = rootProject.getExtensions().findByType(PublishingExtension.class); @@ -220,7 +221,8 @@ public void perform() { try { final Set lockFileDeps = LockFileIO - .allProjectVersionRefsFromLockfiles(LockFileIO.getLocksRootPath(project)); + .allProjectVersionRefsFromLockfiles(project.getRootDir().toPath()); + final Map dependencies = processAnyExistingManipulationFile( project, getDependencies(project, configuration, lockFileDeps)); @@ -251,10 +253,6 @@ public void perform() { } else { logger.debug("Still have {} projects to scan", cache.getProjectCounterRemaining()); } - - // this needs to happen for each project, not just the last one - LockFileIO.renameAllLockFiles(LockFileIO.getLocksRootPath(project)); - } catch (ManipulationException | IOException e) { throw new ManipulationUncheckedException(e); } @@ -380,6 +378,10 @@ private void align(Configuration configuration, ManipulationCache cache, Manipul writeGmeConfigMarkerFile(rootProject.getBuildFile()); writeGmeReposMarkerFile(); writeRepositorySettingsFile(cache.getRepositories()); + + // this needs to happen for each project, not just the last one + LockFileIO.renameAllLockFiles(rootProject.getRootDir().toPath()); + final Set nonAligned = new LinkedHashSet<>(); processAlignmentReport(rootProject, configuration, cache, nonAligned); } @@ -609,6 +611,7 @@ private Map getDependencies(Project String version = dep.getModuleVersion(); // this is the resolved version from gradle // if the dependency is present in any of the lockfiles, then we use that version + for (ProjectVersionRef lockFileDep : lockFileDeps) { if (lockFileDep.getGroupId().equals(dep.getModuleGroup()) && lockFileDep.getArtifactId().equals(dep.getModuleName())) { diff --git a/analyzer/src/main/java/org/jboss/gm/analyzer/alignment/io/LockFileIO.java b/analyzer/src/main/java/org/jboss/gm/analyzer/alignment/io/LockFileIO.java index 44442ee1..80ea8e4c 100644 --- a/analyzer/src/main/java/org/jboss/gm/analyzer/alignment/io/LockFileIO.java +++ b/analyzer/src/main/java/org/jboss/gm/analyzer/alignment/io/LockFileIO.java @@ -6,7 +6,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; -import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -21,7 +20,6 @@ import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef; import org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef; import org.commonjava.maven.ext.common.ManipulationUncheckedException; -import org.gradle.api.Project; /** * Utility class for lock file I/O. @@ -35,11 +33,13 @@ public class LockFileIO { * Returns a set containing all project version refs from lock files in the given path. * * @param locksRootPath the path to the root for the lock files + * @throws IOException if an error occurs * @return the set of all project version refs */ - public Set allProjectVersionRefsFromLockfiles(Path locksRootPath) { + public Set allProjectVersionRefsFromLockfiles(Path locksRootPath) + throws IOException { final Set result = new HashSet<>(); - getAllLockfiles(locksRootPath).forEach(f -> result.addAll(readProjectVersionRefLocksOfFile(f))); + getAllLockFiles(locksRootPath).forEach(f -> result.addAll(readProjectVersionRefLocksOfFile(f))); return result; } @@ -50,7 +50,7 @@ private Set readProjectVersionRefLocksOfFile(File lockfile) { .filter(l -> !l.startsWith("#")) .map(l -> { try { - return SimpleProjectVersionRef.parse(l); + return SimpleProjectVersionRef.parse(l.split("=")[0]); } catch (InvalidRefException e) { return null; } @@ -66,9 +66,11 @@ private Set readProjectVersionRefLocksOfFile(File lockfile) { * Renames all lock files under the given root. * * @param locksRootPath the root path for the lock files + * @throws IOException if an error occurs */ - public void renameAllLockFiles(Path locksRootPath) { - getAllLockfiles(locksRootPath).forEach(f -> { + public void renameAllLockFiles(Path locksRootPath) + throws IOException { + getAllLockFiles(locksRootPath).forEach(f -> { final Path path = f.toPath(); try { Files.move(path, path.resolveSibling(path.getFileName() + ".unused"), StandardCopyOption.REPLACE_EXISTING); @@ -78,27 +80,15 @@ public void renameAllLockFiles(Path locksRootPath) { }); } - private List getAllLockfiles(Path locksRootPath) { + private List getAllLockFiles(Path locksRootPath) + throws IOException { if (!locksRootPath.toFile().exists()) { return Collections.emptyList(); } - - File[] lockfiles = locksRootPath.toFile() - .listFiles((dir, filename) -> filename.endsWith(LOCKFILE_EXTENSION)); - if (lockfiles == null) { - return Collections.emptyList(); - } - - return Arrays.asList(lockfiles); - } - - /** - * Gets the lock file root path for the given project. - * - * @param project the project - * @return the lock file root path - */ - public Path getLocksRootPath(Project project) { - return project.getProjectDir().toPath().resolve("gradle/dependency-locks"); + return Files.find(locksRootPath, Integer.MAX_VALUE, + (filePath, fileAttr) -> fileAttr.isRegularFile() && filePath.getFileName().toString().endsWith( + (LOCKFILE_EXTENSION))) + .map(Path::toFile).collect( + Collectors.toList()); } } diff --git a/analyzer/src/test/java/org/jboss/gm/analyzer/alignment/io/LockFileIOTest.java b/analyzer/src/test/java/org/jboss/gm/analyzer/alignment/io/LockFileIOTest.java index 8a941372..f6d7cc63 100644 --- a/analyzer/src/test/java/org/jboss/gm/analyzer/alignment/io/LockFileIOTest.java +++ b/analyzer/src/test/java/org/jboss/gm/analyzer/alignment/io/LockFileIOTest.java @@ -21,7 +21,8 @@ public class LockFileIOTest { public TemporaryFolder tempDir = new TemporaryFolder(); @Test - public void readNonExistingFileShouldReturnEmptySet() { + public void readNonExistingFileShouldReturnEmptySet() + throws IOException { assertThat(LockFileIO.allProjectVersionRefsFromLockfiles(Paths.get("/lol"))).isEmpty(); } @@ -42,7 +43,8 @@ public void readValidFileShouldReturnExpectedResults() throws URISyntaxException } @Test - public void renameNonExistingFileShouldNotResultInAnError() { + public void renameNonExistingFileShouldNotResultInAnError() + throws IOException { LockFileIO.renameAllLockFiles(Paths.get("/lol")); } diff --git a/analyzer/src/test/resources/compileClasspath.lockfile b/analyzer/src/test/resources/compileClasspath.lockfile index 4cf61f15..358dbd2c 100644 --- a/analyzer/src/test/resources/compileClasspath.lockfile +++ b/analyzer/src/test/resources/compileClasspath.lockfile @@ -1,6 +1,6 @@ # Dummy similar to what gradle writes when --write-locks is enabled io.undertow:undertow-core:2.0.21.Final org.apache.commons:commons-lang3:3.8 -org.hdrhistogram:HdrHistogram:2.1.10 +org.hdrhistogram:HdrHistogram:2.1.10=compileClasspath # there are more transitives added by gradle, we only add one in order to not distract com.google.guava:guava:25.1-android