简体   繁体   English

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

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

When doing a mvn install I want to end up with 2 WAR files in my target directory.在执行mvn install我希望在我的目标目录中得到 2 个 WAR 文件。 One will contain the production web.xml and the other will contain the test/uat web.xml .一个将包含生产web.xml ,另一个将包含test/uat web.xml

I've tried this:我试过这个:

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

But I only end up with the test WAR.但我最终只得到了测试 WAR。

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.我不认为您可以一步完成此操作(实际上,我很惊讶 Maven 不会抱怨您的设置并想知道应用了哪一个),我建议使用配置文件和过滤来管理此使用案件。

If your web.xml are really different, you could just put your maven-war-plugin configuration in two profiles.如果您的web.xml真的不同,您可以将您的 maven-war-plugin 配置放在两个配置文件中。 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>

And set the env property in two profiles to pick up the right web.xml at build time.并在两个配置文件中设置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>

If your web.xml are similar (ie if only values differ in them), you could define properties and their values in two profiles and use filtering to apply them.如果您的web.xml相似(即,如果只有值不同),您可以在两个配置文件中定义属性及其值,并使用过滤来应用它们。 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>

Then activate one profile or the other by passing the env property on the command line, eg:然后通过在命令行上传递 env 属性来激活一个或另一个配置文件,例如:

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 ).另一种选择是将值放入特定的过滤器中,并在构建时选择正确的过滤器(如在这篇文章中)。

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:有关配置文件/过滤的更多资源:

You can tell the Maven Assembly plugin to simply generate two assemblies.您可以告诉 Maven 程序集插件简单地生成两个程序集。 You just write an assembly descriptor file for each output you wish to create and list them in the plugin config.您只需为您希望创建的每个输出编写一个程序集描述符文件,并将它们列在插件配置中。

For example I'm using it to generate a WAR file and a TGZ file, but there's no reason you can't do two WARs in the same way.例如,我使用它来生成一个 WAR 文件和一个 TGZ 文件,但是没有理由不能以相同的方式生成两个 WAR。 mvn package will then generate both files. 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>

I'd generally suggest to use profiles and run two dedicated builds.我通常建议使用配置文件并运行两个专用构建。 However, it should be possible to create any number of artifacts using the maven-assembly-plugin .但是,应该可以使用maven-assembly-plugin创建任意数量的工件。

Old Question, but I want to answer for completeness.老问题,但我想回答完整。

You can do this in one build step very simply with the war plugin with two executions.您可以使用具有两次执行的 war 插件在一个构建步骤中非常简单地完成此操作。 See the sample code below:请参阅下面的示例代码:

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

I think this can only be achieved by writing a custom Maven plugin or interfering with the build lifecycle and running the war assembling process twice (this is just a faint idea).我认为这只能通过编写自定义 Maven 插件或干扰构建生命周期并运行两次战争组装过程来实现(这只是一个模糊的想法)。

Maybe you could create two profiles and run the goal twice with different profiles ( mvn -P prof1 package , mvn -P prof2 package ), but be careful with the generated artifact names, they shouldn't be overwritten.也许您可以创建两个配置文件并使用不同的配置文件( mvn -P prof1 packagemvn -P prof2 package )运行目标两次,但要小心生成的工件名称,它们不应该被覆盖。 Or you might be able to create a custom plugin that uses other plugins and assembles the two war files.或者您可以创建一个使用其他插件的自定义插件并组合两个 war 文件。

While I don't use Maven but Ivy instead, here's how you should generally do this:虽然我不使用 Maven 而是使用Ivy ,但您通常应该这样做:

Have your own application published in to a private repository/similar as JAR with its dependencies/other static stuff and then individual project settings for building the application's deployment specific WARs with context specific configurations.将您自己的应用程序发布到私有存储库/类似于 JAR 及其依赖项/其他静态内容,然后是用于构建具有上下文特定配置的应用程序部署特定 WAR 的各个项目设置。 Now by building any of the individual deployment projects you get the latest version of your actual application with its build specific configurations.现在,通过构建任何单独的部署项目,您可以获得具有构建特定配置的实际应用程序的最新版本。

I would assume that since this is trivial in Ivy, Maven should be able to do it just as easily.我认为由于这在 Ivy 中是微不足道的,Maven 应该能够同样轻松地做到这一点。

Sort-of-ugly hack (it breaks Maven's idea of declaring intentions instead of actions), but worked for me: I had to generate two wars which shared the same back-end code base, but varied on the MVC controller packages.有点丑陋的 hack(它打破了 Maven 声明意图而不是动作的想法),但对我有用:我必须生成两个共享相同后端代码库的战争,但在 MVC 控制器包上有所不同。

After banging my head with several plugins, I thought "hey, I'd do it easily in ant", which lead me to use <maven-antrun-plugin> to generate the wars during the "package" phase (on which we arlready have all the files).在用几个插件敲打我的头后,我想“嘿,我会在 ant 中轻松完成”,这导致我使用<maven-antrun-plugin>在“打包”阶段(我们已经拥有所有文件)。 Sort of like this:有点像这样:

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

(to be fair, I did not delete the original file, but you should be able to do so.) (公平地说,我没有删除原始文件,但您应该可以这样做。)

In your case, you could package one war without the alternate web.xml, rename/move it over the original web.xml, then package the second war.在您的情况下,您可以在没有备用 web.xml 的情况下打包一场战争,将其重命名/移动到原始 web.xml 上,然后打包第二场战争。

More simple:更简单:

Just create a multi modules project.只需创建一个多模块项目。

Each module would have the WAR packaging :)每个模块都有 WAR 包装 :)

Build from parent pom and voila !从父 pom 构建,瞧!

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

相关问题 如何从我的 Maven War 构建中排除文件夹/文件? - How do I exclude folders/files from my Maven War build? 如何在一个Maven项目中打包多个WAR文件? - How do I package multiple WAR files in one Maven project? 如何使用yuicompressor-maven-plugin让maven与缩小的文件建立战争 - How to get maven to build a war with minified files using yuicompressor-maven-plugin 如何在Eclipse中使用Maven构建WAR? - How can I build WAR with Maven in Eclipse? Maven用旧文件构建.war - Maven build .war with old files 如何在Maven构建中包括一个随机文件,以将WAR文件部署在AWS EBS上? - How do I include a random file in a Maven build for a WAR file to be deployed on AWS EBS? 如何创建2个单独的战争文件 - how to create 2 separate war files 如何使用maven多模块程序集将war文件放在jar文件的单独子目录中? - How to have a maven multi-module assembly put war files in a separate subdir from jar files? 如何使Maven创建一个适当命名的.war以与Tomcat 7的并行部署功能一起使用? - How do I get Maven to create an appropriately named .war for use with Tomcat 7's parallel deployment feature? 如何让Maven war插件在我的Web应用程序的/ lib中复制完整的jar? - How do I get Maven war plugin copying full jar in /lib of my web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM