在非 JAR Maven 项目之间共享公共资源 [英] Sharing common resources between non-JAR maven projects

查看:30
本文介绍了在非 JAR Maven 项目之间共享公共资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 Maven 项目,比如 a,b,c,从单个父级继承(我们称之为 parent),同时也是模块(属于与 parent 不同的项目,我们称之为 super).

I have several Maven projects, say a,b,c, inheriting from a single parent (let's call it parent), and also being modules (of a different project than parent, let's call it super).

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

These projects all have a pom packaging. Each of these projects has specific configuration, but they also have a common part. To be more speficic, each project two JMeter test configuration files: one specialized for the given project, and another one that is common and identical for all projects.

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

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

A workaround would be to merge all of them into super, and use profiles. However, in this case, I would have to do a separate build for each configuration manually (whereas now I can just build super).

有类似的问题,比如这个,但他们处理jar 插件,与本案例无关.

There are similar questions, like this one, but they deal with the jar plugin, which is not relevant for this case.

结构,供参考:

  • POM 继承:

  • POM Inheritance:

    parent
      |
-------------
|     |     |
a     b     c

  • 文件结构:

  • File structure:

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

  • 推荐答案

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

    I have used the maven-remote-resources-plugin for a similar purpose. Create a separate resources project (com.company:resourceProj) of type jar. Put the JMeter resource files in /src/main/resources.

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

    按照示例 创建捆绑包.

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

    Now, add this config to your parent POM (in a testing profile if you want):

    <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 资源优先于父项目;它们不会合并.)

    Now, tell Maven these are test resources. If your test resource elements are consistent across the modules, this can go in the parent too, if they are different it goes in the module POM. (In my experience with Maven 3 resources defined in a child project take precedence over the parent's; they aren't merged.)

    <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):

    In the child module, define the resources module property (this is module a):

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

    调整它以满足您的用例.

    Adapt this to meet your use case.

    ---- 编辑----

    ---- Edit ----

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

    If the configuration is placed into a parent POM, the parent POM may fail to build depending on what configuration is provided by the child. When we are building the shared base/parent projects we don't want to require that all of the properties that should be provided by child projects (inheriters) are defined. So we activate this profile when building the shared projects to bypass anything that only applies to children.

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

    To do this, add an empty file pom-packaging.marker to the parent project's basedir. Then add this profile to the parent POM. When the parent project is built, Maven will find the marker file, enable the profile, and disable all of the executions included in the profile. When a child project is built, the marker file doesn't exist, so the configuration in the main part of the POM will take effect.

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

    I've used this technique with the Enforcer plugin as well - the parent defines the enforcer rules that should be applied to projects inheriting from the parent, but cannot satisfy the rules when it is built. If the plugin provides a "skip" property, you may enable that in this profile instead of using phase = none in plugin configuration.

    <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>
    

    这篇关于在非 JAR Maven 项目之间共享公共资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆