How to use POMs as a dependency in Maven?

后端 未结 5 1850
醉酒成梦
醉酒成梦 2020-11-29 22:16

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:

相关标签:
5条回答
  • 2020-11-29 22:52

    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

    0 讨论(0)
  • 2020-11-29 22:52

    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 .

    0 讨论(0)
  • 2020-11-29 22:54

    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>
    
    0 讨论(0)
  • 2020-11-29 23:05

    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.

    0 讨论(0)
  • 2020-11-29 23:18

    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.

    0 讨论(0)
提交回复
热议问题