Jim*_*Jim 5 resources dependencies maven-2 filtering
我正在尝试构建一个包含xml文件作为资源的jar.我想对该xml应用过滤器以将依赖项的名称插入到xml中.过滤是有效的,因为我能够进入${project.build.finalName}并取代它.我发现了一个暗示,我正在寻找的房产可能是
${project.dependencies[0].artifactId}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用.我想替换
<fileName>${project.dependencies[0].artifactId}</fileName>
Run Code Online (Sandbox Code Playgroud)
同
<fileName>OtherLibrary</fileName>
Run Code Online (Sandbox Code Playgroud)
那可能吗?
xml,位于src/main/resources中:
<somenode>
<fileName>${project.dependencies[0].artifactId}</fileName>
</somenode>
Run Code Online (Sandbox Code Playgroud)
pom.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.foo</groupId>
<artifactId>Thing</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Thing</name>
<url>http://maven.apache.org</url>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>com.pts</groupId>
<artifactId>OtherLibrary</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
该死的,你是对的,这个属性在过滤资源时不会被取代.这很奇怪,它听起来像是Maven Resources Plugin中的一个错误,因为这个属性在这个process-resources阶段被正确插值,我将在下面建议的解决方法中展示(基于maven-antrun-plugin和replace任务).
首先,将以下内容添加到您的POM:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<tasks>
<echo>${project.dependencies[0].artifactId}</echo><!-- I'm a test -->
<replace file="${project.build.outputDirectory}/myxmlfile.xml"
token="@@@" value="${project.dependencies[0].artifactId}"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
然后,将您的XML文件更新为:
<somenode>
<fileName>@@@</fileName>
</somenode>
Run Code Online (Sandbox Code Playgroud)
通过这些更改,运行mvn process-resources将产生以下结果:
$ cat target/classes/myxmlfile.xml
<somenode>
<fileName>OtherLibrary</fileName>
</somenode>
Run Code Online (Sandbox Code Playgroud)
这证明了属性是内插的(但是在maven过滤资源期间没有设置)1.如果您需要过滤多个文件,则该replace任务可以采用文件集.根据您的需求进行调整.
1 实际上,在Maven 2.x Resources Plugin中为这个bug创建一个新的Jira会很不错.我创造了MRESOURCES-118.