From e289280c70f51a11b5c83c1a07d6a15c21328a1c Mon Sep 17 00:00:00 2001 From: Omer Zidkoni <50792403+omerzi@users.noreply.github.com> Date: Mon, 1 Jan 2024 14:25:26 +0200 Subject: [PATCH] Add -DdepsTreeOutputFile system property for Projects command (#13) --- .../jfrog/mavendeptree/MavenProjectTree.java | 27 +++++++++++++++++-- .../integration/MavenProjectTreeITest.java | 22 ++++++++++++--- .../jfrog/mavendeptree/integration/Utils.java | 26 +++++++++++++++--- 3 files changed, 66 insertions(+), 9 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..359e066 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; @@ -74,18 +75,37 @@ 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. */ - static List runMavenProjectTree(String projectName, String pluginVersion) throws IOException, VerificationException { + static List runMavenProjectTree(String projectName, String pluginVersion, Boolean withOutputFile) 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"); } - verifier.executeGoals(Lists.newArrayList("clean", "com.jfrog:maven-dep-tree:" + pluginVersion + ":projects", "-q")); + + String outputFile = verifier.getLogFileName(); + List goals = Lists.newArrayList("clean", "com.jfrog:maven-dep-tree:" + pluginVersion + ":projects", "-q"); + + if (withOutputFile) { + Path tempFile = Files.createTempFile(Paths.get(testDir.getAbsolutePath()), "testOutput", ".out"); + String tempFilePath = tempFile.toAbsolutePath().toString(); + goals.add("-DdepsTreeOutputFile=" + tempFilePath); + outputFile = Paths.get(tempFilePath).getFileName().toString(); + } + + verifier.executeGoals(goals); verifier.verifyErrorFreeLog(); - return verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false); + List results = verifier.loadFile(verifier.getBasedir(), outputFile, false); + + if (withOutputFile) { + Files.deleteIfExists(Paths.get(testDir.getAbsolutePath(), outputFile)); + } + + return results; } }