如何过滤Maven中的资源,替换依赖关系artifactId? [英] How to filter resource in Maven, replacing with a dependencies artifactId?

查看:170
本文介绍了如何过滤Maven中的资源,替换依赖关系artifactId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个具有xml文件作为资源的jar。我想将一个过滤器应用于xml,以将依赖项的名称插入到xml中。过滤正在工作,因为我可以在 $ {project.build.finalName} 中删除​​,并将其替换。我发现我正在寻找的物业的一个提示可能是

  $ {project.dependencies [0] .artifactId} 
pre>

但似乎不起作用。我正在寻找替换

 < fileName> $ {project.dependencies [0] .artifactId}< / fileName> 

 <文件名> OtherLibrary< /文件名> 

可以吗?



xml,这在src / main / resources中:

 < somenode> 
< fileName> $ {project.dependencies [0] .artifactId}< / fileName>
< / somenode>

pom.xml:

 <?xml version =1.0encoding =UTF-8?> 
< project xsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdxmlns = 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>
<依赖关系>
<依赖关系>
< groupId> com.pts< / groupId>
< artifactId> OtherLibrary< / artifactId>
< version> 1.0-SNAPSHOT< / version>
< scope>已提供< / scope>
< / dependency>
<依赖关系>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 3.8.1< / version>
< scope> test< / scope>
< / dependency>
< / dependencies>
< / project>


解决方案

嗯,你是对的,在资源过滤过程中不得更换。这很奇怪,这听起来像 Maven资源插件中的错误,因为此属性正确在进程资源阶段内插,我将在下面我建议的解决方法中演示(基于maven-antrun-plugin和 替换 任务)。



首先,将以下内容添加到您的POM中:

 插件> 
< artifactId> maven-antrun-plugin< / artifactId>
<执行>
< execution>
< phase> process-resources< / phase>
< configuration>
< tasks>
< echo> $ {project.dependencies [0] .artifactId}< / echo><! - 我是一个测试 - >
< replace file =$ {project.build.outputDirectory} /myxmlfile.xml
token =@@@value =$ {project.dependencies [0] .artifactId}/ >
< / tasks>
< / configuration>
< goals>
< goal>运行< / goal>
< / goals>
< / execution>
< / executions>
< / plugin>

然后,将您的XML文件更新为:

 < somenode> 
< fileName> @@@< / fileName>
< / somenode>

通过这些更改,运行 mvn process-resources 将产生以下结果:

  $ cat target / classes / myxmlfile.xml 
< somenode>
< fileName> OtherLibrary< / fileName>
< / somenode>

哪个证明属性是插值的(但是在资源的maven过滤期间不设置) 1 。如果您需要过滤多个文件, 替换 任务可以采取一个文件集。适应您的需要。



1 实际上,在 Maven 2.x资源插件我创建了 MRESOURCES-118


I'm trying to build a jar that has an xml file as a resource. I'd like to apply a filter to that xml to insert the name of a dependency into the xml. The filtering is working, because I was able to drop in ${project.build.finalName} and get it replaced. I found one hint that the property I'm looking for might be

${project.dependencies[0].artifactId}

but that doesn't seem to work. I'm looking to replace

<fileName>${project.dependencies[0].artifactId}</fileName>

with

<fileName>OtherLibrary</fileName>

Is that possible?

xml, which is in src/main/resources:

<somenode>
  <fileName>${project.dependencies[0].artifactId}</fileName>
</somenode>

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>

解决方案

Damn, you're right, this property doesn't get replaced during the filtering of resources. That's weird and it sounds like a bug in the Maven Resources Plugin because this property is correctly interpolated during the process-resources phase as I'll demonstrate in the workaround I'm suggesting below (based on the maven-antrun-plugin and the replace task).

First, add the following to your 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>

Then, update your XML file into:

<somenode>
  <fileName>@@@</fileName>
</somenode>

With these changes, running mvn process-resources would produce the following result:

$ cat target/classes/myxmlfile.xml 
<somenode>
  <fileName>OtherLibrary</fileName>
</somenode>

Which proves the property is interpolated (but not set during maven filtering of resources)1. And if you need to filter more than one file, the replace task can take a fileset. Adapt it to suit your needs.

1 Actually, it would be nice to create a new Jira for this bug in the Maven 2.x Resources Plugin. I've created MRESOURCES-118.

这篇关于如何过滤Maven中的资源,替换依赖关系artifactId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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