在非JAR Maven项目之间共享公共资源
问题内容:
我有几个Maven项目,比如a
,b
,c
,从一个单亲继承(让我们称之为parent
),并且也正在模块(不同的项目比parent
,让我们叫它super
)。
这些项目都有pom
包装。这些项目中的每一个都有特定的配置,但是它们也有一个共同的部分。更具体的说,每个项目有两个JMeter测试配置文件:一个专门用于给定项目,另一个则对于所有项目都是通用且相同的。
问题是-我应该如何配置POM,以便在项目之间共享此公共配置文件?
一种解决方法是将它们全部合并到中super
,并使用配置文件。但是,在这种情况下,我将必须为每个配置手动进行单独的构建(而现在我可以只构建super
)。
也有类似的问题,例如this,但它们处理的是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.
现在,将此配置添加到您的父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- packaging.marker
到父项目的basedir。然后将此配置文件添加到父POM。构建父项目后,Maven将找到标记文件,启用概要文件,并禁用概要文件中包含的所有执行。构建子项目时,标记文件不存在,因此POM主要部分中的配置将生效。
我也将这种技术与Enforcer插件一起使用-
父级定义了强制规则,该规则应应用于从父级继承的项目,但是在构建时不能满足该规则。如果插件提供“跳过”属性,则可以在此配置文件中启用该属性,而不是在插件配置中使用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>