我们有一个 Common
git存储库,其中包含父 pom.xml
和一些模块。父 pom.xml
看起来像这样(简化):
<modelVersion>4.0.0</modelVersion>
<groupId>group</groupId>
<artifactId>common</artifactId>
<packaging>pom</packaging>
<modules>
<!-- Utility classes -->
<module>utils</module>
<!-- Data transfer objects -->
<module>dto</module>
</modules>
</dependencyManagement>
<dependencies>
<dependency>
<!-- Version of the module -->
<groupId>group</groupId>
<artifactId>utils</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.0</version>
</dependency>
...
</dependencies>
</dependencyManagement>
dependencyManagement
用于在所有模块上提供一致的版本化。
现在,我们想在其他项目中使用Common
模块的一部分(每个git存储库),但仍然保留dependencyManagement
。因此,我们创建了一个ProjectX
存储库,并将Common
添加为git-submodule
。ProjectX
具有以下pom.xml
(简化):
<parent>
<groupId>group</groupId>
<artifactId>common</artifactId>
<relativePath>../Common/pom.xml</relativePath>
</parent>
</dependencies>
<dependency>
<groupId>group</groupId>
<artifactId>utils</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
</dependency>
</dependencies>
以这种方式,我们可以使用Common.utils
的类并具有依赖性一致性。但是,在每次更新Common
之后,我们必须致电:
mvn clean install
在Common git-submodule
中用于ProjectX pom.xml
以找到utils依赖。
问题:
- 可以将
maven
命令集成到pom.xml? - 有更好的方法来处理子模块依赖性吗?
带有git子模块的结构是不幸的。我不会这样做,这不是马文哲学。
我会分开两件事:
-
如果您需要多模块项目之外的一个模块,则只需以通常的方式将其声明为依赖。
-
如果要在多模块项目和其他项目之间共享配置或依赖关系,请构建用于不同项目的父pom(是否是多模块)。