在非JAR maven项目之间共享公共资源

mik*_*łak 6 jmeter maven-3 maven

我有几个Maven项目,比如a,b,c,从一个单亲继承(让我们称之为parent),并且也正在模块(不同的项目比parent,让我们叫它super).

这些项目都有pom包装.这些项目中的每一个都有特定的配置,但它们也有一个共同的部分.为了更具说明性,每个项目都有两个JMeter测试配置文件:一个专用于给定项目,另一个专用于所有项目.

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

解决方法是将所有这些内容合并到一起super,然后使用配置文件.但是,在这种情况下,我必须手动为每个配置单独构建(而现在我可以构建super).

有类似的问题,比如这个,但是它们处理jar插件,这与这种情况无关.

结构,供参考:

use*_*849 7

我已经将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.
Run Code Online (Sandbox Code Playgroud)

按照示例中的说明创建捆绑包.

现在,将此配置添加到您的父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>
Run Code Online (Sandbox Code Playgroud)

现在,告诉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>
Run Code Online (Sandbox Code Playgroud)

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

<properties>
  <module.file>a</module.file>
</properties>
Run Code Online (Sandbox Code Playgroud)

对此进行调整以满足您的使用案例.

----编辑----

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

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

我也将此技术与Enforcer插件一起使用 - 父级定义了应该应用于从父级继承的项目的实施者规则,但在构建时无法满足规则.如果插件提供"skip"属性,则可以在此配置文件中启用该属性,而不是在插件配置中使用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>
Run Code Online (Sandbox Code Playgroud)