How to use POMs as a dependency in Maven?

Is there a way to add a pom type dependency to my POM and get all its modules?

JavaMail is a good example. Maven Central Repo has a parent POM called: com.sun.mail:all:1.5.0 with modules: mail, mailapi, mailapijar, smtp, imap, gimap, pop3, and dsn.

However, the "all" artefact only has a single file: pom.xml Is there a way to add this "all" artefact as a dependency to my POM and get all its modules? I am 90% sure this is not the right way to use dependencies in Maven, but I want to hear it from an expert on The Stack.

Ideas:

  • <dependencies><dependency>...<type>pom</type></dependency></dependencies>
  • <dependencyManagement><dependencies><dependency>...<type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

Related: Netbeans: maven dependencies of type pom


You have to go with

<dependencies>
  <dependency>
     <groupId>com.my</groupId>
     <artifactId>commons-deps</artifactId>
     <type>pom</type>
  </dependency>
</dependencies>

This will transitively add all dependencies declared in com.my:commons-deps to your current POM.

Using

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>...</groupId>
            <artifactId>...</artifactId>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

works as a simple 'include' of artifacts versions in your dependency management. Thus, it won't add any dependency in your project.


I believe you can create your own POM which aggregates the dependencies you want, and then in your original project add a dependency on that aggregate pom. You will still have to add dependencies on each individual module in your dependency POM, but it will be abstracted from the actual project POMs and allows those dependencies to be managed in one place, which could become useful if you end up having multiple projects that depend on that set of dependencies.

In your example you could create a new pom 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>mail-deps</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <dependencies>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>mailapi</artifactId>
            <version>1.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>mailapijar</artifactId>
            <version>1.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>imap</artifactId>
            <version>1.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>gimap</artifactId>
            <version>1.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>pop3</artifactId>
            <version>1.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>dsn</artifactId>
            <version>1.5.0</version>
        </dependency>
    </dependencies>
</project>

Then in your original project just add:

<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/maven-v4_0_0.xsd">
 ...
 <modules>
   <module>src/main/java/com/mycompany</module>
 </modules>
 ...
 <dependencies>
     <dependency>
         <groupId>com.mycompany</groupId>
         <artifactId>mail-deps</artifactId>
         <version>1.0.0</version>
         <type>pom</type>
     </dependency>
 </dependencies>
</project>

The short answer: You cannot do this in Maven.

The other answers make only the "all" POM a dependency. Does not solve the issue. Another answer tries to import the dependencies of the "all" POM. I don't need the dependencies; I need the (child) modules of the "all" POM. Again, does not solve the issue.

Side note: I was using the JavaMail library incorrectly. I only needed to add one dependency: com.sun.mail:javax.mail:1.5.0


As someone already wrote above : You can't do it . But this is what i did and it worked . Lets assume you have some pom file (JavaMail in your example) with following :

<type>pom</type>
<dependencyManagement><dependencies><dependency></dependencyManagement>

And You want copy all jars mentioned in this pom to some place . This is what i did and it is fast working solution
Open original pom and just copy-paste all dependencies section from original pom file to your new pom as is . Of course use maven dependency plugin to copy all .