我怎样才能让maven中的资源目录工作?

How can I get the resource directory in maven to work?

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

<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 工件有 "resources" 的意义,但对 Maven 本身没有意义。对于 Maven,"resources" 目录具有特殊意义,因为 Maven 知道将这些文件复制到生成的 jar 文件中的何处。但是对于 Maven 处理文件或过滤文件,您必须告诉 Maven 确切的路径,因为 Maven 不知道过滤的文件是资源、源文件还是其他东西。此外,您可以定义多个源目录或资源目录,而 Maven 不知道要在哪一个目录中进行过滤。因此,您始终需要指定 Maven 的完整路径。

所以,简而言之:

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

没有