【问题标题】:Pass properties to maven sub-module, which does not have current module as parent将属性传递给 maven 子模块,该子模块没有当前模块作为父模块
【发布时间】:2018-08-31 12:32:30
【问题描述】:

我有几个 maven 项目:

  • commons-lib(简单的 Java 项目
  • 用户服务(Spring-Boot 驱动的项目
  • 复合服务(Spring-Boot 驱动的项目
  • 前端服务(Spring-Boot-/Angular2 驱动的项目
  • all-services-parent(构建其他一切的父项目

虽然 commons-lib 不太可能单独发布,但所有其他项目(父项目除外)可能会单独发布。

如果列表中的前四个是第五个的子模块,它们是否必须将其父级设置为父级(例如 all-services-parent)作为回报?

由于我想在用户服务和复合服务中包含 commons-lib,我知道我必须先构建它。但是:上面的每个服务都可以单独发布——那么哪种建筑结构最适合我的需要?

会是:

-- all-services-parent
   |-- (maven sub-module) commons-lib
   |-- (maven sub-module) user-service
   |-- (maven sub-module) composite-service
   |-- (maven sub-module) frontend-service

或:

-- all-services-parent
   |-- user-service-parent
       |-- (maven sub-module) commons-lib
       |-- (maven sub-module) user-service
   |-- composite-service-parent
       |-- (maven sub-module) commons-lib
       |-- (maven sub-module) composite-service
   |-- frontend-service

第二个构建结构允许我通过在 all-services-parent 上调用“mvn clean install”来构建所有 JAR,同时仍然能够通过在相应父级上调用“mvn clean install”来正确构建单独的项目,但是真的是这样吗?

在我当前的设置中,我尝试使用第一个建筑结构,但因为例如复合服务将“spring-boot-starter-parent”设置为其父级,我无法访问“all-services-parent”-模块中的属性或任何内容。

我阅读了Maven parent pom vs modules pom(一个起初看起来很有希望的问题),但它并没有像我希望的那样适用于我的案例。

【问题讨论】:

    标签: java maven spring-boot


    【解决方案1】:

    尝试导入spring boot parent,而不是像这样继承它:

    <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>1.5.9.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
           </dependencies>
    </dependencyManagement>
    

    【讨论】:

    • 但是之后我会怎么做呢?我是否应该始终将模块父级设置为子模块的 pom.xml 中的父级?
    • 看来这个建议最接近解决我的问题。我决定使用第一个构建结构并使用配置文件,这些配置文件默认处于活动状态,但也允许构建单独的模块组。我仍然不知道如何将属性传递给子模块,但我绕过了这个需求。此外,在例如使用变量似乎不是一个好主意。 pom.xml 中的版本或组标签,因为这会不必要地将模块捆绑在一起。
    【解决方案2】:

    父模块属性文件可以被子模块访问,但子模块属性文件不能访问其他子模块。

    您需要为每个子模块做单独的属性文件。

    【讨论】:

    • 我知道(子模块无法访问其他子模块的属性)。就我而言,我只是不确定我应该尝试设置第一个建筑结构还是第二个(请参阅我帖子中的前置标签)。
    【解决方案3】:

    在你的 pom.xml 文件中添加这样的子模块并给出它的绝对路径。
    这应该是工作

    <modules>
            <module>../TestWeb</module>
            <module>../TestBusiness</module>
            <module>../MsgScheduler</module>
            <module>../Connector</module>                   
    </modules>
    

    【讨论】:

    • 我已经完成了 - 我确实阅读了手册。只是我无法继承属性,因为我认为我无法将父级调整为除了 spring-boot-starter-parent 之外的任何其他内容,但显然我可以(参见 Sandor Juhos 的回复)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多