Intellij Idea Maven build没有在Intellij中过滤属性

xzlaal3s  于 5个月前  发布在  Maven
关注(0)|答案(5)|浏览(76)

我有一个问题,当我从Intellij 15.0.2运行Maven build时,Maven Resources插件没有将我的属性过滤到我的文件中。当我从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>

字符串

goqiplq2

goqiplq21#

修复

tldr:我能够重现你的问题,然后通过将<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的配置不起作用,因此查看了maven-resources中resources mojo的示例-插件documentation似乎有<resources>直接指定下<build>.
Usage文档中的措辞似乎暗示<resources>配置仅在copy-resources mojo的插件配置下需要:
x1c 0d1x的数据

更新

应该从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集成中有一个bug。对于它的价值,在Intellij中使用maven时,我还没有遇到过这个问题(从12.x版开始使用它已经有一段时间了)。
您的Intellij是否使用了与命令行所使用的mvn不同的捆绑mvn?即,当在这里看到和从命令行看到时,maven是否相同?这是我能想到的 * 唯一 * 的事情,除了Intellij的maven集成中的一个bug(不太可能),可能会解释您所看到的不同行为。


hiz5n14c

hiz5n14c2#

这是我的解决方案。
转到运行>编辑脚本。
在服务器选项卡>启动之前。
删除工件并添加此maven目标:clean compile


的数据

sycxhyv7

sycxhyv73#

尝试将${pom.basedir}添加到<directory>标记的开头:

<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属性的替换是必要的。

bxpogfeg

bxpogfeg4#

对我来说,问题是我忘记配置Intellij将构建委托给mvn。
x1c 0d1x的数据
更多详细信息可在文档中找到:https://www.jetbrains.com/help/idea/delegate-build-and-run-actions-to-maven.html#delegate_to_maven。
我怀疑Intellij只复制src/main/resources到target/classes,其他什么都没有。

cvxl0en2

cvxl0en25#

Go to Run > Edit Configurations.
Modify Options> On "Update" Action> Update resources.

字符串
enter image description here

相关问题