gpt4 book ai didi

maven - 在非 JAR Maven 项目之间共享公共(public)资源

转载 作者:行者123 更新时间:2023-12-02 09:40:05 26 4
gpt4 key购买 nike

我有几个 Maven 项目,比如 abc,它们继承自单个父项(我们称之为 parent ),并且也是模块(与 parent 不同的项目,我们称之为 super)。

这些项目都有一个pom包装。这些项目中的每一个都有特定的配置,但它们也有共同的部分。更具体地说,每个项目都有两个 JMeter 测试配置文件:一个专门用于给定项目,另一个对于所有项目都是通用且相同的。

问题是 - 我应该如何配置 POM,以便在项目之间共享这个通用配置文件?

解决方法是将它们全部合并到 super 中,并使用配置文件。但是,在这种情况下,我必须手动为每个配置进行单独的构建(而现在我可以只构建 super)。

还有类似的问题,例如this one ,但它们处理的是 jar 插件,这与本例无关。

结构,供引用:

  • POM 继承:

        parent
    |
    -------------
    | | |
    a b c
  • 文件结构:

    super
    |
    |-a
    |
    |-b
    |
    |-c

最佳答案

我已经使用了maven-remote-resources-plugin出于类似的目的。创建一个单独的 jar 类型的资源项目 (com.company:resourceProj)。将 JMeter 资源文件放入 /src/main/resources 中。

/src/main/resources/common.properties  (your filenames obviously)
/src/main/resources/a.properties
etc.

按照 example 中的说明进行操作创建 bundle 。

现在,将此配置添加到您的父 POM(如果需要,可以在测试配置文件中):

<properties>
<shared.resources.dir>${project.build.directory}/shared-resources</shared.resources.dir>
</properties>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<executions>
<execution>
<id>load-resources</id>
<phase>initialize</phase>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>com.company:resourceProj:version</resourceBundle>
</resourceBundles>
<attached>false</attached>
<outputDirectory>${shared.resources.dir}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

现在,告诉 Maven 这些是测试资源。如果您的测试资源元素在模块之间保持一致,则也可以放入父级中,如果它们不同,则放入模块 POM 中。 (根据我的经验,子项目中定义的 Maven 3 资源优先于父项目;它们不会合并。)

<testResources>
<testResource>
<directory>${shared.resources.dir}</directory>
<includes>
<include>common.properties</include>
<include>${module.file}.properties</include>
</includes>
</testResource>
<!-- any other test resources here -->
</testResources>

在子模块中,定义资源模块属性(这是模块a):

<properties>
<module.file>a</module.file>
</properties>

对此进行调整以满足您的用例。

---- 编辑 ----

如果将配置放入父 POM 中,则父 POM 可能无法构建,具体取决于子 POM 提供的配置。当我们构建共享的基础/父项目时,我们不想要求定义子项目(继承者)应提供的所有属性。因此,我们在构建共享项目时激活此配置文件,以绕过仅适用于 child 的任何内容。

为此,请将一个空文件 pom-packaging.marker 添加到父项目的 basedir 中。然后将此配置文件添加到父 POM 中。当构建父项目时,Maven 将找到标记文件,启用配置文件,并禁用配置文件中包含的所有执行。当构建子项目时,标记文件不存在,因此POM主要部分中的配置将生效。

我也在 E​​nforcer 插件中使用了这种技术 - 父级定义了应应用于从父级继承的项目的执行器规则,但在构建时无法满足规则。如果插件提供“跳过”属性,您可以在此配置文件中启用该属性,而不是在插件配置中使用 Phase = none。

<profile>
<id>pom-packaging</id>
<activation>
<file>
<exists>pom-packaging.marker</exists>
</file>
</activation>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<executions>
<execution>
<id>load-resources</id>
<phase>none</phase> <!-- disables this execution -->
</execution>
</executions>
</plugin>
.... other plugin executions here ....
</plugins>
</build>
</profile>

关于maven - 在非 JAR Maven 项目之间共享公共(public)资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11454061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com