繁体   English   中英

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

[英]How do I get Maven 2 to build 2 separate WAR files

在执行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。

我不认为您可以一步完成此操作(实际上,我很惊讶 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

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

确实有很多选择,但正如我所说,我认为如果不运行构建两次,您就无法做到这一点。

有关配置文件/过滤的更多资源:

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

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

        <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>

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

老问题,但我想回答完整。

您可以使用具有两次执行的 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>

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

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

虽然我不使用 Maven 而是使用Ivy ,但您通常应该这样做:

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

我认为由于这在 Ivy 中是微不足道的,Maven 应该能够同样轻松地做到这一点。

有点丑陋的 hack(它打破了 Maven 声明意图而不是动作的想法),但对我有用:我必须生成两个共享相同后端代码库的战争,但在 MVC 控制器包上有所不同。

在用几个插件敲打我的头后,我想“嘿,我会在 ant 中轻松完成”,这导致我使用<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 上,然后打包第二场战争。

更简单:

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

每个模块都有 WAR 包装 :)

从父 pom 构建,瞧!

暂无
暂无

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

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