java - 如何指定资源文件夹使用 Maven 的哪个过滤器?

标签 java maven maven-resources-plugin

我有两个配置属性文件和两个应用程序上下文 xml 文件,一个用于测试,另一个用于开发,我必须指定每种情况使用哪个过滤器。

我尝试这样做,但它不起作用,它从 pom.xml 中选择最后一个过滤器,所以当我检查文件夹中的文件 applicationContext.xmltarget/classes/target/test-classes 我发现这两个文件具有从 pom.xml 上声明的最后一个过滤器获取的相同值

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>

        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

    <testResources>
        <testResource>
            <filtering>true</filtering>
            <directory>src/test/resources</directory>
        </testResource>
    </testResources>


    <filters>
        <filter>src/test/filters/filter.properties</filter>
        <filter>src/main/filters/filter.properties</filter>
    </filters>

所以我想知道是否有一个解决方案来为每种情况指定要使用的适当过滤器?

最佳答案

您可以使用profiles .

不幸的是,配置文件内不允许使用过滤器,因此您可以使用以下解决方法:

外部配置文件写道:

<filters>
    <filter>src/${switch}/filters/filter.properties</filter>
</filters>

在主要配置文件中写入:

<properties>
  <switch>main</switch>
</properties>
<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

在测试配置文件中写入:

<properties>
  <switch>test</switch>
</properties>
<testResources>
    <testResource>
        <filtering>true</filtering>
        <directory>src/test/resources</directory>
    </testResource>
</testResources>

最终运行:

mvn install -Pmain

或者:

mvn install -Ptest

关于java - 如何指定资源文件夹使用 Maven 的哪个过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23476452/

相关文章:

java - 使用java apache commons下载文件?

java - B-Tree- 方法 inorder(TreeNode<E> root)

java - 启动并配置 WildFly 服务器,并在 1 个 Maven 运行中使用 wildfly-maven-plugin 部署应用程序

maven-resources-plugin 将资源副本中断到测试类

使用复制资源目标 : 'resources' , 'outputDirectory' 丢失或无效的 maven-resources-plugin 错误

java - 是否可以增加 Java 进程(运行时)的大小?

java - 动态添加 UI 组件

maven - Intellij 试图将 pom.xml 视为 jar 文件

java - 为什么 GWT 开发模式 "restart server"失败?

java - WSDL URL 的 Maven 过滤