Home > Software engineering >  Can we generate maven dependency tree separatery in a folder with a multi-module project?
Can we generate maven dependency tree separatery in a folder with a multi-module project?

Time:10-14

We have un maven multimodule projets and we would like to generate a textual represensation of each maven modules in a folder.

Is it possible ?

CodePudding user response:

Say you have a parent module like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>deps</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>junit-module</module>
        <module>testng-module</module>
    </modules>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>7.4.0</version>
            </dependency>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit5-engine</artifactId>
                <version>5.0.0-ALPHA</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

</project>

and two child modules:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>junit-module</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.example</groupId>
        <artifactId>deps</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit5-engine</artifactId>
        </dependency>
    </dependencies>

</project>

and

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>tesng-module</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.example</groupId>
        <artifactId>deps</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
        </dependency>
    </dependencies>

</project>

so if you would run mvn dependency:tree under the parent folder you would get:

[INFO] ----------------------< org.example:junit-module >----------------------
[INFO] Building junit-module 1.0-SNAPSHOT                                 [2/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ junit-module ---
[INFO] org.example:junit-module:jar:1.0-SNAPSHOT
[INFO] \- org.junit:junit5-engine:jar:5.0.0-ALPHA:compile
[INFO]     - org.junit:junit5-api:jar:5.0.0-ALPHA:compile
[INFO]    |   - org.opentest4j:opentest4j:jar:1.0.0-ALPHA:compile
[INFO]    |  \- org.junit:junit-commons:jar:5.0.0-ALPHA:compile
[INFO]    \- org.junit:junit-engine-api:jar:5.0.0-ALPHA:compile
[INFO] 
[INFO] ----------------------< org.example:tesng-module >----------------------
[INFO] Building tesng-module 1.0-SNAPSHOT                                 [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ tesng-module ---
[INFO] org.example:tesng-module:jar:1.0-SNAPSHOT
[INFO] \- org.testng:testng:jar:7.4.0:compile
[INFO]     - com.beust:jcommander:jar:1.78:compile
[INFO]    \- org.webjars:jquery:jar:3.5.1:compile
[INFO] ------------------------------------------------------------------------

which is basically what you need. You also can run the same command from within child module folder hence get the tree for particular module separately.

CodePudding user response:

Thx for your answer but is it possible to generate separate tree for each modules into a specified folder ? (we want to link these trees in a documentation and refreshing the trees during a maven phase )

  • Related