构建一个聚合项目,项目有多个子项目构成,需要对项目中用到构件进行管理。项目选择了gradle而非maven,所以我专门了的研究了一下gradle,参考多篇文章,不保证这些文章内容都是正确的,但确实收到启发和获得到了知识。
我参考的文章列表:
What is the difference between allprojects and subprojects
项目实战
1.构建的聚合项目结构
build-some-emp
some-common
some-core
some-emp
其中build-some-emp 相当于maven的parent,some-emp依赖于some-common和some-core
而some-common依赖于some-core
这里,build-some-emp我使用eclipse的gradle插件创建的gradle项目,其余均为普通java项目
2.具体配置
在build-some-emp中build.gradle,内容如下
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
// if ($bp != null) { buildDir = "$bp" }
}
buildscript {
repositories {
mavenCentral()
}
}
subprojects {
apply plugin: 'java'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
repositories {
mavenCentral()
maven {url 'http://jaspersoft.artifactoryonline.com/jaspersoft/jr-ce-releases'}
maven {url 'https://oss.sonatype.org/content/repositories/snapshots' }
mavenCentral artifactUrls: [
'http://jaspersoft.artifactoryonline.com/jaspersoft/third-party-ce-artifacts/'
]
}
dependencies {
compile 'com.google.code.gson:gson:2.3.1'
compile group: 'org.apache.poi', name: 'poi', version: '3.15'
}
}
project(':some-emp') {
dependencies {
compile project(':some-common')
compile project(':some-core')
}
}
project(':some-core') {
sourceSets {
main {
resources {
srcDir 'src/main/sql'
srcDir 'src/main/xml'
}
java{
srcDir 'src/main/ojdbc'
}
}
}
dependencies {
}
}
project(':some-common') {
dependencies {
compile 'net.lingala.zip4j:zip4j:1.3.2'
compile project(':some-core')
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.14.1'
}
- allprojects 全部项目构建设置,设置当前项目和其子项目的构建参数,这里主要是为了忽略构建警告
- buildscript 构建脚本,这个是gradle脚本执行所需依赖
- subprojects 所有子项目构建依赖设置,这里我们设置了java插件,编码集和仓库位置,此外还有所有子项目都依赖的构件
- project 具体子项目的构建设置,这里除了设置依赖之外,还设置了sourceSets,因为gradle默认的source folder 就是maven构建的那四个文件夹,其余source folder必须在配置文件指出,否则每次构建都会有
- task wrapper 解决没有安装gradle想要构建项目时的配置
在build-some-emp中的settings.gradle的内容如下
rootProject.name = 'build-some-emp'
includeFlat 'some-core','some-common','some-emp'
- 设置根项目名,要与当前项目一致
- includeFlat 指定子项目名,网上说平级时使用includeFlat,单我用include一样也能构建成功
经过上述配置后,就可以看到我们想要的结果了。
另附一个spring-boot项目配置:
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.10.RELEASE'
}
}
subprojects {
apply plugin: 'java'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
repositories {
mavenCentral()
maven {url 'http://jaspersoft.artifactoryonline.com/jaspersoft/jr-ce-releases'}
maven {url 'https://oss.sonatype.org/content/repositories/snapshots' }
mavenCentral artifactUrls: [
'http://jaspersoft.artifactoryonline.com/jaspersoft/third-party-ce-artifacts/'
]
}
dependencies {
compileOnly 'org.projectlombok:lombok:1.16.10'
compile 'com.google.code.gson:gson:2.3.1'
compile group: 'org.apache.poi', name: 'poi', version: '3.15'
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.15'
// jdbc
compile('org.springframework.boot:spring-boot-starter-jdbc')
// Doma
compile group: 'org.seasar.doma', name: 'doma', version: '2.19.2'
compile 'org.seasar.doma.boot:doma-spring-boot-starter:1.1.0'
// Thymeleaf
compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
// Test
testCompile 'junit:junit:4.12'
testCompile 'org.glassfish.web:javax.el:2.2.4'
testCompile 'org.springframework.boot:spring-boot-starter-test:1.4.1.RELEASE'
// Log
runtime 'org.slf4j:jul-to-slf4j:1.7.12'
// Spring
compile 'org.springframework.boot:spring-boot-starter-aop:1.4.1.RELEASE'
compile 'org.springframework.boot:spring-boot-devtools:1.4.1.RELEASE'
// Flyway
compile 'org.flywaydb:flyway-core:4.0.3'
compile 'org.springframework.boot:spring-boot-starter-jdbc:1.4.1.RELEASE'
// metrics-mackerel
compile group: 'net.unit8.metrics', name: 'metrics-mackerel', version: '0.1.1'
compile group: 'io.dropwizard.metrics', name: 'metrics-parent', version: '3.2.4', ext: 'pom'
compile group: 'io.dropwizard.metrics', name: 'metrics-core', version: '3.2.4', ext: 'pom'
// zip4j
compile 'net.lingala.zip4j:zip4j:1.3.2'
}
ext['thymeleaf.version'] = '3.0.9.RELEASE'
ext['thymeleaf-layout-dialect.version'] = '2.3.0'
}
project(':pdfconv-batch') {
apply plugin: 'java'
sourceSets {
main {
resources {
srcDir 'src/main/sql'
}
}
}
sourceCompatibility = targetCompatibility = 1.8
processResources.destinationDir = compileJava.destinationDir
compileJava.dependsOn processResources
dependencies {
compile project(':some-batch-common')
compile project(':some-common')
compile project(':some-core')
}
}
project(':some-batch-common') {
apply plugin: 'java'
sourceCompatibility = targetCompatibility = 1.8
sourceSets {
main {
resources {
srcDir 'src/main/sql'
}
}
}
// Doma設定
processResources.destinationDir = compileJava.destinationDir
compileJava.dependsOn processResources
dependencies {
compile project(':some-common')
compile project(':some-core')
}
}
project(':some-common') {
apply plugin: 'java'
sourceCompatibility = targetCompatibility = 1.8
sourceSets {
main {
resources {
srcDir 'src/main/sql'
}
}
}
processResources.destinationDir = compileJava.destinationDir
compileJava.dependsOn processResources
dependencies {
compile project(':some-core')
}
}
project(':some-core') {
apply plugin: 'java'
dependencies {
compile files ('lib/ojdbc7.jar')
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.14.1'
}