gpt4 book ai didi

java - 如何让maven中的资源目录正常工作?

转载 作者:行者123 更新时间:2023-12-02 12:10:23 26 4
gpt4 key购买 nike

我有一个正在尝试清理的 Java Maven Web 应用程序项目。在 <build>我的部分pom.xml ,我有以下内容:

<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<filters>
<filter>profiles/${build.profile.id}.profile.properties</filter>
</filters>
[...other properties...]
</build>

在我的项目中,在我的 Mac 上是 /Users/anthony/workspace/my-project/ ,我src/main/resources/profiles/我有local.profile.propertiesqa.profile.properties .

然后,在我的 pom 的 Maven 配置文件中,我定义 ${build.profile.id}如下:

<profile>
<id>local</id>
[...stuff...]
<properties>
<build.profile.id>local</build.profile.id> <!-- or qa -->
[...stuff...]
</properties>
</properties>

当我在控制台中运行 $ mvn clean install -Plocal 时,我收到以下错误:

Failed to execute goal ... on project my-project: Error loading property file '/Users/anthony/workspace/my-project/profiles/local.profile.properties' .

Maven 似乎无法识别我的过滤配置文件属性文件的资源目录。仅当我明确放置属性文件的整个路径时,这才有效,如下所示:

<filters>
<filter>src/main/resources/profiles/${build.profile.id}.profile.properties</filter>
</filters>

有没有办法让我不必明确声明 src/main/resources ?我认为我声明资源目录的目的是我可以使用它,特别是声明过滤。

最佳答案

资源目录仅对正在构建的 Java Artifact 具有“资源”的含义,但对 Maven 本身没有意义。对于 Maven,“资源”目录具有特殊含义,因为 Maven 知道将这些文件复制到生成的 jar 文件中的何处。但是对于处理文件或过滤文件的 Maven,您必须告诉 Maven 确切的路径,因为 Maven 不知道过滤的文件是资源、源文件还是其他文件。另外,您可能定义了多个源或资源目录,而 Maven 不知道要在哪一个目录中进行过滤。因此,您始终需要指定 Maven 的完整路径。

所以,简而言之:

Is there a way for me to not have to explicitly state src/main/resources?

没有。

关于java - 如何让maven中的资源目录正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46589902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com