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

在非JAR Maven项目之间共享公共资源,第1张

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

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

/src/main/resources

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

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

现在,将此配置添加到您的父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>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4978376.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存