我在这里问了一个相关问题JOOQ class generation and gradle
在那个问题中,我试图找到进行多阶段构建的最佳方法,包括在中间步骤中生成类。我已经采用了选项二的方法,现在发现自己陷入了僵局。
我有以下 build.gradle 文件
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
}
结构是这样的
- bootstrap 源代码集包含代码生成所需的一些核心 java 类,以及将用于填充数据库的 sql 文件
- generate任务使用boostrap中的类和sql文件生成类
- generated 源集包含生成任务的输出,并且
- main 源代码集包含所谓的“普通”类(即使用 Bootstrap 和生成类描述的数据库的类)
我有几个问题,我无法解决:
- 我似乎必须为每个源集复制依赖项
- 构建 jar 文件时,它只包含从主要源集生成的类
我应该注意到上面的构建将成功生成每个源集。
如有任何帮助,我们将不胜感激。
最佳答案
好的。我想我已经找到了这个问题的答案。有两个部分....
第一个问题,必须多次指定相同的依赖项,已通过添加此修复:
configurations {
generatedCompile {
extendsFrom compile
}
bootstrapCompile {
extendsFrom compile
}
}
第二个问题,jar 文件不包含所有构建工件,已通过将 jar 任务更改为修复
jar
{
from sourceSets.generated.output
from sourceSets.bootstrap.output
dependsOn bootstrapClasses
dependsOn generatedClasses
}
关于java - Gradle:将多个源集组装到一个 jar 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27263662/