简体   繁体   English

Maven项目需要建立本地依赖关系

[英]Maven Project Requires Local Dependency to be Built

I have 2 Maven projects, A and B. Project A is entirely independent, while B requires A as a dependency. 我有2个Maven项目,A和B。项目A完全独立,而项目B需要A作为依赖项。 Currently, B is getting A from the maven repository: 当前,B正在从Maven存储库获取A:

<dependency>
  <groupId>com.myproject</groupId>
  <artifactId>projectA</artifactId>
  <version>2.0.0-SNAPSHOT</version>
</dependency>

What I would like to do is set up the dependency such that B can reference the local instance of A. I can do the following: 我想做的是设置依赖性,以便B可以引用A的本地实例。我可以执行以下操作:

<dependency>
  <groupId>com.myproject</groupId>
  <artifactId>projectA</artifactId>
  <version>2.0.0-SNAPSHOT</version>
  <scope>system</scope>
  <systemPath>${basedir}/../projectA/target/projectA-2.0.0-SNAPSHOT.jar</systemPath>
</dependency>

but this only works if A is already built (so projectA-2.0.0-SNAPSHOT.jar already exists). 但这仅在已经构建A的情况下才有效(因此projectA-2.0.0-SNAPSHOT.jar已经存在)。

Is there a way to force A to build before B in cases where I don't already have a build of A? 如果我还没有A的构建,是否可以强迫A在B之前构建?

One way is you create a parent pom, and add these projects as child modules. 一种方法是创建父pom,然后将这些项目添加为子模块。 So, the directory structure would look like this: 因此,目录结构如下所示:

+ pom.xml
|
+-A
|  \ 
|   + pom.xml
+-B 
   \
    + pom.xml

The parent pom would have <modules> tag, adding A and B projects as child modules: 父pom将具有<modules>标记,将AB项目添加为子模块:

<groupId>com.myproject</groupId>
<artifactId>parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>A</module>
    <module>B</module>
<modules>

And then, add A module as dependency to B module. 然后,将A模块添加为对B模块的依赖。 Just keep the group-id same for parent, A and B. And then refer the module using artifact-id. 只需将父代A和B的组ID保持相同即可。然后使用工件ID引用该模块。

pom.xml for B would be like: B pom.xml如下所示:

<parent>
    <groupId>com.myproject</groupId>
    <artifactId>parent</artifactId>
    <version>2.0.0-SNAPSHOT</version>
</parent>
<artifactId>projectB</artifactId>

<dependencies>
    <dependency>
        <groupId>com.myproject</groupId>
        <artifactId>projectA</artifactId>
        <version>2.0.0-SNAPSHOT</version>
    </dependency>
</dependencies>

Now, build the parent pom. 现在,构建父pom。 It will take care of build order. 它将负责构建顺序。

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

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