Maven 构建未过滤 Intellij 中的属性

2024-01-07

我遇到一个问题,当我从 Intellij 15.0.2 运行 Maven 构建时,Maven 资源插件不会将我的属性过滤到我的文件中。当我跑步时它确实有效mvn compile从 Windows 命令行。我的插件配置是:

<properties>
    <prop1>aaa</prop1>
    <prop2>bbb</prop2>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>file1</include>
                <include>file2</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</configuration>
<executions>
    <execution>
        <phase>compile</phase>
        <goals>
            <goal>resources</goal>
        </goals>
    </execution>
</executions>
</plugin>

The fix

tldr https://en.wikipedia.org/wiki/Wikipedia:Too_long;_didn%27t_read:我能够重现您的问题,然后通过移出来解决它<resources>元素从插件配置直接到下面<build>像这样:

<build>
    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- <snip> Other plugins -->
    </plugins>
</build>

未来的读者,如果您只对修复感兴趣,请不要继续阅读。对于勇敢的 SO-er,血淋淋的细节在下面等待着!

我为什么这么做?

我执行了上述操作,因为这就是我在之前的项目中打开资源过滤的方式。我不需要更改默认阶段(process-resources),因此不需要明确指定maven-resources-plugin根本不。然而,我很好奇为什么 OP 的配置不起作用,因此查看了示例resourcesmaven-resources-plugin 中的 mojo文档 https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html这似乎有<resources>直接指定在<build>.

中的措辞Usage https://maven.apache.org/plugins/maven-resources-plugin/usage.html文档似乎暗示<resources>仅需要在插件配置下进行配置copy-resources mojo:

Update

应该从 maven-resources-plugin 开始介绍 https://maven.apache.org/plugins/maven-resources-plugin/index.html其中明确指出:

resources:resources 将主源代码的资源复制到 主输出目录。

这个目标通常会自动执行,因为它受到 默认为进程资源生命周期阶段。它总是使用 project.build.resources 元素来指定资源,并由 默认使用project.build.outputDirectory来指定副本 目的地。



Intellij 的奇怪之处?

我很想认为 Intellij 没有错。

使用 Intellij 15.0.2,执行时过滤行为(即是否有效)是相同的mvn clean compile从 Intellij 或从命令行。我本以为问题出在插件/pom 配置中,而不是 Intellij 本身,除非 Intellij 的 maven 集成中存在错误。不管怎样,我在 Intellij 中使用 maven 时还没有遇到这个问题(从版本 12.x 开始使用它已经有一段时间了)。

您的 Intellij 使用的捆绑 mvn 是否与命令行使用的 mvn 不同?即,在这里看到的 Maven 和从命令行看到的 Maven 是否相同?那就是only我能想到的,除了 Intellij 的 Maven 集成中的一个错误(不太可能)之外,它可能会解释您所看到的不同行为。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Maven 构建未过滤 Intellij 中的属性 的相关文章

随机推荐