Is there a way to add a pom
type dependency to my POM and get all its modules?
有没有办法将pom类型依赖项添加到我的POM并获取其所有模块?
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.
JavaMail就是一个很好的例子。 Maven Central Repo有一个父POM,名为:com.sun.mail:all:1.5.0 with modules:mail,mailapi,mailapijar,smtp,imap,gimap,pop3和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.
但是,“all”artefact只有一个文件:pom.xml有没有办法将这个“all”artefact作为依赖项添加到我的POM并获取其所有模块?我90%肯定这不是在Maven中使用依赖项的正确方法,但我希望从Stack上的专家那里听到它。
Ideas:
思路:
<dependencies><dependency>...<type>pom</type></dependency></dependencies>
- <依赖性> <依赖性> ... <类型> POM
<dependencyManagement><dependencies><dependency>...<type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
-
<依赖性> <依赖性> ... <类型> POM <范围> 进口
Related: Netbeans: maven dependencies of type pom
相关:Netbeans:类型为pom的maven依赖项
5 个解决方案
#1
41
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.
这会将com.my:commons-deps中声明的所有依赖项传递给当前的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.
在依赖关系管理中作为工件版本的简单“包含”。因此,它不会在您的项目中添加任何依赖项。
#2
6
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.
我相信你可以创建自己的POM来聚合你想要的依赖项,然后在原始项目中添加对该聚合pom的依赖。您仍然必须在依赖项POM中的每个单独模块上添加依赖项,但它将从实际项目POM中抽象出来,并允许在一个地方管理这些依赖项,如果您最终拥有多个依赖项目,这可能会很有用在那组依赖项上。
In your example you could create a new pom like this:
在您的示例中,您可以创建一个像这样的新pom:
<?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>
#3
3
The short answer: You cannot do this in Maven.
简短的回答:你不能在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.
其他答案只使“全部”POM成为依赖。没有解决问题。另一个答案是尝试导入“所有”POM的依赖关系。我不需要依赖项;我需要“全部”POM的(子)模块。再次,并没有解决问题。
Side note: I was using the JavaMail library incorrectly. I only needed to add one dependency: com.sun.mail:javax.mail:1.5.0
旁注:我错误地使用了JavaMail库。我只需要添加一个依赖项:com.sun.mail:javax.mail:1.5.0
#4
1
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.
如果您要导入的pom包含
(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.
您也可以直接在不需要
#5
1
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 :
正如上面已经写过的人:你不能这样做。但这就是我所做的并且有效。让我们假设你有一些pom文件(在你的例子中是JavaMail),如下所示:
<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 .
并且你想把这个pom中提到的所有罐子复制到某个地方。这就是我所做的,它是快速工作的解决方案打开原始的pom,只需将原始pom文件中的所有依赖项部分复制粘贴到新的pom中。当然使用maven依赖插件来复制所有。
#1
41
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.
这会将com.my:commons-deps中声明的所有依赖项传递给当前的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.
在依赖关系管理中作为工件版本的简单“包含”。因此,它不会在您的项目中添加任何依赖项。
#2
6
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.
我相信你可以创建自己的POM来聚合你想要的依赖项,然后在原始项目中添加对该聚合pom的依赖。您仍然必须在依赖项POM中的每个单独模块上添加依赖项,但它将从实际项目POM中抽象出来,并允许在一个地方管理这些依赖项,如果您最终拥有多个依赖项目,这可能会很有用在那组依赖项上。
In your example you could create a new pom like this:
在您的示例中,您可以创建一个像这样的新pom:
<?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>
#3
3
The short answer: You cannot do this in Maven.
简短的回答:你不能在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.
其他答案只使“全部”POM成为依赖。没有解决问题。另一个答案是尝试导入“所有”POM的依赖关系。我不需要依赖项;我需要“全部”POM的(子)模块。再次,并没有解决问题。
Side note: I was using the JavaMail library incorrectly. I only needed to add one dependency: com.sun.mail:javax.mail:1.5.0
旁注:我错误地使用了JavaMail库。我只需要添加一个依赖项:com.sun.mail:javax.mail:1.5.0
#4
1
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.
如果您要导入的pom包含
(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.
您也可以直接在不需要
#5
1
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 :
正如上面已经写过的人:你不能这样做。但这就是我所做的并且有效。让我们假设你有一些pom文件(在你的例子中是JavaMail),如下所示:
<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 .
并且你想把这个pom中提到的所有罐子复制到某个地方。这就是我所做的,它是快速工作的解决方案打开原始的pom,只需将原始pom文件中的所有依赖项部分复制粘贴到新的pom中。当然使用maven依赖插件来复制所有。