从单个项目源创建多个罐子 [英] build multiple jars from a single project source
问题描述
我想知道是否可以从同一个项目源创建多个jar。我浏览过以前的类似问题,但是我的情况有点不同。我想编译所有的源文件两次,以产生两个不同的罐子,每个都有不同的依赖关系,以及不同的工件ID。从概念上讲,它可能表示如下:
可能与gradle?最好的方法是什么?感谢!
实现你想要的一个简单方法是创建一个多项目构建,包含两个子项目(foobase& amp ; foo2),其中foo2的源代码集被配置为除了包含自己的源代码之外,还包含foobase源代码集。
为了获得工件的不同依赖关系,您只需在子工程中以不同方式声明依赖关系
部分即可。为了测试这个,我在每个子项目中创建了一个包含一个java文件的多项目构建。为了简化此处的输出,根 build.gradle
文件包含所有内容,包括子项目特定的自定义项。然而,在现实生活中,我总是将子项目特定的配置放在正确的子项目级别的 build.gradle
文件中。
gradle构建文件包含
总而言之,我得到了以下结果:
项目结构
build.gradle =>根项目构建文件
settings.gradle =>包含子项目的规格
foo2 \ => foo2子项目文件夹
src\
main\
java\
Foo2.java =>空课
foobase\ => foobase子项目文件夹
src\
main\
java\
FooBase.java =>空课
settings.gradle strong>
include':foobase',':foo2'
build.gradle
<$
apply all plugs:'idea'
apply plugin:'eclipse'
group ='org.foo'
version =' 1.0'
}
子项目{
apply plugin:'java'
apply plugin:'maven'
repositories {
mavenCentral()
}
uploadArchives {
it.repositories.mavenDeployer {
repository(url:file:/// tmp / maven-repo / )
}
}
}
项目(':foobase'){
依赖项{
编译'log4j:log4j:1.2。 13'
}
}
项目(':foo2'){
依赖关系{
编译'log4j:log4j:1.2.16'
}
sourceSets.main.java.srcDirs项目(':foobase')。sourceSets.main.java
sourceSets.main.resources.srcDirs项目(':foobase')。sourceSets.main.resources
sourceSets。 test.java.srcDirs项目(':foobase')。sourceSets.test.java
sourceSets.test.resources.srcDirs项目(':foobase')。sourceSets.test.resources
}
请注意,我还为资源和测试添加了源目录。如果不需要,可以省略最后三行。
验证版本:
在我的例子中:
foobase-1.0。 jar
仅包含 FooBase.class
foo2-1.0.jar code>包含
FooBase.class
和 Foo2.class
foobase-1.0.pom
包含对 log4j-1.2.13
foo2-1.0.pom
包含对 log4j-1.2.16
I'm fairly new to gradle.
I'd like to find out if it is possible to build multiple jars from the same project source. I've browsed previous similar questions, but my situation is a little different. I'd like to compile all the source files twice to produce two different jars, each with different dependencies, and different artifact ids. Conceptually it may be represented as follows:
- project "FooBase"
- src/main/java/...
- depends on library Bar version 1.0.0
- compiles all source and produces "org.foo/Foo1" (group/artifact)
- project "Foo2"
- no source of its own, or may add source
- depends on library Bar version 2.0.0
- compiles all source in "FooBase" and its source, and produces "org.foo/Foo2" (group/artifact)
Is this sort of build possible with gradle? What would be the best approach? Thanks!
One clean way of achieving what you want is to create a multi-project build, with two subprojects (foobase & foo2), where the source sets for foo2 is configured to contain the source sets of foobase in addition to its own sources.
To get different dependencies for the artifacts, you will just need to declare the dependencies
section differently in the subprojects.
To test this, I created a multiproject build with one java file in each subproject. To simplify the output here, the root build.gradle
file contains everything, including subproject specific customizations. In "real life" though, I always put subproject specific configurations in a build.gradle
file at the correct subproject level.
The gradle build file contains
- Adding source sets from foobase to foo2
- Different dependencies in the two projects
- Deployment to a local maven repository (to verify that pom's are created correctly)
- Optional IDE plugins for IntelliJ IDEA and Eclipse
All in all, I ended up with the following:
Project structure
build.gradle => root project build file
settings.gradle => specification of included subprojects
foo2\ => foo2 subproject folder
src\
main\
java\
Foo2.java => Empty class
foobase\ => foobase subproject folder
src\
main\
java\
FooBase.java => Empty class
settings.gradle
include ':foobase', ':foo2'
build.gradle
allprojects {
apply plugin: 'idea'
apply plugin: 'eclipse'
group = 'org.foo'
version = '1.0'
}
subprojects {
apply plugin: 'java'
apply plugin: 'maven'
repositories {
mavenCentral()
}
uploadArchives {
it.repositories.mavenDeployer {
repository(url: "file:///tmp/maven-repo/")
}
}
}
project(':foobase') {
dependencies {
compile 'log4j:log4j:1.2.13'
}
}
project(':foo2') {
dependencies {
compile 'log4j:log4j:1.2.16'
}
sourceSets.main.java.srcDirs project(':foobase').sourceSets.main.java
sourceSets.main.resources.srcDirs project(':foobase').sourceSets.main.resources
sourceSets.test.java.srcDirs project(':foobase').sourceSets.test.java
sourceSets.test.resources.srcDirs project(':foobase').sourceSets.test.resources
}
Note that I added the source directories for resources and tests as well. You may omit the last three lines if that is not required.
To verify the build:
- Make sure that the two jar files contains all the wanted classes.
- Verify that the two deployed maven pom's has the correct dependencies.
In my case:
foobase-1.0.jar
contains onlyFooBase.class
foo2-1.0.jar
contains bothFooBase.class
andFoo2.class
- the uploaded
foobase-1.0.pom
contains a dependency tolog4j-1.2.13
- the uploaded
foo2-1.0.pom
contains a dependency tolog4j-1.2.16
这篇关于从单个项目源创建多个罐子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!