如何让Maven 2构建2个单独的WAR文件 [英] How do I get Maven 2 to build 2 separate WAR files

查看:143
本文介绍了如何让Maven 2构建2个单独的WAR文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在执行 mvn install 时,我希望在目标目录中最终得到2个WAR文件。一个将包含生产 web.xml ,另一个将包含 test / uat web.xml

When doing a mvn install I want to end up with 2 WAR files in my target directory. One will contain the production web.xml and the other will contain the test/uat web.xml.

我试过这个:

<build>
    <finalName>cas-server</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1-beta-1</version>
            <configuration>
                <webXml>src/main/config/prod/web.xml</webXml>
                <warName>cas-prod</warName>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1-beta-1</version>
            <configuration>
                <webXml>src/main/config/test/web.xml</webXml>
                <warName>cas-test</warName>
            </configuration>
        </plugin>
    </plugins>
</build>

但我最终只得到测试WAR。

But I only end up with the test WAR.

推荐答案

我认为你不能一步到位(实际上,我很惊讶Maven没有抱怨你的设置并且想知道哪一个被应用)和我建议使用配置文件,也许过滤来管理这个用例。

I don't think you can do this in one step (actually, I'm surprised that Maven doesn't complain about your setup and wonder which one is applied) and I'd suggest to use profiles and maybe filtering to manage this use case.

如果你的 web.xml 真的不同,你可以把你的maven-war-plugin配置放在两个配置文件。或者,更好的是,您可以将它们合并为:

If your web.xml are really different, you could just put your maven-war-plugin configuration in two profiles. Or, better, you could merge them into something like this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.1-beta-1</version>
  <configuration>
    <webXml>src/main/config/${env}/web.xml</webXml>
    <warName>cas-test</warName>
  </configuration>
</plugin>

并在两个配置文件中设置 env 属性在构建时选择正确的 web.xml

And set the env property in two profiles to pick up the right web.xml at build time.

<profiles>
  <profile>
    <id>uat</id>
    <properties>
      <env>test</env>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <env>prod</env>
    </properties>
  </profile>
</profiles>

如果您的 web.xml 类似(即,如果只有值不同),您可以在两个配置文件中定义属性及其值,并使用过滤来应用它们。这样的事情:

If your web.xml are similar (i.e. if only values differ in them), you could define properties and their values in two profiles and use filtering to apply them. Something like this:

<profiles>
  <profile>
    <id>env-uat</id>
    <activation>
      <property>
        <name>env</name>
        <value>uat</value>
      </property>
    </activation>
    <properties>
      <key1>uat_value_key_1</key1>
      <keyN>uat_value_key_n</keyN>
    </properties>
  </profile>
  <profile>
    <id>env-prod</id>
    <activation>
      <property>
        <name>env</name>
        <value>prod</value>
      </property>
    </activation>  
    <properties>
      <key1>prod_value_key_1</key1>
      <keyN>prod_value_key_n</keyN>
    </properties>
  </profile>
</profiles>

然后通过在命令行上传递env属性来激活一个配置文件或另一个配置文件,例如:

Then activate one profile or the other by passing the env property on the command line, e.g.:

mvn -Denv=uat package

另一种选择是将值放入特定的过滤器中,并在构建时选择正确的过滤器(例如这篇文章)。

Another option would be to put the values into specific filters and pick up the right one at build time (like in this post).

有很多选择,但正如我所说,我不认为你能做到这一点没有runngin构建两次。

There are really many options but as I said, I don't think you can do this without runngin the build twice.

有关个人资料/过滤的更多资源:

More resources on profiles/filtering:

  • Maven Book: Chapter 11. Build Profiles
  • Maven Book: Chapter 15.3. Resource Filtering
  • Introduction to Build Profiles
  • Use an alternative Maven Profile during test phase
  • maven profile filtering search on Google

这篇关于如何让Maven 2构建2个单独的WAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆