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

xzlaal3s  于 2024-01-05  发布在  Maven
关注(0)|答案(5)|浏览(225)

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

  1. <properties>
  2. <prop1>aaa</prop1>
  3. <prop2>bbb</prop2>
  4. </properties>
  5. ...
  6. <plugin>
  7. <groupId>org.apache.maven.plugins</groupId>
  8. <artifactId>maven-resources-plugin</artifactId>
  9. <version>2.7</version>
  10. <configuration>
  11. <resources>
  12. <resource>
  13. <directory>src/main/resources</directory>
  14. <includes>
  15. <include>file1</include>
  16. <include>file2</include>
  17. </includes>
  18. <filtering>true</filtering>
  19. </resource>
  20. </resources>
  21. </configuration>
  22. <executions>
  23. <execution>
  24. <phase>compile</phase>
  25. <goals>
  26. <goal>resources</goal>
  27. </goals>
  28. </execution>
  29. </executions>
  30. </plugin>

字符串

goqiplq2

goqiplq21#

修复

tldr:我能够重现你的问题,然后通过将<resources>元素从插件配置中移到<build>下面来修复它,如下所示:

  1. <build>
  2. <resources>
  3. <resource>
  4. <filtering>true</filtering>
  5. <directory>${basedir}/src/main/resources</directory>
  6. <includes>
  7. <include>*</include>
  8. </includes>
  9. </resource>
  10. </resources>
  11. <plugins>
  12. <plugin>
  13. <groupId>org.apache.maven.plugins</groupId>
  14. <artifactId>maven-resources-plugin</artifactId>
  15. <version>2.7</version>
  16. <executions>
  17. <execution>
  18. <phase>compile</phase>
  19. <goals>
  20. <goal>resources</goal>
  21. </goals>
  22. </execution>
  23. </executions>
  24. </plugin>
  25. <!-- <snip> Other plugins -->
  26. </plugins>
  27. </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>标记的开头:

  1. <build>
  2. (...)
  3. <testResources>
  4. <testResource>
  5. <filtering>true</filtering>
  6. <directory>src/main/resources</directory>
  7. </testResource>
  8. (...)
  9. </build>

字符串

  1. <build>
  2. (...)
  3. <testResources>
  4. <testResource>
  5. <filtering>true</filtering>
  6. <directory>${pom.basedir}/src/test/resources</directory>
  7. </testResource>
  8. </testResources>
  9. (...)
  10. </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#

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

字符串
enter image description here

相关问题