37

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

Community
  • 1
  • 1
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • Note: there is a great tut here: http://books.sonatype.com/mvnref-book/reference/pom-relationships-sect-pom-best-practice.html – Andrejs Dec 18 '17 at 20:11

5 Answers5

67

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.

Guillaume Darmont
  • 5,002
  • 1
  • 23
  • 35
  • 7
    Note that while this answers the question most people have that led most likely them here, it's not really what the OP asked. – Nicola Ambrosetti Sep 04 '17 at 11:01
  • Neither of the solutions provided above would achieve what OP want: **import all the modules (not dependencies) defined in com.sun.mail:all:1.5.0 pom into his own pom file** – Murphy Ng Jul 12 '19 at 09:10
  • works flawlessly! need to add both type and scope tag to make it work! – Gaurav Feb 04 '20 at 12:06
  • What is the point of adding pom in the first example ? Even without it all transivite dependencies will be added. – Olivier Masseau May 17 '23 at 11:04
12

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>
Felipe Pereira
  • 1,368
  • 16
  • 26
Chris Messina
  • 141
  • 1
  • 3
  • 1
    I do it all the time and it's really helpful. I also suggest versioning your mail-deps with the same number of JavaMail dependencies. This make obvious that mail-deps version 1.5.0 includes everything from JavaMail 1.5.0, for example. – Alex Oliveira Oct 30 '15 at 13:49
9

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

kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • 1
    As you cannot do exactly what you ask in Maven, it has become common to create a "bills-of-materials" artifact whose only purpose in life is declaring the dependencies and their versions. – Thorbjørn Ravn Andersen Jan 31 '17 at 16:19
2

If the pom you're trying to import, contains dependencies defined in a <dependencies/> section, and you would like to import them all, you can try the code below.

(Disclaimer: I haven't done this in a while): in your <dependencyManagement/> section, add the pom dependency like this:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>kung.fu<groupId>
            <artifactId>ninja</artifactId>
            <version>1.2.3</version>
            <scope>import</scope>
            <type>pom</type> <!-- Not too sure if you needed this
                                  when it's scoped as import,
                                  but just in case -->
        </dependency>
    </dependencies>
</dependencyManagement>

It may as well be the case that you define the dependency directly in the <dependencies/> section not needing the <dependencyManagement/> bit, but as far as I recall, it should be scoped import as shown above.

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • This should be the correct answer. It works for me in a project with a database-lib that imports jdbi-libs. With your solution I don't need all the the jdbi libs in my main project pom and just reference the database-lib in ``. – JackLeEmmerdeur Oct 25 '20 at 22:55
2

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 .

Oleg Kaploun
  • 287
  • 2
  • 6