简体   繁体   English

将Maven配置文件与WAR包装产品一起使用

[英]Using Maven Profiles with a WAR-Packaged Product

We have a Maven build (version 2.2.1) that currently produces a WAR file. 我们有一个Maven版本(版本2.2.1),它当前生成一个WAR文件。 Our output directory is target/ , so we end up with a build artifact target/MyWar.WAR . 我们的输出目录是target/ ,因此最终得到一个构建工件target/MyWar.WAR

I'm adding two profiles to our POM.xml file to facilitate specific build "flavors" that each require a specific version of an A.xml file. 我将两个配置文件添加到我们的POM.xml文件中,以便于特定的构建“风格”,每个都需要特定版本的A.xml文件。 In the intermediate build directory target/MyWar/ there are 3 files: 在中间构建目录target/MyWar/有3个文件:

A.xml
A_1.xml
A_2.xml

Building in Maven without a specified profile should use A.xml, and it does currently. 在没有指定配置文件的情况下在Maven中构建应使用A.xml,并且它当前正在使用。 I want to use maven-antrun-plugin to (for Profile 1) replace A.xml with A_1.xml, and for Profile 2 replace A.xml with A_2.xml. 我想使用maven-antrun-plugin (对于Profile 1)用A_1.xml替换A.xml,对于Profile 2用A_2.xml替换A.xml。 (Removing the _1 and _2 suffixes.) (删除_1和_2后缀。)

This is an example of Profile 1's execution: 这是Profile 1执行的一个例子:

<profile>
<id>Profile1</id>
<build>
...
    <execution>
        <id>Profile1-Replace</id>
        <phase>package</phase>
        <goals>
            <goal>run</goal>
        </goals>
        <configuration>
            <tasks>
                <delete
                    file="${project.build.outputDirectory}/../MyWar/A.xml" />
                <copy
                    file="${project.build.outputDirectory}/../MyWar/A_1.xml"
                    tofile="${project.build.outputDirectory}/../MyWar/A.xml" />
            </tasks>
        </configuration>
    </execution>
...
</build>
</profile>

This correctly replaces the files in the intermediate target/MyWar/ directory, but for whatever reason the final WAR that's being produced in target does not reflect these changes. 这将正确替换中间目标/ MyWar /目录中的文件,但是无论出于何种原因, target中生成的最终WAR都无法反映这些更改。

It's as if running in the 'package' phase is too late, and the WAR has already been built. 好像在“打包”阶段中运行为时已晚,并且已经构建了WAR。 Changing the phase to 'compile' or 'test', the immediately-previous phases, complain because the A.xml file (and the intermediate build directory) have not even been created yet. 将阶段更改为“编译”或“测试”,即之前的阶段,抱怨因为尚未创建A.xml文件(和中间构建目录)。

I would suggest to use the process-resources phase instead, or even generate-resources , if you feel that is a better fit. 如果您觉得更合适,我建议使用process-resources阶段,甚至使用generate-resources As a last resort, use prepare-package . 作为最后的手段,请使用prepare-package But the package phase is the wrong place to do this sort of thing. 但是包装阶段是做这种事情的错误地方。 All such modifications might typically occur directly in the source tree. 所有这些修改通常可能直接在源树中发生。

However, if you do the file manipulation in a separate directory, then you can add it during the package phase using the maven-war-plugin as follows: 但是,如果您在单独的目录中进行文件操作,则可以在打包阶段使用maven-war-plugin将其添加,如下所示:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
      <webResources>
        <resource>
          <directory>A_variant</directory>
        </resource>
      </webResources>
    </configuration>
  </plugin>

Of course if you need to go this route, it would be simpler to keep three directories and choose the appropriate one in your profile. 当然,如果你需要走这条路线,保留三个目录并在你的个人资料中选择合适的目录会更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM