简体   繁体   English

Maven:在pom.xml中编译外部依赖项

[英]Maven: Compile external dependencies within pom.xml

We have a Common git repository containing a parent pom.xml and some modules. 我们有一个Common git仓库,其中包含一个父pom.xml和一些模块。 The parent pom.xml looks like this (simplified): 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>

The dependencyManagement is used to provide consistent versioning across all modules. dependencyManagement用于在所有模块之间提供一致的版本控制。

Now we want to use parts of the Common modules in other projects (own git repository each) and still keep the dependencyManagement . 现在,我们要在其他项目中使用Common模块的各个部分(每个项目都有自己的git存储库),并且仍然保留dependencyManagement Thus, we created, for instance, a ProjectX repository and added Common as an git-submodule . 因此,我们创建了一个ProjectX存储库,并将Common添加为git-submodule ProjectX has the following pom.xml (simplified): 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>

This way, we can use classes of Common.utils and have dependency consistency. 这样,我们可以使用Common.utils类,并具有依赖项一致性。 However, after each update of Common we have to call: 但是,在每次更新Common之后,我们必须调用:

mvn clean install

in the Common git-submodule for ProjectX pom.xml to find the utils dependency. ProjectX pom.xmlCommon git-submodule ProjectX pom.xml中找到utils依赖项。

Question: 题:

  • Can the maven command be integrated into the pom.xml? 可以将maven命令集成到pom.xml中吗?
  • Is there a better way to handle the submodule dependency? 有没有更好的方法来处理子模块依赖性?

The construction with the git submodule is unfortunate. git子模块的构建很不幸。 I would not do it, this is not Maven philosophy. 我不会这样做,这不是Maven的哲学。

I would separate two things: 我将分开两件事:

  • If you need one of the modules outside the multi-module project, just declare it as dependency in the usual way. 如果您需要多模块项目之外的模块之一,只需以通常的方式将其声明为依赖项即可。

  • If you want to share configuration or dependencyManagement between the multi-module project and other projects, construct a parent pom that you use for the different projects (may they be multi-module or not). 如果要在多模块项目和其他项目之间共享配置或dependencyManagement,请构造用于不同项目的父pom(无论它们是否为多模块)。

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

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