Maven为同一项目生成两个不同的WAR文件 [英] Maven generating two different WAR files for same project

查看:435
本文介绍了Maven为同一项目生成两个不同的WAR文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由两个不同的服务类testClass1和testClass2组成的Maven Web服务项目.我要为同一项目的两个类使用两个不同的WAR文件.目前,我正在为这两种服务生成单个WAR文件.

I have a maven web-service project consisting of two different service classes testClass1 and testClass2 . I want to have two different WAR files for both classes of same project. Presently I am generating single WAR file for both services .

如何为同一项目生成两个不同的WAR文件? 提前致谢.

How can I generate two different WAR file for same project ? Thanks In Advance.

推荐答案

一般注意事项

按照每个文档所述,您尝试实现的是糟糕的实践:

General Considerations

As per doc says, what you try to achieve is a bad pratice :

从单个源目录中产生多个唯一的JAR

许多抱怨不能将其源代码分散到多个源目录中的人似乎抱怨不希望分散任何内容,而是使用包含和排除从单个目录中生成了多个独特的工件.

Producing Multiple Unique JARs from a Single Source Directory

As many people that complain about not being able to spread out their sources into multiple source directories seem to complain about not wanting to spread anything out, producing several unique artifacts from a single directory using includes and excludes.

这种做法可能会造成混淆和风险.

This practice can be confusing and risky.

您可能最终会构建两个包含相同类的JAR,这表明应该将通用功能抽象为单独的依赖项. 您可能最终在未意识到的两个JAR之间引入了依赖关系,并且通常是循环依赖关系.这表明这些类使用了错误的JAR,或者也许所有内容都应该只是一个JAR.

You may end up building two JARs that include the same classes - this indicates that the common functionality should have been abstracted into a separate dependency. You may end up introducing a dependency between the two JARs that you didn't realise, and often a circular dependency. This indicates that the classes are in the wrong JAR, or perhaps that everything should just be a single JAR.

最适合专家的解决方案

您应该考虑将您的项目分成两个不同的项目,每个项目一个pom,因此每个项目都产生一种伪像,这是一场战争.

Solutions that fit the maven best pratices

You should consider splitting you project into two differents ones, having each one a pom, and so each one generating an artifact, here a war.

您可以通过使用两个简单的项目来实现:

You may achieve that by using either two simple projects :

service1-simple-webproject
|-- src
`-- pom.xml

service2-simple-webproject
|-- src
`-- pom.xml

这很简单,但是您可能无法轻松共享它们之间的属性/依赖关系/关系.

This is quite simple, but you may not be able tho share easily properties / dependencies / relations between them.

只需将您自己的代码拆分到单独的目录中,然后尝试使其作为两个独立的项目工作即可.

Just split your own code into separates directory, and try to make it work as two independant projects.

如果您认为它们之间有一些代码可以共享(实用程序,配置等),请参阅关于多模块的下一部分.

If you think that there is some code to share between them (utilities, configuration, ...), see next section about multi module.

或者您可以使用适当的多模块层次结构:

Or you can use an appopriate multi-module hierarchy :

parent-multimodule-project
|-- service1-simple-webmodule
|   |-- src/
|   `-- pom.xml
|-- service2-simple-webmodule
|   |-- src
|   `-- pom.xml
`-- pom.xml

这将使您能够在项目之间建立关系,在更高级别上共享配置,... 看一下这个文档:

This will allow you to have relations between projects, share configuration at a higher level, ... Ta ke a look to this documentation :

  • Maven Official doc : http://maven.apache.org/guides/mini/guide-multiple-modules.html
  • Sonatype fantastic doc : http://www.sonatype.com/books/mvnex-book/reference/multimodule.html

我可能会带领您创建第三个项目,可能命名为core或util,这将产生一个jar,其中包含两个Web项目的通用类.

I will probably lead you to create a third project, probably named core, or util, that would produce a jar which will contains common classes to both web project.

这确实是最好的方法!

我只给您一些信息,因为我什至可以确定在这种情况下它可以工作.

I would only give you some information, because I'm even sure it could work in this case.

Maven Assembly Plugin允许您基于xml描述文件生成不同格式的软件包(zip,jar,war,其他……). 此处的文档: http://maven.apache.org/plugins/maven-assembly-plugin /

Maven Assembly Plugin allow you to generate different format of package (zip, jar, war, other, ...) based on xml description files. Here documentation : http://maven.apache.org/plugins/maven-assembly-plugin/

再一次,我强烈建议您不要使用这种解决方法.

Once again, I strongly advise you to NOT use this kind of workaround.

这篇关于Maven为同一项目生成两个不同的WAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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