From a15b94db5e8d05aa32e74bb438fe457c94818ef6 Mon Sep 17 00:00:00 2001 From: Omer Zidkoni Date: Sun, 31 Dec 2023 14:00:53 +0200 Subject: [PATCH 1/4] Add -DdepsTreeOutputFile system property for Projects --- .../jfrog/mavendeptree/MavenProjectTree.java | 27 +++++++++++++- .../integration/MavenProjectTreeITest.java | 22 +++++++++-- .../jfrog/mavendeptree/integration/Utils.java | 37 +++++++++++++++---- 3 files changed, 72 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/jfrog/mavendeptree/MavenProjectTree.java b/src/main/java/com/jfrog/mavendeptree/MavenProjectTree.java index 5654867..8c2663a 100644 --- a/src/main/java/com/jfrog/mavendeptree/MavenProjectTree.java +++ b/src/main/java/com/jfrog/mavendeptree/MavenProjectTree.java @@ -2,12 +2,16 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import static com.jfrog.mavendeptree.Utils.getGavString; +import static java.lang.System.lineSeparator; /** * @author yahavi @@ -24,11 +28,30 @@ public class MavenProjectTree extends AbstractMojo { @Parameter(property = "project.file") private File file; + // Specify the system property to an empty file, where the projects command output will be written + @Parameter(property = "depsTreeOutputFile", readonly = true) + private File depsTreeOutputFile; + @Override - public void execute() { + public void execute() throws MojoExecutionException { String gav = getGavString(artifact); String parentGav = getGavString(parent); String pomPath = file.getAbsolutePath(); - System.out.printf("{\"gav\":\"%s\",\"parentGav\":\"%s\",\"pomPath\":\"%s\"}%n", gav, parentGav, pomPath); + String projectInfo = String.format( + "{\"gav\":\"%s\",\"parentGav\":\"%s\",\"pomPath\":\"%s\"}", + gav, parentGav, pomPath + ); + + if (depsTreeOutputFile == null) { + System.out.println(projectInfo); + return; + } + + try (FileWriter fileWriter = new FileWriter(depsTreeOutputFile, true)) { + fileWriter.append(projectInfo).append(lineSeparator()); + } catch (IOException e) { + String errorMessage = "Error writing to depsTreeOutputFile: " + e.getMessage(); + throw new MojoExecutionException(errorMessage, e); + } } } diff --git a/src/test/java/com/jfrog/mavendeptree/integration/MavenProjectTreeITest.java b/src/test/java/com/jfrog/mavendeptree/integration/MavenProjectTreeITest.java index b760101..0f6fc9f 100644 --- a/src/test/java/com/jfrog/mavendeptree/integration/MavenProjectTreeITest.java +++ b/src/test/java/com/jfrog/mavendeptree/integration/MavenProjectTreeITest.java @@ -1,5 +1,6 @@ package com.jfrog.mavendeptree.integration; +import com.fasterxml.jackson.core.JsonProcessingException; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -23,11 +24,24 @@ public class MavenProjectTreeITest { @Test public void testMultiModule() throws VerificationException, IOException { // Run Mojo - List projectInfos = runMavenProjectTree("multi-module", pluginVersion); + List projectInfos = runMavenProjectTree("multi-module", pluginVersion, false); // Test output - assertEquals(projectInfos.size(), 4); - for (String projectInfoJson : projectInfos) { + verifyTestMultiModuleResults(projectInfos); + } + + @Test + public void testMultiModuleWithOutputFile() throws VerificationException, IOException { + // Run Mojo + List projectInfos = runMavenProjectTree("multi-module", pluginVersion, true); + + // Test output + verifyTestMultiModuleResults(projectInfos); + } + + private void verifyTestMultiModuleResults(List testResults) throws JsonProcessingException { + assertEquals(testResults.size(), 4); + for (String projectInfoJson : testResults) { ProjectInfo projectInfo = mapper.readValue(escapePathInWindows(projectInfoJson), ProjectInfo.class); switch (projectInfo.getGav()) { case "org.jfrog.test:multi:3.7-SNAPSHOT": @@ -64,7 +78,7 @@ public void testMavenArchetypeDependencyManagement() throws VerificationExceptio private void testMavenArchetype(String projectName) throws VerificationException, IOException { // Run Mojo - List projectInfoJson = runMavenProjectTree(projectName, pluginVersion); + List projectInfoJson = runMavenProjectTree(projectName, pluginVersion, false); // Test output assertEquals(projectInfoJson.size(), 1); diff --git a/src/test/java/com/jfrog/mavendeptree/integration/Utils.java b/src/test/java/com/jfrog/mavendeptree/integration/Utils.java index 387fd06..6aa0daa 100644 --- a/src/test/java/com/jfrog/mavendeptree/integration/Utils.java +++ b/src/test/java/com/jfrog/mavendeptree/integration/Utils.java @@ -15,6 +15,7 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; @@ -78,14 +79,34 @@ static List runMavenDepTree(String projectName, String testOutputDir, St * @throws IOException in case of any unexpected I/O error. * @throws VerificationException in case of any Maven Verifier error. */ - static List runMavenProjectTree(String projectName, String pluginVersion) throws IOException, VerificationException { - File testDir = ResourceExtractor.simpleExtractResources(Utils.class, "/integration/" + projectName); - Verifier verifier = new Verifier(testDir.getAbsolutePath()); - if (StringUtils.equalsIgnoreCase(System.getProperty("debugITs"), "true")) { - verifier.setEnvironmentVariable("MAVEN_OPTS", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"); + static List runMavenProjectTree(String projectName, String pluginVersion, Boolean withOutputFile) throws IOException, VerificationException { + try { + File testDir = ResourceExtractor.simpleExtractResources(Utils.class, "/integration/" + projectName); + Verifier verifier = new Verifier(testDir.getAbsolutePath()); + + if (StringUtils.equalsIgnoreCase(System.getProperty("debugITs"), "true")) { + verifier.setEnvironmentVariable("MAVEN_OPTS", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"); + } + + String outputFile = verifier.getLogFileName(); + List goals = Lists.newArrayList("clean", "com.jfrog:maven-dep-tree:" + pluginVersion + ":projects", "-q"); + + if (withOutputFile) { + goals.add("-DdepsTreeOutputFile=" + testDir.getAbsolutePath() + "/testOutput.out"); + outputFile = "testOutput.out"; + } + + verifier.executeGoals(goals); + verifier.verifyErrorFreeLog(); + List results = verifier.loadFile(verifier.getBasedir(), outputFile, false); + + if (withOutputFile) { + Files.deleteIfExists(Path.of(testDir.getAbsolutePath(), outputFile)); + } + + return results; + } catch (IOException | VerificationException e) { + throw e; } - verifier.executeGoals(Lists.newArrayList("clean", "com.jfrog:maven-dep-tree:" + pluginVersion + ":projects", "-q")); - verifier.verifyErrorFreeLog(); - return verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false); } } From ae942f89d6ba803d1ff14d0662dd899bafec0d72 Mon Sep 17 00:00:00 2001 From: Omer Zidkoni Date: Sun, 31 Dec 2023 14:09:01 +0200 Subject: [PATCH 2/4] Add -DdepsTreeOutputFile system property for Projects --- .../jfrog/mavendeptree/integration/Utils.java | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/test/java/com/jfrog/mavendeptree/integration/Utils.java b/src/test/java/com/jfrog/mavendeptree/integration/Utils.java index 6aa0daa..b6b9860 100644 --- a/src/test/java/com/jfrog/mavendeptree/integration/Utils.java +++ b/src/test/java/com/jfrog/mavendeptree/integration/Utils.java @@ -80,33 +80,29 @@ static List runMavenDepTree(String projectName, String testOutputDir, St * @throws VerificationException in case of any Maven Verifier error. */ static List runMavenProjectTree(String projectName, String pluginVersion, Boolean withOutputFile) throws IOException, VerificationException { - try { - File testDir = ResourceExtractor.simpleExtractResources(Utils.class, "/integration/" + projectName); - Verifier verifier = new Verifier(testDir.getAbsolutePath()); - - if (StringUtils.equalsIgnoreCase(System.getProperty("debugITs"), "true")) { - verifier.setEnvironmentVariable("MAVEN_OPTS", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"); - } + File testDir = ResourceExtractor.simpleExtractResources(Utils.class, "/integration/" + projectName); + Verifier verifier = new Verifier(testDir.getAbsolutePath()); - String outputFile = verifier.getLogFileName(); - List goals = Lists.newArrayList("clean", "com.jfrog:maven-dep-tree:" + pluginVersion + ":projects", "-q"); + if (StringUtils.equalsIgnoreCase(System.getProperty("debugITs"), "true")) { + verifier.setEnvironmentVariable("MAVEN_OPTS", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"); + } - if (withOutputFile) { - goals.add("-DdepsTreeOutputFile=" + testDir.getAbsolutePath() + "/testOutput.out"); - outputFile = "testOutput.out"; - } + String outputFile = verifier.getLogFileName(); + List goals = Lists.newArrayList("clean", "com.jfrog:maven-dep-tree:" + pluginVersion + ":projects", "-q"); - verifier.executeGoals(goals); - verifier.verifyErrorFreeLog(); - List results = verifier.loadFile(verifier.getBasedir(), outputFile, false); + if (withOutputFile) { + goals.add("-DdepsTreeOutputFile=" + testDir.getAbsolutePath() + "/testOutput.out"); + outputFile = "testOutput.out"; + } - if (withOutputFile) { - Files.deleteIfExists(Path.of(testDir.getAbsolutePath(), outputFile)); - } + verifier.executeGoals(goals); + verifier.verifyErrorFreeLog(); + List results = verifier.loadFile(verifier.getBasedir(), outputFile, false); - return results; - } catch (IOException | VerificationException e) { - throw e; + if (withOutputFile) { + Files.deleteIfExists(Paths.get(testDir.getAbsolutePath(), outputFile)); } + + return results; } } From 001b6699665905ab31c1e0d98469d22d7adf716d Mon Sep 17 00:00:00 2001 From: Omer Zidkoni Date: Mon, 1 Jan 2024 11:09:40 +0200 Subject: [PATCH 3/4] Add -DdepsTreeOutputFile system property for Projects --- .../java/com/jfrog/mavendeptree/integration/Utils.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/jfrog/mavendeptree/integration/Utils.java b/src/test/java/com/jfrog/mavendeptree/integration/Utils.java index b6b9860..241fa0d 100644 --- a/src/test/java/com/jfrog/mavendeptree/integration/Utils.java +++ b/src/test/java/com/jfrog/mavendeptree/integration/Utils.java @@ -75,6 +75,7 @@ static List runMavenDepTree(String projectName, String testOutputDir, St * * @param projectName - The test project to run * @param pluginVersion - The plugin version + * @param withOutputFile - Write the test result to a temporary output file * @return the output. * @throws IOException in case of any unexpected I/O error. * @throws VerificationException in case of any Maven Verifier error. @@ -91,8 +92,10 @@ static List runMavenProjectTree(String projectName, String pluginVersion List goals = Lists.newArrayList("clean", "com.jfrog:maven-dep-tree:" + pluginVersion + ":projects", "-q"); if (withOutputFile) { - goals.add("-DdepsTreeOutputFile=" + testDir.getAbsolutePath() + "/testOutput.out"); - outputFile = "testOutput.out"; + Path tempFile = Files.createTempFile(Paths.get(testDir.getAbsolutePath()), "testOutput", ".out"); + String tempFilePath = tempFile.toAbsolutePath().toString(); + goals.add("-DdepsTreeOutputFile=" + tempFilePath); + outputFile = tempFile.toString(); } verifier.executeGoals(goals); From 9b1ee363c077b23f5490ecc984be67d5f027bcba Mon Sep 17 00:00:00 2001 From: Omer Zidkoni Date: Mon, 1 Jan 2024 14:20:41 +0200 Subject: [PATCH 4/4] fix tests --- src/test/java/com/jfrog/mavendeptree/integration/Utils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/jfrog/mavendeptree/integration/Utils.java b/src/test/java/com/jfrog/mavendeptree/integration/Utils.java index 241fa0d..359e066 100644 --- a/src/test/java/com/jfrog/mavendeptree/integration/Utils.java +++ b/src/test/java/com/jfrog/mavendeptree/integration/Utils.java @@ -95,7 +95,7 @@ static List runMavenProjectTree(String projectName, String pluginVersion Path tempFile = Files.createTempFile(Paths.get(testDir.getAbsolutePath()), "testOutput", ".out"); String tempFilePath = tempFile.toAbsolutePath().toString(); goals.add("-DdepsTreeOutputFile=" + tempFilePath); - outputFile = tempFile.toString(); + outputFile = Paths.get(tempFilePath).getFileName().toString(); } verifier.executeGoals(goals);