Skip to content

Commit

Permalink
Bug fixes in Maven dependency resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamgahlout committed Mar 1, 2024
1 parent 2209012 commit ca7e318
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
<version>1.26.0</version>
</dependency>

<dependency>
Expand Down
116 changes: 107 additions & 9 deletions src/main/java/com/phsyberdome/drona/Plugins/JavaMavenPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,9 +45,13 @@ public class JavaMavenPlugin implements PluginInterface

private ArrayList<Module> modules;
private final LicenseDetector licenseDetector;

private Set<Module> scannedDependencies;


public JavaMavenPlugin(LicenseDetector licenseDetector) {
this.licenseDetector = licenseDetector;
this.scannedDependencies = new HashSet<>();
}


Expand Down Expand Up @@ -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<list.getLength();i++){
Expand All @@ -89,29 +95,53 @@ public void readModules() {
}
Element element = (Element) node;


var isSoftRequirement = false;
String artifactId = MavenRepoHelper.extractAttributeFromNode(element, "artifactId");
String groupId = MavenRepoHelper.extractAttributeFromNode(element, "groupId");
String version = MavenRepoHelper.extractAttributeFromNode(element, "version");
String scope = MavenRepoHelper.extractAttributeFromNode(element, "scope");
String optional = MavenRepoHelper.extractAttributeFromNode(element, "optional");
if(scope!=null && (scope.equals("test") || scope.equals("import"))){
continue;
}
if(optional!=null && (optional.equals("true"))){
continue;
}

// if we have a property as version.
if(version!=null)
version = MavenRepoHelper.resolvePropertyValue(version, doc);
// If we dont even have version mentioned.
else
version = MavenRepoHelper.getVersionFromParent(artifactId,MavenRepoHelper.getParentPOM(doc));
version = MavenVersionHelper.resolveVersion(groupId, artifactId, version);
if(version!=null){
var resolvedVersion = MavenVersionHelper.resolveVersion(groupId, artifactId, version);
if(resolvedVersion.equals(version)) {
// Was it a soft requirement?
isSoftRequirement = true;
}
version = resolvedVersion;
}

Module m = new Module(artifactId,version);
m.setSupplier(groupId);
if(alreadyScanned(m)){
CLIHelper.updateCurrentLine("Dependency "+artifactId+" already scanned",Ansi.Color.GREEN);
root.addToDependencies(getScannedModule(m));
continue;
}else if(isSoftRequirement && isAlreadyScannedArtifact(m.getName())){
CLIHelper.updateCurrentLine("Dependency "+artifactId+" already scanned",Ansi.Color.GREEN);
root.addToDependencies(getScannedModule(artifactId));
continue;
}

if(version!=null){
getLicenseAndTransitiveDependenciesForModule(m);
}else{
}else {
CLIHelper.updateCurrentLine("Cannot proceed! REASON: Couldnt get version for "+m.getName(),Ansi.Color.CYAN);
}
root.addToDependencies(m);
scannedDependencies.add(m);
}
}
modules.add(root);
Expand All @@ -124,7 +154,7 @@ public void readModules() {


private void getLicenseAndTransitiveDependenciesForModule(Module root) {
CLIHelper.updateCurrentLine("Building dep tree for "+root.getName(),Ansi.Color.CYAN);
CLIHelper.updateCurrentLine("Building dep tree for "+root.getName()+"@"+root.getVersion(),Ansi.Color.CYAN);
String repoUrlString = buildRepositoryUrl(root);
String loc = Configuration.getConfiguration().getCloneLocation().toString();
Path path = FileUtil.getFilePathFromURL(repoUrlString,loc);
Expand Down Expand Up @@ -154,10 +184,10 @@ private void buildDependencyTree(Module root, Path pathToModule) {
try {
FileUtils.copyURLToFile(new URL(urlToPomString), file);
} catch (MalformedURLException ex) {
Logger.getLogger(MavenRepoHelper.class.getName()).log(Level.SEVERE, null, ex);
// Logger.getLogger(MavenRepoHelper.class.getName()).log(Level.SEVERE, null, ex);
return;
} catch (IOException ex) {
Logger.getLogger(MavenRepoHelper.class.getName()).log(Level.SEVERE, null, ex);
// Logger.getLogger(MavenRepoHelper.class.getName()).log(Level.SEVERE, null, ex);
return;
}
CLIHelper.updateCurrentLine("Downloaded pom from "+urlToPomString,Ansi.Color.GREEN);
Expand All @@ -172,36 +202,61 @@ private void buildDependencyTree(Module root, Path pathToModule) {
return;
}
NodeList list = doc.getElementsByTagName("dependency");

for(int i=0;i<list.getLength();i++){
Node node = list.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE) {
if(!node.getParentNode().getNodeName().equals("dependencies")){
continue;
}
var isSoftRequirement = false;
Element element = (Element) node;
String artifactId = MavenRepoHelper.extractAttributeFromNode(element, "artifactId");
String groupId = MavenRepoHelper.extractAttributeFromNode(element, "groupId");
String version = MavenRepoHelper.extractAttributeFromNode(element, "version");
String scope = MavenRepoHelper.extractAttributeFromNode(element, "scope");
String optional = MavenRepoHelper.extractAttributeFromNode(element, "optional");

if(scope!=null && (scope.equals("test") || scope.equals("import"))){
continue;
}
if(optional!=null && (optional.equals("true"))){
continue;
}
// if we have a property as version.
if(version!=null)
version = MavenRepoHelper.resolvePropertyValue(version, doc);
// If we dont even have version mentioned.
else
version = MavenRepoHelper.getVersionFromParent(artifactId,MavenRepoHelper.getParentPOM(doc));
version = MavenVersionHelper.resolveVersion(groupId, artifactId, version);
if(version!=null){
var resolvedVersion = MavenVersionHelper.resolveVersion(groupId, artifactId, version);
if(resolvedVersion.equals(version)) {
// Was it a soft requirement?
isSoftRequirement = true;
}
version = resolvedVersion;
}

Module m = new Module(artifactId,version);
m.setSupplier(groupId);
if(alreadyScanned(m)){
CLIHelper.updateCurrentLine("Dependency "+artifactId+" already scanned",Ansi.Color.GREEN);
root.addToDependencies(getScannedModule(m));
continue;
}else if(isSoftRequirement && isAlreadyScannedArtifact(m.getName())){
CLIHelper.updateCurrentLine("Dependency "+artifactId+" already scanned",Ansi.Color.GREEN);
root.addToDependencies(getScannedModule(artifactId));
continue;
}

if(version!=null){
getLicenseAndTransitiveDependenciesForModule(m);
}else{
CLIHelper.updateCurrentLine("Cannot proceed! REASON: Couldnt get version for "+m.getName(),Ansi.Color.RED);
}
root.addToDependencies(m);
scannedDependencies.add(m);
}
}

Expand Down Expand Up @@ -229,5 +284,48 @@ private static String buildRepositoryUrl(Module root) {
public ArrayList<Module> 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;
}

}
6 changes: 3 additions & 3 deletions src/main/java/com/phsyberdome/drona/Utils/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ public static List<String> getAllVersions(String groupId,String artifactId) {
}

public static String resolveVersion(String groupId,String artifactId,String version) {
if(version.length() == 0 || isPureVersion(version)) {
return version;
}
ArrayList<String> allVersions = (ArrayList<String>) getAllVersions(groupId, artifactId);

if(allVersions.isEmpty()) {
return version;
}
List<MavenVersionRange> ranges = extractRanges(version);
MavenVersion versionSpec = new MavenVersion(ranges);
List<String> satisfiedVersions = new ArrayList<>();
Expand Down Expand Up @@ -101,4 +106,12 @@ private static List<MavenVersionRange> extractRanges(String version) {

return ranges;
}

private static boolean isPureVersion(String version) {
return !version.contains("(")
|| !version.contains(")")
|| !version.contains("]")
|| !version.contains("[")
|| !version.contains(".");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ca7e318

Please sign in to comment.