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

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

问题描述

我有几个Maven项目,例如abc,它们是从单个父级继承的(我们称其为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
    

  • 推荐答案

    我使用了

    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可能无法构建,具体取决于子级提供的配置.当我们构建共享的基础/父项目时,我们不想要求定义子项目(继承程序)应提供的所有属性.因此,我们在构建共享项目时会激活此配置文件,以绕过仅适用于子级的所有内容.

    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添加到父项目的baseir中.然后将此配置文件添加到父POM.构建父项目后,Maven将找到标记文件,启用概要文件,并禁用概要文件中包含的所有执行.构建子项目时,标记文件不存在,因此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天全站免登陆