Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of maven dependencies of org.apache.maven.project.MavenProject

Tags:

java

maven

I'm trying to get a list of dependencies of some maven artifacts using org.apache.maven.project.MavenProject.

My code is like this.

public List<Dependencies> loadProject() {
    Model mavenModel = new Model();
    mavenModel.setModelVersion("4.0.0");
    mavenModel.setGroupId("org");
    mavenModel.setArtifactId("wso2");
    mavenModel.setVersion("1.0.0");

    addDependency(mavenModel, "com.google.inject", "guice", "4.2.2");
    addDependency(mavenModel, "ch.qos.logback", "logback-classic", "1.2.3");

    MavenProject mavenProject = new MavenProject(mavenModel);

    //******* Need to resolve dependencies of `mavenProject` and *******
    //******* get the list of dependencies of this project.    *******

    return dependencies;
}

private static void addDependency(Model mavenModel, String groupId, String artifactId, String version) {


Dependency dependency = new Dependency();
    dependency.setGroupId(groupId);
    dependency.setArtifactId(artifactId);
    dependency.setVersion(version);
    mavenModel.addDependency(dependency);
}

Basically I'm trying to get the dependency tree results which returns by mvn dependency:tree command as a list by programmatically.

Example For the artifacts:

  • com.google.inject:guide:4.2.2
  • ch.qos.logback:logback-classic:1.2.3

Dependency list:

List = [
com.google.inject:guice:jar:4.2.2:compile,
javax.inject:javax.inject:jar:1:compile,
aopalliance:aopalliance:jar:1.0:compile,
com.google.guava:guava:jar:25.1-android:compile,
com.google.code.findbugs:jsr305:jar:3.0.2:compile,
org.checkerframework:checker-compat-qual:jar:2.0.0:compile,
com.google.errorprone:error_prone_annotations:jar:2.1.3:compile,
com.google.j2objc:j2objc-annotations:jar:1.1:compile,
org.codehaus.mojo:animal-sniffer-annotations:jar:1.14:compile,
ch.qos.logback:logback-classic:jar:1.2.3:compile,
ch.qos.logback:logback-core:jar:1.2.3:compile,
org.slf4j:slf4j-api:jar:1.7.25:compile
]
like image 829
Pramodya Mendis Avatar asked Mar 11 '20 12:03

Pramodya Mendis


People also ask

Which Maven command is to list all dependencies of a project hierarchically?

In Maven, you can use mvn dependency:tree to display the project dependencies in tree format.

Where can I find Maven dependencies?

In your project's POM, press Ctrl and hover the mouse over the dependency. Click the dependency to open the dependency's POM. In the dependency POM, view the active dependency, its transitive dependencies and their versions. You can check the origin from which the dependency was pulled in.

How do you analyze a dependency tree in Maven?

A project's dependency tree can be filtered to locate specific dependencies. For example, to find out why Velocity is being used by the Maven Dependency Plugin, we can execute the following in the project's directory: mvn dependency:tree -Dincludes=velocity:velocity.

How to attach artifacts to a Maven project?

Use the @ {MavenProjectHelper} to attach artifacts to a project. In spite of the 'throws' declaration on this API, this method has never thrown an exception since Maven 3.0.x. Historically, it logged and ignored a second addition of the same g/a/v/c/t.

What is Maven and how does it work?

Based on the concept of a project object model: builds, dependency management, documentation creation, site publication, and distribution publication are all controlled from the declarative file. Maven can be extended by plugins to utilise a number of other development tools for reporting or the build process.

How to get all dependencies of a project in jcabi?

Try to use jcabi-aether, which is a wrapper around Aether. First, you will need to get a list of all project dependencies: Then, for every one of them you can get a list of all transitive dependencies:

Does the Maven API ever throw an exception?

In spite of the 'throws' declaration on this API, this method has never thrown an exception since Maven 3.0.x. Historically, it logged and ignored a second addition of the same g/a/v/c/t. Now it replaces the file for the artifact, so that plugins (e.g. shade) can change the pathname of the file for a particular set of coordinates.


1 Answers

You can use the method public Set<Artifact> getArtifacts() of your MavenProject class which returns a set of artifacts representing all dependencies that the project has, including transitive ones.

NB: Contents are lazily populated, so depending on what phases have run dependencies in some scopes won't be included. eg. if only compile phase has run, dependencies with scope test won't be included.

All the information are coming from the documentation that you can find here.

like image 88
rakwaht Avatar answered Nov 10 '22 01:11

rakwaht