简体   繁体   English

Maven从项目继承依赖关系到另一个项目

[英]Maven inherit dependencies from a project into another

Given a Maven project A producing a jar: 给定一个生产罐子的Maven项目A:

<groupId>myprojects</groupId>
<artifactId>A</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
    <dependency>
        <groupId>some.needed.group</groupId>
        <artifactId>some.needed.artifact1</artifactId>
    </dependency>
    <dependency>
        <groupId>some.needed.group</groupId>
        <artifactId>some.needed.artifact2</artifactId>
    </dependency>
</dependencies>

and a project B depending on A and producing an enterprise archive: 以及一个依赖于A并生成企业档案的项目B:

<groupId>myprojects</groupId>
<artifactId>B</artifactId>
<version>1.0</version>
<packaging>ear</packaging>
<dependencies>
    <dependency>
        <artifactId>A</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

how can I achieve to have the project B ear artifact to include not only the project B's jar artifact but also its dependent artifacts some.needed.artifact1 and some.needed.artifact2? 如何实现项目B的耳朵工件不仅包含项目B的jar工件,还包括其相关工件some.needed.artifact1和some.needed.artifact2?


I edited and added this because it seems that the behaviour occurs only when running mvn from Bamboo: The strange thing is that it is working when I'm running mvn locally from Eclipse but not when it's being ran by the Bamboo continuous integration server. 我编辑并添加了它,是因为似乎仅在从Bamboo运行mvn时才会发生该行为:奇怪的是,当我从Eclipse本地运行mvn时它正在工作,而由Bamboo持续集成服务器运行时却没有。 On a local run I have the application.xml generated correctly with references to all the transitive dependencies the corresponding jar files copied to the lib folder. 在本地运行时,我正确引用了所有传递依赖项,并且将相应的jar文件复制到lib文件夹,正确生成了application.xml。 On Bamboo plan the generated application.xml does not include the references and the jar files are not in the lib folder. 在Bamboo计划上,生成的application.xml不包含引用,并且jar文件不在lib文件夹中。

First as a correction I think dependency should be on A, not on B , like this : 首先,作为纠正,我认为依赖应该依赖于A,而不是依赖于B,如下所示:

   <dependencies>
    <dependency>
        <artifactId>A</artifactId>
        <version>1.0</version>
    </dependency>
 </dependencies>

And about the original question of having the dependent jars , i think they should be available in your EAR file , as you have dependency on A project and which in turn depends on some.needed.artifact1 and some.needed.artifact2 (transitive dependencies) as the scope you provided is default so maven will take it as compile and all the jars will be bundled in your ear file. 关于具有依赖jar的原始问题,我认为它们应该在您的EAR文件中可用,因为您依赖A project ,而依赖于some.needed.artifact1 and some.needed.artifact2 (传递依赖)因为您提供的范围是默认的,所以maven会将其作为编译对象,并且所有jar都将捆绑在您的ear文件中。

Thanks 谢谢

In project B which produces the ear, you can include a dependency to A like this 在产生耳朵的项目B中,您可以像这样包含对A的依赖

<modules>
  <module>project A</module>
</modules>
<dependencies>
  <dependency>
    <groupId>some.needed.group</groupId>
    <artifactId>A</artifactId>
    <version>1.0</version>
  </dependency>
</dependencies>

In project A, define the parent project to be project B like the following 在项目A中,将父项目定义为项目B,如下所示

<parent>
        <groupId>myprojects</groupId>
        <artifactId>B</artifactId>
        <version>1.0</version>
        <name>project B</name>
</parent>
<name>project A</name>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM