maven 变量属性过滤

标签 maven maven-profiles

我正在尝试使用一些变量属性值并使用 Maven 配置文件来获得正确的输出。我已经为我的 hibernate xml, log4j.properties 做了这个并且没有问题。

所以它在项目#1 中对我有用,我在/src/main/resources 下有一堆文件。我在 Maven 中设置了属性和资源过滤,如下所示:

<properties>
    <log.level>DEBUG</log.level>
</properties>


<profiles>
    <profile>
        <id>production</id>
        <properties>
    <log.level>INFO</log.level>
        </properties>
    </profile>
</profiles>

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

以上工作没有问题。
但是,在我的项目#2 中 - 我有一些具有可变属性的文件,但它们位于/src/main/webapp/WEB-INF 下 - 除了指向 WEB-INF 的目录外,我的操作与上述相同它不起作用。我尝试在项目 #2 上将文件放在/src/main/resources 下,它工作正常。

所以在我看来,当文件位于/src/main/webapp/WEB-INF 下时,资源过滤有问题,但我需要该文件在那里,以便在生成 war 时进入 WEB-INF 文件夹。

有没有人有关于如何做到这一点的指针?

以下是 pom.xml 中不起作用的以下代码片段(资源过滤被完全忽略)
<properties>
        <wsdl.url>http://stage/wsdl-url</wsdl.url>
</properties>

<profiles>
    <profile>
        <id>production</id>
        <properties>
    <wsdl.url>http://prod/wsdl-url</wsdl.url>
        </properties>
    </profile>
</profiles>

<build>
    <resources>
        <resource>
            <directory>src/main/webapp/WEB-INF</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

最佳答案

我也有这个问题;我怀疑主<resources> war 插件忽略了 POM 的部分,因此我想出了直接配置插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <filters>
            <filter>filter.properties</filter>
        </filters>
        <webResources>
            <resource>
                <directory>WebContent/WEB-INF</directory>
                <filtering>true</filtering>
                <targetPath>WEB-INF</targetPath>
            </resource>
        </webResources>
    </configuration>
</plugin>

关于maven 变量属性过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8386759/

相关文章:

maven - 如何将特定的Maven配置文件分配给intellij运行配置?

java - 当我使用 maven-release-plugin 发布分支时,为什么它会尝试从版本 0 创建分支?

java - Jython - 在 Java 中调用 Python 类

maven - Bamboo - SonarQube 多模块 maven 项目跳过子模块

java - 如何在指定最终名称时避免生成默认jar?

java - 如何禁用 Maven 配置文件?

java - Tomcat/Maven 插件 - 未经授权的错误

java - 无法执行目标org.springframework.boot :spring-boot-maven-plugin:2. 1.0.RELEASE

maven - Maven 设置和 POM 中的配置文件名称相同

jsp - 使用 Maven 配置文件在缩小/未压缩的 JS 之间切换?