Skip to content

Commit

Permalink
Add dependency size to JSON field
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsotovalero committed Nov 16, 2020
1 parent 2a40a3d commit e9dacba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println(SEPARATOR);
getLog().info("Starting DepClean dependency analysis");

File pomFile = new File(project.getBasedir().getAbsolutePath() + "/" + "pom.xml");
File pomFile = new File(project.getBasedir().getAbsolutePath() + File.separator + "pom.xml");

String packaging = project.getPackaging();
if (packaging.equals("pom")) {
getLog().info("Skipping because packaging type " + packaging + ".");
return;
}

String pathToPutDebloatedPom = project.getBasedir().getAbsolutePath() + "/" + "pom-debloated.xml";
String pathToPutDebloatedPom = project.getBasedir().getAbsolutePath() + File.separator + "pom-debloated.xml";

/* Build Maven model to manipulate the pom */
Model model;
Expand All @@ -196,19 +196,17 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}

// TODO calculate the size of all the dependencies in target/dependency
/* Get the size of the dependencies */
Map<String, Long> sizeOfDependencies = new HashMap<>();
Iterator<File> iterator = FileUtils.iterateFiles(
new File(
project.getBuild().getDirectory() + "/"
project.getBuild().getDirectory() + File.separator
+ "dependency"), new String[]{"jar"}, true);
while (iterator.hasNext()) {
File file = iterator.next();
sizeOfDependencies.put(file.getName(), FileUtils.sizeOf(file));
}


/* Decompress dependencies */
String dependencyDirectoryName = project.getBuild().getDirectory() + "/" + "dependency";
File dependencyDirectory = new File(dependencyDirectoryName);
Expand Down Expand Up @@ -378,8 +376,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {

/* Writing the JSON file with the debloat results */
if (createResultJson) {
getLog().info("Starting creating JSON file");
String treeFile = project.getBuild().getDirectory() + "/" + "tree.txt";
String jsonFile = project.getBuild().getDirectory() + File.separator + "results.json";
String treeFile = project.getBuild().getDirectory() + File.separator + "tree.txt";
/* Copy direct dependencies locally */
try {
MavenInvoker.runCommand("mvn dependency:tree -DoutputFile=" + treeFile + " -Dverbose=true");
Expand All @@ -389,16 +387,15 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
ParsedDependencies parsedDependencies = new ParsedDependencies(
treeFile,
sizeOfDependencies,
usedDeclaredArtifactsCoordinates,
usedUndeclaredArtifactsCoordinates
);
try {
FileUtils.write(new File(project.getBuild().getDirectory() + "/" + "results.json"),
parsedDependencies.parseTreeToJSON(),
Charset.defaultCharset()
);
FileUtils.write(new File(jsonFile), parsedDependencies.parseTreeToJSON(), Charset.defaultCharset());
getLog().info("JSON file in created in " + jsonFile);
} catch (ParseException | IOException e) {
getLog().error("Unable generate JSON file.");
getLog().error("Unable to generate JSON file.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@
import fr.dutra.tools.maven.deptree.core.Node;

import java.io.IOException;
import java.util.Random;
import java.util.Map;
import java.util.Set;

public class NodeAdapter extends TypeAdapter<Node> {

private final Set<String> usedDeclaredArtifactsCoordinates;
private final Set<String> usedUndeclaredArtifactsCoordinates;
Random random = new Random();
private final Map<String, Long> sizeOfDependencies;

public NodeAdapter(Set<String> usedDeclaredArtifactsCoordinates,
Set<String> usedUndeclaredArtifactsCoordinates) {
Set<String> usedUndeclaredArtifactsCoordinates,
Map<String, Long> sizeOfDependencies) {
this.usedDeclaredArtifactsCoordinates = usedDeclaredArtifactsCoordinates;
this.usedUndeclaredArtifactsCoordinates = usedUndeclaredArtifactsCoordinates;
this.sizeOfDependencies = sizeOfDependencies;
}

@Override
public void write(JsonWriter jsonWriter, Node node) throws IOException {
String coordinates = node.getGroupId() + ":" + node.getArtifactId() + ":" + node.getVersion() + ":" + node.getScope();
String dependencyJar = node.getArtifactId() + "-" + node.getVersion() + ".jar";
jsonWriter.beginObject()
.name("coordinates")
.jsonValue("\"" + node.getArtifactCanonicalForm() + "\"")
Expand All @@ -50,7 +53,7 @@ public void write(JsonWriter jsonWriter, Node node) throws IOException {
.jsonValue("\"" + node.getClassifier() + "\"")

.name("size")
.jsonValue(String.valueOf(random.nextDouble()))
.jsonValue(String.valueOf(sizeOfDependencies.get(dependencyJar)))

.name("status")
.jsonValue((usedDeclaredArtifactsCoordinates.contains(coordinates) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;

@Slf4j
Expand All @@ -22,13 +23,17 @@ public class ParsedDependencies {
private final String treeTextFilePath;
private final Set<String> usedDeclaredArtifactsCoordinates;
private final Set<String> usedUndeclaredArtifactsCoordinates;
private final Map<String, Long> sizeOfDependencies;

public ParsedDependencies(String treeTextFilePath,
Map<String, Long> sizeOfDependencies,
Set<String> usedDeclaredArtifactsCoordinates,
Set<String> usedUndeclaredArtifactsCoordinates) {
Set<String> usedUndeclaredArtifactsCoordinates
) {
this.treeTextFilePath = treeTextFilePath;
this.usedDeclaredArtifactsCoordinates = usedDeclaredArtifactsCoordinates;
this.usedUndeclaredArtifactsCoordinates = usedUndeclaredArtifactsCoordinates;
this.sizeOfDependencies = sizeOfDependencies;
}

public String parseTreeToJSON() throws ParseException, IOException {
Expand All @@ -40,7 +45,8 @@ public String parseTreeToJSON() throws ParseException, IOException {
Node tree = parser.parse(r);
NodeAdapter nodeAdapter = new NodeAdapter(
usedDeclaredArtifactsCoordinates,
usedUndeclaredArtifactsCoordinates
usedUndeclaredArtifactsCoordinates,
sizeOfDependencies
);
GsonBuilder gsonBuilder = new GsonBuilder()
.registerTypeAdapter(Node.class, nodeAdapter);
Expand Down

0 comments on commit e9dacba

Please sign in to comment.