如何提供除工件之外的属性文件?

时间:2009-08-28 09:20:19

标签: maven-2 build-process build-automation

我正在使用 Maven2 来构建WAR项目。 某些属性文件取决于发布的目标环境。

除了WAR之外,我想传递一个名为datasource.xml的文件。此文件已存在于我的项目目录中,但包含将在构建期间过滤的属性(即某些${foo.bar})。

换句话说,在运行命令 mvn clean install 之后,我想在target/目录中看到两个文件,my-webapp.wardatasource.xml。< / p>

请注意,datasource.xml 不得包含在my-webapp.war工件中

我该怎么做?

1 个答案:

答案 0 :(得分:10)

您可以使用build-helper-maven-plugin附加其他工件。下面的配置会在阶段将datasource.xml附加为附加工件。如果该工件在src / main / resources和src / main / webapp之外定义,它将不会包含在战争中。

更新:为确保根据您的评论应用资源过滤,您可以指定执行resource-plugin的复制资源目标,指定要应用的过滤。然后,您仍然可以使用build-helper-maven-plugin通过引用相应的目标目录来附加该过滤的工件。我已更新下面的示例以显示此用法。

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.4</version>
  <executions>
    <execution>
      <id>copy-resources</id>
      <phase>validate</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.outputDirectory}/datasource</outputDirectory>
        <resources>          
          <resource>
            <directory>src/main/datasource</directory>
            <filtering>true</filtering>
          </resource>
        </resources>              
      </configuration>            
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>attach-artifacts</id>
      <phase>package</phase>
      <goals>
        <goal>attach-artifact</goal>
      </goals>
      <configuration>
        <artifacts>
          <artifact>
            <file>${project.build.outputDirectory}/datasource/datasource.xml</file>
            <type>xml </type>
            <classifier>datasource</classifier>
          </artifact>
        </artifacts>
      </configuration>
    </execution>
  </executions>
</plugin>

这不会出现在目标文件夹中,但会在战争期间部署/安装到存储库。

可以通过使用分类器“datasource”定义依赖项来引用附加的工件。例如:

<dependency>
  <groupId>my.group.id</groupId>
  <artifactId>my-artifact-id/artifactId>
  <version>1.0.0</version>
  <classifier>datasource</classifier>
  <type>xml</type>
</dependency>

您可以使用依赖项插件的copy goal来检索工件,并将其作为部署过程的一部分放在任何需要的地方。