0

I want my Java program to log the dependencies before anything. I am aware of the mvn dependency:tree command, but the Jar file created will not be executed on a computer with Maven.

How could we get the dependencies list during the execution of the program ?

Mattew Eon
  • 1,722
  • 1
  • 21
  • 38
  • print the classpath? – niekname Jan 09 '18 at 11:38
  • Will this print the dependencies present in the pom.xml file ? And how can I do this without a command line ? – Mattew Eon Jan 09 '18 at 11:40
  • Yes, dependencies specified in the pom.xml are added to the classpath when creating the build artifact (of course considering the scope of the dependencies). You can print the class path from java code. – niekname Jan 09 '18 at 11:44
  • Might be that this can help you? : https://stackoverflow.com/questions/38635488/how-to-include-maven-dependencies-in-manifest-file Then you'd just parse the manifest file and you'll be presented with the stuff used whilst creating the project.. – vegaasen Jan 09 '18 at 11:54
  • Or you can just navigate to the maven-folder within the META-INF-folder and locate your package structure and the pom.xml-file right there. From there, just parse the pom.xml-file for dependencies. – vegaasen Jan 09 '18 at 11:56

2 Answers2

0

You could just write your own parser, so you can define your custom formatting.

A very basic one (using only jdk built-in libraries) might look like this:

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
// other imports...

public static void printPomDependencies() throws IOException, SAXException, ParserConfigurationException {
    // pom relative to your project directory
    final File pomFile = new File("./pom.xml");

    DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = dBuilder.parse(pomFile);
    doc.getDocumentElement().normalize();
    final NodeList dependencyNodes = doc.getElementsByTagName("dependency");

    for (int i = 0; i < dependencyNodes.getLength(); i++) {
        final Node n = dependencyNodes.item(i);

        final NodeList list = n.getChildNodes();

        System.out.println("----------------------------------");
        for (int j = 0; j < list.getLength(); j++) {
            final Node n2 = list.item(j);
            // do not print empty text nodes or others...
            if (n2.getNodeType() != Node.ELEMENT_NODE) continue;


            System.out.println(n2.getNodeName() + ":" + n2.getTextContent());

        }
    }
} 

For instance this parser prints the following output:

----------------------------------
groupId:io.spring.platform
artifactId:platform-bom
version:Brussels-SR6
type:pom
scope:import
----------------------------------
groupId:org.springframework.boot
artifactId:spring-boot-starter-web
----------------------------------
groupId:org.springframework
artifactId:spring-context
version:5.0.2.RELEASE
----------------------------------
groupId:org.springframework.data
artifactId:spring-data-jpa
version:2.0.2.RELEASE
payloc91
  • 3,724
  • 1
  • 17
  • 45
  • This doesn't list transitive dependencies. – Cyril Jan 09 '18 at 12:22
  • I don't need transitive dependencies, I just want to list actual dependencies. But knowing the GroupId and ArtifactID, couldn't we read pom.xml files of each dependencies ? – Mattew Eon Jan 09 '18 at 13:05
  • And will this works too once the jar is builded, is the ./pom.xml will be found ? – Mattew Eon Jan 09 '18 at 13:26
  • 1
    @MattewEon I was trying but it got too messed up. If you want to try keep in mind that `Maven` dependencies are stored in a default location (unless you specify otherswise) `E.g: Windows -> C://Users//.m2/repository`. Dependencies follow this pattern: `groupId/artifactId/version/artifactId-version.pom` E.g: the first dep listed in the answer will be in (from ./m2 path) `io/spring/platform/platform-bom/Brussels-SR6/platform-bom-Brussels-sR6.pom` – payloc91 Jan 09 '18 at 13:29
  • @MattewEon 2) did not try that. – payloc91 Jan 09 '18 at 13:30
  • @MattewEon did you try anything? I'm curious about the result. Also, it would help future readers – payloc91 Jan 11 '18 at 12:09
  • II tried your answer and it works fine, I have to try it once from the jar file – Mattew Eon Jan 11 '18 at 12:11
0

This code snippet will output the list of dependencies used in your project:

class example
{

 File pomfile = new File("./pom.xml");

 MavenXpp3Reader reader = new MavenXpp3Reader();

 Model model = reader.read(new FileReader(pomfile));

 List < Dependency > dependencies = model.getDependencies();

 for (Dependency dependency: dependencies)

 {

     System.out.println("----------------------------------");

     System.out.println("Group Id: " + dependency.getGroupId());

     System.out.println("Artifact Id: " + dependency.getArtifactId());

     System.out.println("Version: " + dependency.getVersion());

 }
}

Note that in order to run the above code the following dependency will have to be included your pom.xml file:

Artifact ID : maven-model
version :  4.0.0-alpha-5
flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25