Skip to content

Commit

Permalink
Add -DdepsTreeOutputFile system property for Projects command (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
omerzi authored Jan 1, 2024
1 parent 5b22cca commit e289280
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
27 changes: 25 additions & 2 deletions src/main/java/com/jfrog/mavendeptree/MavenProjectTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jfrog.mavendeptree.integration;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -23,11 +24,24 @@ public class MavenProjectTreeITest {
@Test
public void testMultiModule() throws VerificationException, IOException {
// Run Mojo
List<String> projectInfos = runMavenProjectTree("multi-module", pluginVersion);
List<String> 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<String> projectInfos = runMavenProjectTree("multi-module", pluginVersion, true);

// Test output
verifyTestMultiModuleResults(projectInfos);
}

private void verifyTestMultiModuleResults(List<String> 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":
Expand Down Expand Up @@ -64,7 +78,7 @@ public void testMavenArchetypeDependencyManagement() throws VerificationExceptio

private void testMavenArchetype(String projectName) throws VerificationException, IOException {
// Run Mojo
List<String> projectInfoJson = runMavenProjectTree(projectName, pluginVersion);
List<String> projectInfoJson = runMavenProjectTree(projectName, pluginVersion, false);

// Test output
assertEquals(projectInfoJson.size(), 1);
Expand Down
26 changes: 23 additions & 3 deletions src/test/java/com/jfrog/mavendeptree/integration/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,18 +75,37 @@ static List<String> 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<String> runMavenProjectTree(String projectName, String pluginVersion) throws IOException, VerificationException {
static List<String> 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<String> 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<String> results = verifier.loadFile(verifier.getBasedir(), outputFile, false);

if (withOutputFile) {
Files.deleteIfExists(Paths.get(testDir.getAbsolutePath(), outputFile));
}

return results;
}
}

0 comments on commit e289280

Please sign in to comment.