Gradle多项目构建:如何将WAR项目的JAR包含到另一个WAR中而不创建伪项目 [英] Gradle Multiproject Build: How to include JAR of a WAR project into another WAR without creating pseudo projects
问题描述
我有两个Web应用程序 A
和 B
。 B
使用来自 A
的部分代码。
如果我声明 A
作为 B
A.war
包含 WEB-INF / lib
B.war
这种包含对容器来说是没有意义的。
我看到的唯一解决方案是将 A
更改为Java项目(JAR)并创建一个虚拟web项目(WAR)称为 A_WEB
。然后将 A
声明为 A_WEB
和 B
然而这需要我干预项目结构。选择gradle的一个重要原因是,它承诺不会像maven那样使该工具适合该工具。所以我希望有一个非混乱的方式。
你如何声明依赖关系?我假设你有一个多项目的子项目A和B,都使用War插件。我使用Gradle 2.4做了一个实验,如果我这样声明 B / build.gradle
:
apply plugin:'war'
dependencies {
compile project(':A')
}
然后B.war包含 WEB-INF / lib / A.jar
。如果您正确遵循Gradle War插件的约定(在 A / src / main / webapp /
中放置网络资源并在 A / src中放置与代码相关的资源/ main / resources /
),那么 A.jar
应该包含您想要的内容。
<另外,如果你真的需要破解一些东西,你可以有选择地复制A:
apply plugin:'war '
war.dependsOn(':A:war')
war {
from project(':A')。sourceSets.main.output.classesDir
}
classesDir 是 java.io.File $ c $的一个实例因此您可以按照 Gradle关于使用文件的教程中所述过滤文件 。
然而,如果你写 B
使用部分o f A
,这意味着您的 A
实际上包含一个子项目 C
这是由 A
和 B
共享的代码。你只是不接受这个事实。你试图做的是从任何人手中破解和隐藏对 C
的依赖关系 - 而不是通过提取 C
作为一个单独的项目,设置适当的项目间依赖关系,并让Gradle默认行为负责正确构建所有项目。我会说什么是混乱的是试图做任何事情,以防止重新组织文件在磁盘上,即使模块间依赖关系建议你应该提取一个公共部分。
I have two web apps A
and B
. B
uses parts of code from A
.
If I declare A
as a dependency for B
the A.war
is included WEB-INF/lib
of B.war
This kind of include is meaningless to the container.
The only solution I see is to change A
to a Java project (JAR) and create a dummy web project (WAR) called A_WEB
. Then declare A
as dependency for A_WEB
and B
However this will require me to meddle with the project structure. A big reason for choosing gradle was that it promised not to make the project adapat to the tool like maven does. So I hope there is a non messy way out of this.
How have you declared the dependency? I assume you have a multi-project build with subprojects A and B, both using the War plugin. I made an experiment using Gradle 2.4 and if I declare B/build.gradle
like this:
apply plugin: 'war'
dependencies {
compile project(':A')
}
then B.war contains WEB-INF/lib/A.jar
. If you correctly follow conventions of Gradle War plugin (place web resources in A/src/main/webapp/
and code-related resources in A/src/main/resources/
), then A.jar
should contain what you want.
Alternatively, if you really need to hack something around, you can selectively copy things from A:
apply plugin: 'war'
war.dependsOn(':A:war')
war {
from project(':A').sourceSets.main.output.classesDir
}
classesDir is an instance of java.io.File
, so you may filter files as described in Gradle's tutorial on working with files.
However, if you write that "B
uses parts of code from A
", this means your A
actually contains a sub-project C
which is the code shared both by A
and B
. You just don't accept the fact. What you are trying to do is to hack around and hide the dependency on C
from anyone - instead of making it clear by extracting C
as a separate project, setting up proper inter-project dependencies and letting Gradle default behaviour take care of building all properly. I'd say that what's messy is trying to do anything to prevent re-organizing files on disk even if inter-module dependencies suggest you should extract a common part.
这篇关于Gradle多项目构建:如何将WAR项目的JAR包含到另一个WAR中而不创建伪项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!