Gradle:将多个源集合组合成一个jar [英] Gradle: assembling multiple source-sets into one jar
问题描述
我在这里问了一个相关的问题 JOOQ class generation and gradle
p>在这个问题中,我试图找到最好的方法来完成多阶段构建,包括在中间阶段生成类。我放弃了方案二的方法,现在发现自己陷入了僵局。
我有以下build.gradle文件
apply plugin: 'java'
apply plugin:'eclipse'
sourceSets
{
bootstrap
generated {
compileClasspath + = bootstrap .output
}
main {
compileClasspath + = bootstrap.output
compileClasspath + = generated.output
}
}
buildscript {
repositories {
mavenCentral()
}
依赖关系{
classpath'org.jooq:jooq-codegen:3.5 .0'
classpath'postgresql:postgresql:9.1-901.jdbc4'
classpath project(:)
}
}
依赖关系
{
compile'org.jooq:jooq:3.5.0'
compile'org.jooq:jooq-codegen:3.5.0'
compile'org.apache.poi: poi:3.10.1'
compile'com.google.guava:guava:18.0'
generatedcompile'org.jooq:jooq:3.5.0'
generatedcompile'org.jooq:jooq-codegen:3.5.0'
generatedcompile'org.apache.poi:poi:3.10.1'
generatedcompile'com.google.guava:guava:18.0'
bootstrapCompile'org.jooq:jooq:3.5.0'
bootstrapCompile'org.jooq:jooq-codegen:3.5.0'
bootstrapCompile'org.apache.poi:poi :3.10.1'
bootstrapCompile'com.google.guava:guava:18.0'
}
任务生成<< {
//使用JOOQ生成类,输出进入生成的源文件
.withDirectory(file(src / generated / java)。getAbsolutePath())
}
generatedClasses
{
dependsOn bootstrapClasses
dependsOn generate
}
jar
{
dependsOn generatedClasses
dependsOn bootstrapClasses
}
结构是:
- bootstrap 源代码集包含代码生成所需的一些核心Java类,以及一个将用于伪装database
- generate 任务使用 boostrap 中的类和sql文件来生成类
- 生成的源集包含生成任务的输出,
- 主要源集包含可能被称为正常类(即使用的类)由引导程序和生成的类描述)
我有一些问题,我无法解决:
- 我似乎必须复制每个源集的依赖关系
- 当jar文件获取它只包含从 main 源集合生成的类
我应该注意到构建为它站在上面将成功生成每个源集。
任何帮助将不胜感激。
解决方案O.K。我想我已经找到了这个问题的答案。有两个部分....
第一个问题,必须多次指定相同的依赖项,通过添加这个来解决:
配置{
generatedcompile {
extendsFrom编译
}
bootstrapCompile {
extendsFrom compile $ b $第二个问题是,jar文件中没有所有的构建文物,通过将jar任务更改为来自sourceSets.generated.output $ b的
jar
{
$ b from sourceSets.bootstrap.output
dependsOn bootstrapClasses
dependsOn generatedClasses
}
I've asked a related question here JOOQ class generation and gradle
In that question I'm trying to find the best way to do a multi-stage build including generating classes in a middle step. I've gone down the Option Two approach, and now find myself an impasse.
I have the following build.gradle file
apply plugin: 'java'
apply plugin: 'eclipse'
sourceSets
{
bootstrap
generated {
compileClasspath += bootstrap.output
}
main {
compileClasspath += bootstrap.output
compileClasspath += generated.output
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.jooq:jooq-codegen:3.5.0'
classpath 'postgresql:postgresql:9.1-901.jdbc4'
classpath project(":")
}
}
dependencies
{
compile 'org.jooq:jooq:3.5.0'
compile 'org.jooq:jooq-codegen:3.5.0'
compile 'org.apache.poi:poi:3.10.1'
compile 'com.google.guava:guava:18.0'
generatedCompile 'org.jooq:jooq:3.5.0'
generatedCompile 'org.jooq:jooq-codegen:3.5.0'
generatedCompile 'org.apache.poi:poi:3.10.1'
generatedCompile 'com.google.guava:guava:18.0'
bootstrapCompile 'org.jooq:jooq:3.5.0'
bootstrapCompile 'org.jooq:jooq-codegen:3.5.0'
bootstrapCompile 'org.apache.poi:poi:3.10.1'
bootstrapCompile 'com.google.guava:guava:18.0'
}
task generate << {
//Use JOOQ to generate classes, with the output going into the generated sourceSet
.withDirectory(file("src/generated/java").getAbsolutePath())
}
generatedClasses
{
dependsOn bootstrapClasses
dependsOn generate
}
jar
{
dependsOn generatedClasses
dependsOn bootstrapClasses
}
The structure is that
- the bootstrap source set holds some core java classes that are required for the code generation, plus an sql file that will be used to pouplate a database
- The generate task uses the classes and sql file in boostrap to generate classes
- The generated source set holds the outputs of the generation task, and
- The main source set holds what might be called the "normal" classes (i.e. the ones that make use of the database being described by the bootstrap and generated classes)
I have a couple of problems, which I can't untangle:
- I seem to have to duplicate the dependencies for each source-set
- When the jar file gets built, it only contains the classes generated from the main source set
I should note that the build as it stands above will successfully generate each of the source-sets.
Any help would be greatly appreciated.
解决方案 O.K. I think I have found the answer to this question. There were two parts....
The first problem, having to specify the same dependencies multiple times, was fixed by adding this:
configurations {
generatedCompile {
extendsFrom compile
}
bootstrapCompile {
extendsFrom compile
}
}
The second problem, the jar file not having all the build artefacts, was fixed by changing the jar task to
jar
{
from sourceSets.generated.output
from sourceSets.bootstrap.output
dependsOn bootstrapClasses
dependsOn generatedClasses
}
这篇关于Gradle:将多个源集合组合成一个jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!