1

want to print maven dependency tree (all the dependencies including transitive dependencies) programmatically by just reading pom.xml file without connecting to remote repository.

Janki
  • 21
  • 5
  • Without reading the (remote) repository it is not possible. – khmarbaise Jan 07 '22 at 00:06
  • If I use remote repository, nothing is printing on console. If anyone knows, how to do it connecting to remote repository, please share. – Janki Jan 07 '22 at 00:13
  • Please show your code ? Why not using Maven itself or `maven-dependency-plugin:tree` ? etc. ? – khmarbaise Jan 07 '22 at 00:17
  • It might be worth to take a look here: https://github.com/apache/maven-resolver/tree/master/maven-resolver-demos/maven-resolver-demo-snippets/src/main/java/org/apache/maven/resolver/examples – khmarbaise Jan 07 '22 at 00:18
  • want to print dependency tree at runtime using java code. – Janki Jan 07 '22 at 00:19
  • MavenXpp3Reader mavenReader = new MavenXpp3Reader(); Model model; File pomFile = new File("/pom.xml"); model = mavenReader.read(new FileReader(pomFile)); MavenProject project=new MavenProject(model); – Janki Jan 07 '22 at 00:20
  • DefaultArtifact pomArtifact = new DefaultArtifact(project.getId()); RepositorySystem repoSystem = Booter.newRepositorySystem( Booter.selectFactory( args ) ); RepositorySystemSession repoSession = Booter.newRepositorySystemSession( repoSystem ); // TODO List remoteRepos = project.getRemoteProjectRepositories(); List ret = new ArrayList(); Dependency dependency = new Dependency(pomArtifact, "compile"); – Janki Jan 07 '22 at 00:21
  • 1
    At runtime? Why ? At runtime you don't have access to your repositories or maybe not to your pom file at all etc. ? – khmarbaise Jan 07 '22 at 00:21
  • CollectRequest collectRequest = new CollectRequest(); collectRequest.setRoot(dependency); collectRequest.setRepositories(remoteRepos); DependencyNode node = repoSystem.collectDependencies(repoSession, collectRequest).getRoot(); DependencyRequest projectDependencyRequest = new DependencyRequest(node, null); repoSystem.resolveDependencies(repoSession, projectDependencyRequest); PreorderNodeListGenerator nlg = new PreorderNodeListGenerator(); node.accept(nlg); – Janki Jan 07 '22 at 00:21
  • ret.addAll(nlg.getDependencies(true)); for(Dependency dep:ret){ System.out.println(dep.getArtifact()); } – Janki Jan 07 '22 at 00:21
  • The MavenXpp3 reader will only give the plain pom content...nothing resolved... – khmarbaise Jan 07 '22 at 00:21
  • Take a deep look into Maven Resolver (see answer or in comment). – khmarbaise Jan 07 '22 at 00:22
  • github.com/apache/maven-resolver/tree/master/… I have tried it and it is working for maven dependency, but when I want to use custom group id, artifact id and version it is not giving the output. – Janki Jan 07 '22 at 00:24
  • how can we connect to our own repo instead of https://repo.maven.apache.org/maven2/? – Janki Jan 07 '22 at 00:26

3 Answers3

0

Not really possible, sorry. Also, have you checked the answer and comments here? How can you display the Maven dependency tree for the *plugins* in your project?

James McPherson
  • 2,476
  • 1
  • 12
  • 16
  • want to print dependency tree using java code not using maven command. – Janki Jan 07 '22 at 00:09
  • Then you're going to have to re-implement the Maven dependency analyser yourself. I believe this to be a suboptimal choice. – James McPherson Jan 07 '22 at 02:57
  • In Maven project, inside external libraries, every jar has pom.properties file, Is it possible to read values from each and every jar's pom.properties in java code? – Janki Jan 07 '22 at 03:06
0

I can recommend to take a look at the maven resolver project which has some example code which might be sufficient as a starting point:

https://github.com/apache/maven-resolver/tree/master/maven-resolver-demos/maven-resolver-demo-snippets/src/main/java/org/apache/maven/resolver/examples

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
0

You can do this by using ProcessBuilder to retrieve the result of maven's dependency:tree command.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public static void main(String[] args) {

    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("cmd.exe", "/c", "mvn -f \"C:\\myprojectpath\"", "dependency:tree");

    try {

        Process process = processBuilder.start();
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }

        int exitCode = process.waitFor();
        System.out.println("\nExited with error code : " + exitCode);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

Read more here.

k4k4sh1
  • 115
  • 1
  • 10