From ca7e318325ad8c7709a3f3347ae4b93a89cce3ad Mon Sep 17 00:00:00 2001 From: pgahlaut994 Date: Fri, 1 Mar 2024 14:58:34 +0530 Subject: [PATCH] Bug fixes in Maven dependency resolver --- pom.xml | 2 +- .../drona/Plugins/JavaMavenPlugin.java | 116 ++++++++++++++++-- .../com/phsyberdome/drona/Utils/FileUtil.java | 6 +- .../drona/Utils/MavenRepoHelper.java | 3 +- .../drona/Utils/MavenVersionHelper.java | 15 ++- .../licensedetector/LicenseDatabase.java | 2 + 6 files changed, 128 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index f45f269..8d63c59 100644 --- a/pom.xml +++ b/pom.xml @@ -68,7 +68,7 @@ org.apache.commons commons-compress - 1.18 + 1.26.0 diff --git a/src/main/java/com/phsyberdome/drona/Plugins/JavaMavenPlugin.java b/src/main/java/com/phsyberdome/drona/Plugins/JavaMavenPlugin.java index b8f808e..a4c4f5c 100644 --- a/src/main/java/com/phsyberdome/drona/Plugins/JavaMavenPlugin.java +++ b/src/main/java/com/phsyberdome/drona/Plugins/JavaMavenPlugin.java @@ -19,6 +19,9 @@ import java.nio.file.FileSystems; import java.nio.file.Path; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; @@ -42,9 +45,13 @@ public class JavaMavenPlugin implements PluginInterface private ArrayList modules; private final LicenseDetector licenseDetector; + + private Set scannedDependencies; + public JavaMavenPlugin(LicenseDetector licenseDetector) { this.licenseDetector = licenseDetector; + this.scannedDependencies = new HashSet<>(); } @@ -78,7 +85,6 @@ public void readModules() { String rootGroupId = MavenRepoHelper.extractAttributeFromNode(doc.getDocumentElement(), "groupId"); String rootVersion = MavenRepoHelper.extractAttributeFromNode(doc.getDocumentElement(), "version"); Module root = new Module(rootArtifactId,rootVersion); - NodeList list = doc.getElementsByTagName("dependency"); for(int i=0;i getModules() { return modules; } + + private boolean alreadyScanned(Module m){ + Iterator it = scannedDependencies.iterator(); + while(it.hasNext()){ + Module a = (Module) it.next(); + if(a.getName()==m.getName() && a.getVersion() == m.getVersion()){ + return true; + } + } + return false; + } + + private Module getScannedModule(Module m){ + Iterator it = scannedDependencies.iterator(); + while(it.hasNext()){ + Module a = (Module) it.next(); + if(a.getName().strip().equalsIgnoreCase(m.getName().strip()) && a.getVersion().strip().equalsIgnoreCase(m.getVersion().strip())){ + return a; + } + } + return m; + } + private Module getScannedModule(String artifactId){ + Iterator it = scannedDependencies.iterator(); + while(it.hasNext()){ + Module a = (Module) it.next(); + if(a.getName().strip().equalsIgnoreCase(artifactId.strip())){ + return a; + } + } + return new Module(artifactId,""); + } + + private boolean isAlreadyScannedArtifact(String artifiactId) { + Iterator it = scannedDependencies.iterator(); + while(it.hasNext()){ + Module a = (Module) it.next(); + if(a.getName().equalsIgnoreCase(artifiactId)){ + return true; + } + } + return false; + } } diff --git a/src/main/java/com/phsyberdome/drona/Utils/FileUtil.java b/src/main/java/com/phsyberdome/drona/Utils/FileUtil.java index 227797a..ee789fb 100644 --- a/src/main/java/com/phsyberdome/drona/Utils/FileUtil.java +++ b/src/main/java/com/phsyberdome/drona/Utils/FileUtil.java @@ -215,10 +215,10 @@ public static String readFile(Path filePath) { text = content.collect(Collectors.joining("\n")); } return text; - } catch (IOException ex) { + } catch (Exception ex) { CLIHelper.updateCurrentLine(ex.getLocalizedMessage(), Ansi.Color.RED); - return null; } + return ""; } public static void extractZipFolder(String zipFile,String extractFolder) @@ -331,7 +331,7 @@ public static void extractTarball(String tarfile,String toPath) throws FileNotFo tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); - }catch(IOException e){ + }catch(Exception e){ CLIHelper.updateCurrentLine("Failed to untar file " + tarfile,Ansi.Color.RED); } diff --git a/src/main/java/com/phsyberdome/drona/Utils/MavenRepoHelper.java b/src/main/java/com/phsyberdome/drona/Utils/MavenRepoHelper.java index a9b3087..e251c21 100644 --- a/src/main/java/com/phsyberdome/drona/Utils/MavenRepoHelper.java +++ b/src/main/java/com/phsyberdome/drona/Utils/MavenRepoHelper.java @@ -144,7 +144,6 @@ public static String getVersionFromParent(String artifactId,Document doc){ public static Document getParentPOM(Document pom) { NodeList parents = pom.getElementsByTagName("parent"); if(parents.getLength() <= 0){ - System.out.println("This is the root pom!"); return null; } Element parent = (Element) parents.item(0); @@ -155,7 +154,7 @@ public static Document getParentPOM(Document pom) { String urlToParentPom = buildUrlForPomFile(groupId, artifactId, version); File file; try { - file = FileUtil.downloadFile("/.drona/temp/poms/pom.xml", urlToParentPom); + file = FileUtil.downloadFile("/.drona/temp/poms/"+artifactId+"_parent_pom.xml", urlToParentPom); } catch (IOException ex) { return null; } diff --git a/src/main/java/com/phsyberdome/drona/Utils/MavenVersionHelper.java b/src/main/java/com/phsyberdome/drona/Utils/MavenVersionHelper.java index 497b16b..1480b99 100644 --- a/src/main/java/com/phsyberdome/drona/Utils/MavenVersionHelper.java +++ b/src/main/java/com/phsyberdome/drona/Utils/MavenVersionHelper.java @@ -44,8 +44,13 @@ public static List getAllVersions(String groupId,String artifactId) { } public static String resolveVersion(String groupId,String artifactId,String version) { + if(version.length() == 0 || isPureVersion(version)) { + return version; + } ArrayList allVersions = (ArrayList) getAllVersions(groupId, artifactId); - + if(allVersions.isEmpty()) { + return version; + } List ranges = extractRanges(version); MavenVersion versionSpec = new MavenVersion(ranges); List satisfiedVersions = new ArrayList<>(); @@ -101,4 +106,12 @@ private static List extractRanges(String version) { return ranges; } + + private static boolean isPureVersion(String version) { + return !version.contains("(") + || !version.contains(")") + || !version.contains("]") + || !version.contains("[") + || !version.contains("."); + } } diff --git a/src/main/java/com/phsyberdome/drona/licensedetector/LicenseDatabase.java b/src/main/java/com/phsyberdome/drona/licensedetector/LicenseDatabase.java index 7d79e66..3e62a47 100644 --- a/src/main/java/com/phsyberdome/drona/licensedetector/LicenseDatabase.java +++ b/src/main/java/com/phsyberdome/drona/licensedetector/LicenseDatabase.java @@ -114,8 +114,10 @@ private void downloadLicenseListData(){ pathToLicenseListJSON = pathToLicenseList.toString(); } catch (MalformedURLException ex) { Logger.getLogger(LicenseDatabase.class.getName()).log(Level.SEVERE, null, ex); + System.exit(0); } catch (IOException ex) { Logger.getLogger(LicenseDatabase.class.getName()).log(Level.SEVERE, null, ex); + System.exit(0); } CLIHelper.printLine("License data downloaded at "+pathToLicenseListJSON.toString(), Color.GREEN);