如何让Maven 2构建2个单独的WAR文件

时间:2009-12-10 16:47:22

标签: java maven-2

执行mvn install时,我希望在目标目录中最终得到2个WAR文件。一个将包含生产 web.xml,另一个将包含 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。

8 个答案:

答案 0 :(得分:16)

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

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

<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

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

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

<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属性来激活一个配置文件或另一个配置文件,例如:

mvn -Denv=uat package

另一种选择是将值放入特定的过滤器中,并在构建时选择正确的过滤器(如this post中所示)。

实际上有很多选择,但正如我所说,我不认为你可以在没有运行两次构建的情况下做到这一点。

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

答案 1 :(得分:6)

您可以告诉Maven Assembly插件只生成两个程序集。您只需为要创建的每个输出编写汇编描述符文件,并在插件配置中列出它们。

例如,我正在使用它来生成WAR文件和TGZ文件,但是没有理由不能以相同的方式执行两个WAR。然后mvn包将生成两个文件。

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/assembly-war.xml</descriptor>
                    <descriptor>src/main/assembly/assembly-dist.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>dist-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

答案 2 :(得分:2)

我通常建议使用配置文件并运行两个专用版本。但是,应该可以使用maven-assembly-plugin创建任意数量的工件。

答案 3 :(得分:0)

我认为这只能通过编写自定义Maven插件或干扰构建生命周期并运行两次战争组装过程来实现(这只是一个微弱的想法)。

也许您可以创建两个配置文件并使用不同的配置文件(mvn -P prof1 packagemvn -P prof2 package)运行两次目标,但要小心生成的工件名称,不应覆盖它们。或者您可以创建一个使用其他插件并组装两个war文件的自定义插件。

答案 4 :(得分:0)

虽然我不使用Maven而是使用Ivy,但以下是您应该如何执行此操作:

将您自己的应用程序发布到私有存储库/类似于JAR及其依赖项/其他静态内容,然后发布单个项目设置,以使用特定于上下文的配置构建应用程序的部署特定WAR。现在,通过构建任何单独的部署项目,您可以获得实际应用程序的最新版本及其特定于构建的配置。

我认为,由于这在Ivy中是微不足道的,Maven应该能够轻松地完成它。

答案 5 :(得分:0)

丑陋的黑客攻击(它破坏了Maven宣称意图而非行动的想法),但对我有用:我必须生成两个共享相同后端代码库的战争,但在MVC控制器包上有所不同

在用几个插件敲打我的头后,我想“嘿,我会在蚂蚁中轻松做到”,这导致我在“包裹”阶段使用<maven-antrun-plugin>来产生战争(我们在这个阶段)已经拥有所有文件)。有点像这样:

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
    <version>1.3</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <tasks>  
            <delete file="target/war-1.war" />
            <delete file="target/war-2.war" />            
            <war destfile="target/war-1.war">
              <fileset dir="target/original">
                <exclude name="**/WEB-INF/classes/package_of_war2/**" />
              </fileset>
            </war>
            <war destfile="target/war-2.war">
              <fileset dir="target/original">
                <exclude name="**/WEB-INF/classes/package_of_war1/**" />
              </fileset>
            </war>
            <delete file="target/original.war" /
          </tasks>
        </configuration>
      </execution>
    </executions>
  </plugin>

(公平地说,我没有删除原始文件,但你应该可以这样做。)

在你的情况下,你可以在没有备用web.xml的情况下打包一场战争,在原始web.xml上重命名/移动它,然后打包第二次战争。

答案 6 :(得分:0)

老问题,但我想回答一下。

您可以使用带有两次执行的war插件在一个构建步骤中非常简单地完成此操作。请参见下面的示例代码:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <executions>
                <execution>
                    <id>build-context-one</id>
                    <phase>install</phase>
                    <goals>
                        <goal>war</goal>
                    </goals>
                    <configuration>
                        <classifier>context-one</classifier>
                        <webResources>
                            <resource>
                                <filtering>true</filtering>
                                <directory>src/main/webapp</directory>
                                <includes>
                                    <include>**</include>
                                </includes>
                            </resource>
                            <resource>
                                <directory>your-context-one-directory</directory>
                            </resource>
                        </webResources>
                    </configuration>
                </execution>
                <execution>
                    <id>build-context-two</id>
                    <phase>install</phase>
                    <goals>
                        <goal>war</goal>
                    </goals>
                    <configuration>
                        <classifier>classifier-two</classifier>
                        <webResources>
                            <resource>
                                <filtering>true</filtering>
                                <directory>src/main/webapp</directory>
                                <includes>
                                    <include>**</include>
                                </includes>
                            </resource>
                            <resource>
                                <directory>your-context-two-directory</directory>
                            </resource>
                        </webResources>
                    </configuration>
                </execution>
            </executions>

答案 7 :(得分:0)

更简单:

只需创建一个多模块项目。

每个模块都有WAR包装:)

从父pom和voila构建!