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

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

问题描述

我正在尝试构建一个将 xml 文件作为资源的 jar.我想对该 xml 应用过滤器以将依赖项的名称插入 xml.过滤工作正常,因为我能够放入 ${project.build.finalName} 并替换它.我发现 一个提示 我正在寻找的属性可能是

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>

<fileName>OtherLibrary</fileName>

这可能吗?

xml,在 src/main/resources:

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>

推荐答案

妈的,你是对的,这个属性在资源过滤的时候没有被替换.这很奇怪,听起来像 Maven 资源插件 中的错误,因为这个属性是正确的在 process-resources 阶段进行插值,正如我将在下面建议的解决方法中演示的那样(基于 maven-antrun-plugin 和 replace 任务).

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).

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

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>

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

Then, update your XML file into:

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

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

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

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

这证明该属性是插值的(但在 Maven 资源过滤期间未设置)1.如果你需要过滤多个文件,replace 任务可以采用文件集.调整它以满足您的需求.

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 其实在 Maven 2.x 资源插件. 我已经创建了 MRESOURCES-118.

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

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