java 如何让 Maven 2 构建 2 个单独的 WAR 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1882322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get Maven 2 to build 2 separate WAR files
提问by TiGz
When doing a mvn installI want to end up with 2 WAR files in my target directory. One will contain the productionweb.xmland the other will contain the test/uatweb.xml.
执行 a 时,mvn install我希望在目标目录中得到 2 个 WAR 文件。一个将包含生产web.xml,另一个将包含test/uatweb.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。
回答by Pascal Thivent
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.xmlare really different, you could just put your maven-war-plugin configuration in two profiles. Or, better, you could merge them into something like this:
如果你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>
And set the envproperty in two profiles to pick up the right web.xmlat 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.xmlare 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:
如果您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>
Then activate one profile or the other by passing the env property on the command line, e.g.:
然后通过在命令行上传递 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:
有关配置文件/过滤的更多资源:
回答by Ben Hardy
You can tell the Maven Assembly plugin to simply generate two assemblies. You just write an assembly descriptor file for each output you wish to create and list them in the plugin config.
您可以告诉 Maven 程序集插件简单地生成两个程序集。您只需为您希望创建的每个输出编写一个程序集描述符文件,并将它们列在插件配置中。
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. mvn package will then generate both files.
例如,我使用它来生成一个 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>
回答by sfussenegger
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创建任意数量的工件。
回答by Peter
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. See the sample code below:
您可以使用具有两次执行的 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>
回答by Esko
While I don't use Maven but Ivyinstead, 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. Now by building any of the individual deployment projects you get the latest version of your actual application with its build specific configurations.
将您自己的应用程序发布到私有存储库/类似于 JAR 及其依赖项/其他静态内容,然后是用于构建具有上下文特定配置的应用程序部署特定 WAR 的各个项目设置。现在,通过构建任何单独的部署项目,您可以获得具有构建特定配置的实际应用程序的最新版本。
I would assume that since this is trivial in Ivy, Maven should be able to do it just as easily.
我认为由于这在 Ivy 中是微不足道的,Maven 应该能够同样轻松地做到这一点。
回答by chesterbr
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 声明意图而不是动作的想法),但对我有用:我不得不生成两个共享相同后端代码库的War,但在 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). Sort of like this:
在用几个插件敲打我的头后,我想“嘿,我可以在 ant 中轻松完成”,这导致我<maven-antrun-plugin>在“打包”阶段(我们已经拥有所有文件)使用它来生成War。有点像这样:
<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 的情况下打包一场War,将其重命名/移动到原始 web.xml 上,然后打包第二场War。
回答by Tamas Mezei
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 插件或干扰构建生命周期并运行War组装过程两次来实现(这只是一个模糊的想法)。
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. Or you might be able to create a custom plugin that uses other plugins and assembles the two war files.
也许您可以创建两个配置文件并使用不同的配置文件 ( mvn -P prof1 package, mvn -P prof2 package)运行目标两次,但要小心生成的工件名称,它们不应被覆盖。或者您可以创建一个使用其他插件的自定义插件并组合两个 war 文件。

