Skip to content

Commit

Permalink
Remove SonarCloud false positive warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsotovalero committed Nov 20, 2020
1 parent fb789ce commit dff7d9a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true)
@Slf4j
public class DepCleanMojo extends AbstractMojo {

private static final String SEPARATOR = "-------------------------------------------------------";

/**
Expand Down Expand Up @@ -167,7 +168,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}

System.out.println(SEPARATOR);
printString(SEPARATOR);
getLog().info("Starting DepClean dependency analysis");

File pomFile = new File(project.getBasedir().getAbsolutePath() + File.separator + "pom.xml");
Expand All @@ -178,7 +179,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}

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

/* Build Maven model to manipulate the pom */
Model model;
Expand Down Expand Up @@ -287,25 +288,25 @@ public void execute() throws MojoExecutionException, MojoFailureException {

/* Printing the results to the console */

System.out.println(" D E P C L E A N A N A L Y S I S R E S U L T S");
System.out.println(SEPARATOR);
printString(" D E P C L E A N A N A L Y S I S R E S U L T S");
printString(SEPARATOR);

System.out.println("Used direct dependencies" + " [" + usedDeclaredArtifactsCoordinates.size() + "]" + ": ");
printString("Used direct dependencies" + " [" + usedDeclaredArtifactsCoordinates.size() + "]" + ": ");
printDependencies(sizeOfDependencies, usedDeclaredArtifactsCoordinates);

System.out.println("Used transitive dependencies" + " [" + usedUndeclaredArtifactsCoordinates.size() + "]" + ": ");
printString("Used transitive dependencies" + " [" + usedUndeclaredArtifactsCoordinates.size() + "]" + ": ");
printDependencies(sizeOfDependencies, usedUndeclaredArtifactsCoordinates);

System.out.println("Potentially unused direct dependencies" + " [" + unusedDeclaredArtifactsCoordinates.size() + "]" + ": ");
printString("Potentially unused direct dependencies" + " [" + unusedDeclaredArtifactsCoordinates.size() + "]" + ": ");
printDependencies(sizeOfDependencies, unusedDeclaredArtifactsCoordinates);

System.out.println("Potentially unused transitive dependencies" + " [" + unusedUndeclaredArtifactsCoordinates.size() + "]" + ": ");
printString("Potentially unused transitive dependencies" + " [" + unusedUndeclaredArtifactsCoordinates.size() + "]" + ": ");
printDependencies(sizeOfDependencies, unusedUndeclaredArtifactsCoordinates);

if (!ignoreDependencies.isEmpty()) {
System.out.println(SEPARATOR);
System.out.println("Dependencies ignored in the analysis by the user" + " [" + ignoreDependencies.size() + "]" + ": ");
ignoreDependencies.stream().forEach(s -> System.out.println("\t" + s));
printString(SEPARATOR);
printString("Dependencies ignored in the analysis by the user" + " [" + ignoreDependencies.size() + "]" + ": ");
ignoreDependencies.stream().forEach(s -> printString("\t" + s));
}

/* Fail the build if there are unused dependencies */
Expand Down Expand Up @@ -369,14 +370,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {

/* Write the debloated pom file */
try {
Path path = Paths.get(pathToPutDebloatedPom);
Path path = Paths.get(pathToDebloatedPom);
writePom(path, model);
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}

getLog().info("POM debloated successfully");
getLog().info("pom-debloated.xml file created in: " + pathToPutDebloatedPom);
getLog().info("pom-debloated.xml file created in: " + pathToDebloatedPom);
}


Expand Down Expand Up @@ -419,7 +420,7 @@ private void printDependencies(Map<String, Long> sizeOfDependencies, Set<String>
.sorted(Comparator.comparing(o -> sizeOfDependencies.get(o.split(":")[1] + "-" + o.split(":")[2] + ".jar")))
.collect(Collectors.toCollection(LinkedList::new))
.descendingIterator()
.forEachRemaining(s -> System.out.println("\t" + s + " (" + getSize(s, sizeOfDependencies) + ")"));
.forEachRemaining(s -> printString("\t" + s + " (" + getSize(s, sizeOfDependencies) + ")"));
}

/**
Expand Down Expand Up @@ -528,4 +529,8 @@ private Dependency createDependency(final Artifact artifact) {
dependency.setType(artifact.getType());
return dependency;
}

private void printString(String string) {
System.out.println(string); //NOSONAR avoid a warning of non-used logger
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static void decompressJarFile(String destDirectory, String jarFilePath)
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
new File(filePath).getParentFile().mkdirs();
new File(filePath).getParentFile().mkdirs(); //NOSONAR Triggers a false warning of path traversal attack
// if the entry is a file, extracts it
extractFile(jarIn, filePath);
}
Expand All @@ -95,7 +95,7 @@ private static void decompressJarFile(String destDirectory, String jarFilePath)
* @throws IOException In case of IO issues.
*/
private static void extractFile(final JarInputStream jarIn, final String filePath) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) { //NOSONAR Triggers a false warning of path traversal attack
byte[] bytesIn = new byte[BUFFER_SIZE];
int read;
while ((read = jarIn.read(bytesIn)) != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@

package se.kth.depclean.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

import lombok.extern.slf4j.Slf4j;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
Expand All @@ -33,11 +25,17 @@
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.shared.invoker.MavenInvocationException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

@Slf4j
public final class MavenInvoker
{
private MavenInvoker()
{
public final class MavenInvoker {
private MavenInvoker() {
}

/**
Expand All @@ -47,15 +45,15 @@ private MavenInvoker()
* @return The console output.
* @throws IOException In case of IO issues.
*/
public static String[] runCommand(String cmd) throws IOException
{
public static String[] runCommand(String cmd) throws IOException {
ArrayList<String> list = new ArrayList<>();
Process process = Runtime.getRuntime().exec(cmd);
InputStream inputStream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String s; // Temporary String variable
while ((s = br.readLine()) != null)
while ((s = br.readLine()) != null) {
list.add(s);
}
try {
process.waitFor();
} catch (InterruptedException e) {
Expand All @@ -76,8 +74,7 @@ public static String[] runCommand(String cmd) throws IOException
* @return The exit code from the Maven invocation.
* @throws MavenInvocationException In case of any issue invoking maven.
*/
public static int invokeMaven(String mvnHome, String pomPath, String mvnGoal) throws MavenInvocationException
{
public static int invokeMaven(String mvnHome, String pomPath, String mvnGoal) throws MavenInvocationException {
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File(pomPath));
request.setGoals(Collections.singletonList(mvnGoal));
Expand Down

0 comments on commit dff7d9a

Please sign in to comment.