2017-10-05 122 views
0

我有一个Java Maven web应用程序项目,我正在尝试清理。在我pom.xml<build>部分,我有以下几点:如何获取maven中的资源目录以便使用?

<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:当我明确地把我的财产的整个文件的路径,像这样这仅适用?我认为我声明资源目录的重点是我可以使用它,特别是用于声明过滤。

回答

1

资源目录对于正在构建的Java工件仅具有“资源”的含义,但对于Maven本身而言仅具有“资源”的含义。对于Maven而言,“资源”目录具有特殊含义,因为Maven知道将这些文件复制到生成的jar文件中的位置。但是对于使用文件或过滤文件的Maven,您必须告诉Maven确切的路径,因为Maven不知道过滤的文件是资源,源文件还是其他内容。另外,您可以定义多个源或资源目录,而Maven不知道要在哪个目录中进行过滤。因此,您总是需要指定Maven的完整路径。

因此,简而言之:

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

相关问题