我有一个包含3个子项目的父项目:
parent
project-1
/src/main/resources/config.xml
project-2
/src/main/resources/config.xml
project-3
/src/main/resources/config.xml
在config.xml
阶段使用配置generate-sources
。对于这三个项目,config.xml
完全相同。但是,对于每个项目,此config.xml
的使用情况都不同。
在project-X中,我指的是config.xml
如下:
<build>
<plugins>
<plugin>
<groupId>some-group</groupId>
<artifactId>some-artifact</artifactId>
<executions>
<execution>
<goals>
<goal>some-goal</goal>
</goals>
<configuration>
<input>src/main/resources/config.xml</input>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
在所有3个项目之间分享此常见config.xml
的最佳方法是什么?
答案 0 :(得分:1)
您可以在此使用build-helper-maven-plugin
。
项目结构
shared-resources-project +-src +-main +-resources `config.xml +-project-A `pom.xml +-project-B `pom.xml +-project-C `pom.xml `pom.xml
shared-resources-project/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my</groupId>
<artifactId>shared-resources-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>project-A</module>
<module>project-B</module>
<module>project-C</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-sources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.parent.basedir}/src/main/resources</directory>
<includes>
<include>config.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>some-group</groupId>
<artifactId>some-artifact</artifactId>
<executions>
<execution>
<id>some-plugin-job</id>
<phase>generate-sources</phase>
<goals>
<goal>some-goal</goal>
</goals>
<configuration>
<input>${project.build.outputDirectory}/config.xml</input>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
shared-resources-project/src/main/resources/config.xml
<config>
<parameter>${custom-value}</parameter>
</config>
project-X/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>my</groupId>
<artifactId>shared-resources-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-X</artifactId>
<properties>
<custom-value>Project-X Value</custom-value>
</properties>
</project>
现在,让我们构建项目:
D:\workspaces> cd shared-resources-project
D:\workspaces\java\shared-resources-project> mvn clean install
一些注意事项:
config.xml
文件作为资源添加到Project-X。config.xml
复制到项目输出目录(默认情况下为target
目录)。在复制期间,MRP还将使用Project-X提供的特定值替换${custom-value}
。config.xml
阶段,最终generate-source
将可用于另一个插件,并且在build-helper-maven-plugin
声明之后出现其声明。 Maven(至少3.0.4+)按照pom.xml
中的幻影顺序调用插件。