【问题标题】:Maven build is not filtering properties in IntellijMaven 构建未过滤 Intellij 中的属性
【发布时间】:2016-03-24 10:50:15
【问题描述】:

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

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

【问题讨论】:

  • 嗯,这很有趣(也很奇怪)。您能否发布更多关于您的 Intellij 版本、操作系统以及最重要的是您在 iJ 中使用的运行配置(屏幕截图?)的信息?我从来没有遇到过这个问题。
  • 您是否告诉 Intellij 将构建操作委托给 mvn?

标签: maven intellij-idea properties filtering maven-resources-plugin


【解决方案1】:

修复

tldr:我能够重现您的问题,然后通过将插件配置中的 &lt;resources&gt; 元素移出直接在 &lt;build&gt; 下进行修复,如下所示:

<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 的配置不起作用,因此查看了 maven-resources-plugin documentationresources mojo 的示例,它似乎直接在 &lt;build&gt; 下指定了 &lt;resources&gt;

Usage 文档中的措辞似乎暗示 &lt;resources&gt; 配置仅适用于 copy-resources mojo 的插件配置:

更新

应该从 maven-resources-plugin introduction 开始,其中明确指出:

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

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



Intellij 的古怪之处?

我很想建议 Intellij 是/没有过错。

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

您的 Intellij 是否使用了与命令行使用的 mvn 不同的捆绑 mvn?即在这里和从命令行看到的maven是一样的吗?这是我能想到的唯一件事,除了 Intellij 的 maven 集成中的一个错误(不太可能)可能会导致您看到的不同行为。

【讨论】:

    【解决方案2】:

    这是我的解决方案。

    转到运行>编辑配置。

    在“服务器”选项卡中>在启动之前。

    删除工件并添加此 maven 目标:clean compile

    【讨论】:

    • '干净编译'就足够了
    【解决方案3】:

    尝试在&lt;directory&gt;标签的开头添加${pom.basedir}

    来自

    <build>
        (...)
        <testResources>
            <testResource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
        </testResource>
        (...)
    </build>
    

    <build>
        (...)
        <testResources>
            <testResource>
                <filtering>true</filtering>
                <directory>${pom.basedir}/src/test/resources</directory>
            </testResource>
        </testResources>
        (...)
    </build>
    

    我怀疑当 Maven 项目有多个模块时,Intellij 需要找到正确的资源文件来执行 pom.xml 属性的替换。

    【讨论】:

      【解决方案4】:

      对我来说,问题是我忘记配置 Intellij 以将构建委托给 mvn。

      更多详细信息请参见此处的文档:https://www.jetbrains.com/help/idea/delegate-build-and-run-actions-to-maven.html#delegate_to_maven

      我怀疑 Intellij 仅将 src/main/resources 复制到目标/类,没有别的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-29
        • 2012-01-13
        • 2013-03-01
        • 2014-07-31
        • 2019-03-02
        • 2015-10-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多