Skip to content

Commit

Permalink
Merge pull request #237 from manu4linux/issue-236
Browse files Browse the repository at this point in the history
Linker default dependency Lib Order #236
  • Loading branch information
ctrueden authored Jun 13, 2016
2 parents 43338ce + 5973951 commit 93cfe62
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@
<artifactId>maven-common-artifact-filters</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-tree</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-api</artifactId>
Expand Down
129 changes: 129 additions & 0 deletions src/main/java/com/github/maven_nar/AbstractDependencyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
import org.codehaus.plexus.archiver.manager.ArchiverManager;
import org.codehaus.plexus.util.StringUtils;

import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;

/**
* @author Mark Donszelmann
*/
Expand Down Expand Up @@ -101,6 +106,130 @@ public abstract class AbstractDependencyMojo extends AbstractNarMojo {
@Parameter(property = "includeGroupIds", defaultValue = "")
protected String includeGroupIds;

/**
* The computed dependency tree root node of the Maven project.
*/
protected DependencyNode rootNode;

/**
* The dependency tree builder to use.
*/
@Component( hint = "default" )
protected DependencyGraphBuilder dependencyGraphBuilder;

/**
* The computed dependency tree root node of the Maven project.
*/
private List<DependencyNode> LevelOrderList = new ArrayList<DependencyNode>();

/**
* Serializes the dependency tree of current maven project to a string of comma separated list
* of groupId:artifactId traversing nodes in Level-Order way (also called BFS algorithm)
*
* @return Dependency tree string of comma separated list
* of groupId:artifactId
*/
protected String dependencyTreeOrderStr()
{
String depLevelOrderStr = "";
DependencyNode libTreeRootNode;

try {
libTreeRootNode = getRootNodeDependecyTree();
} catch (MojoExecutionException exception) {
this.getLog().warn("linker Nar Default DependencyLibOrder is not used");
return depLevelOrderStr;
}

final List<DependencyNode> NodeList = depLevelOrderList(libTreeRootNode);

this.getLog().debug("{");
this.getLog().debug("Dependency Lib Order to be used::");

for (DependencyNode node : NodeList) {

if ( node != null ) {
String[] nodestring = node.toNodeString().split(":");
String usestring = nodestring[0] + ":" + nodestring[1];

this.getLog().debug(usestring);

if (!depLevelOrderStr.isEmpty()){
depLevelOrderStr= depLevelOrderStr + "," + usestring;
}else{
depLevelOrderStr= usestring;
}

}
}

this.getLog().debug("}");

return depLevelOrderStr;
}

/**
* Get List of Nodes of Dependency tree serialised by traversing nodes
* in Level-Order way (also called BFS algorithm)
*
* @param rootNode root node of the project Dependency tree
* @return the dependency tree string of comma separated list
* of groupId:artifactId
*/
private List<DependencyNode> depLevelOrderList(DependencyNode rootNode )
{

List<DependencyNode> NodeChildList = rootNode.getChildren();
//LevelOrderList.add(rootNode);

while (!NodeChildList.isEmpty()) {
NodeChildList = levelTraverseTreeList(NodeChildList);
}

return this.LevelOrderList;
}

/**
* helper function for traversing nodes
* in Level-Order way (also called BFS algorithm)
*/
private List<DependencyNode> levelTraverseTreeList(List<DependencyNode> NodeList )
{
this.LevelOrderList.addAll(NodeList);
List<DependencyNode> NodeChildList = new ArrayList<DependencyNode>();
for (DependencyNode node : NodeList) {
if ( (node != null) && (node.getChildren() != null) ) {
NodeChildList.addAll(node.getChildren());
}
}

return NodeChildList;
}


//idea from dependency:tree mojo
/**
* Get root node of the current Maven project Dependency tree generated by
* maven.shared dependency graph builder.
*
* @return root node of the project Dependency tree
* @throws MojoExecutionException
*/
private DependencyNode getRootNodeDependecyTree() throws MojoExecutionException{
try {
ArtifactFilter artifactFilter = null;

// works for only maven 3. Use of dependency graph component not handled for maven 2
// as current version of NAR already requires Maven 3.x
rootNode = dependencyGraphBuilder.buildDependencyGraph(getMavenProject(), artifactFilter);

} catch (DependencyGraphBuilderException exception) {
throw new MojoExecutionException("Cannot build project dependency graph", exception);
}

return rootNode;
}

/**
* To look up Archiver/UnArchiver implementations
*/
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/github/maven_nar/Linker.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ public class Linker {
@Parameter
private String narDependencyLibOrder;

/**
* <p>
* Specifies to use Default link ordering of libraries that come from mvn dependency tree.
* The Default link order (generated by nar) is a Level-order tree traversing list (also called BFS) of
* dependency tree, given as a comma separated list of groupId:artifactId.
* </p>
* <p>
* default Value is "false"
* </p>
*/
@Parameter(defaultValue = "false")
private boolean narDefaultDependencyLibOrder = false;

/**
* Specify that the linker should generate an intermediate manifest based on
* the inputs.
Expand Down Expand Up @@ -364,6 +377,11 @@ public final LinkerDef getLinker(final AbstractCompileMojo mojo, final CCTask ta
}
}

//if No user preference of dependency library link order is specified then use the Default one nar generate.
if ((this.narDependencyLibOrder == null) && (narDefaultDependencyLibOrder)) {
this.narDependencyLibOrder = mojo.dependencyTreeOrderStr();
}

// record the preference for nar dependency library link order
if (this.narDependencyLibOrder != null) {

Expand Down

0 comments on commit 93cfe62

Please sign in to comment.