allprojects和subprojects区别
allprojects是对所有project的配置,包括Root Project。而subprojects是对所有Child Project的配置
settings.gradle
配置子模块
build.gradle例子
buildscript { // 指定脚本自身使用的资源
repositories {
mavenLocal()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
mavenCentral()
}
}
group 'cqt.goai'
version '1.0-SNAPSHOT'
allprojects {
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8 // jdk版本
dependencies { // 注意testCompile 和 compile 区别,compile才能给子模块搜索到
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.33.Final'
compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.3.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
// 复制jar包到指定文件
task copyJar(type:Copy) {
from ('build\\libs')
into ('.')
}
// 指定程序入口
jar {
from { configurations.runtime.collect{zipTree(it)} } // 打包依赖
manifest {
attributes("Main-Class": "com.zyf.common.netty.websocket.server.NettyServerLauncher")
}
}
}
gradle仓库
buildscript {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
mavenCentral()
}
}
gradle中文教程
https://www.w3cschool.cn/gradle_user_guide/gradle_user_guide-v6kt26h2.html
编译并打包你的代码, 但是并不运行单元测试
gradle build -x test
pause
多模块,一个模块引用另一个模块
dependencies {
compile project(':shared')
}
https://www.jianshu.com/writer#/notebooks/26273601/notes/40446052
解决 IDEA 创建 Gradle 项目没有src目录
task "create-dirs" << {
sourceSets*.java.srcDirs*.each {
it.mkdirs()
}
sourceSets*.resources.srcDirs*.each{
it.midirs()
}
}
https://www.cnblogs.com/yanfeiLiu/p/9237931.html
gradle打包脚本
gradle clean build -x test
gradle 指定打包的resources文件
processResources {
include "**/logback.xml"
include "**/config**.yml"
}
打包指定入口类和打包依赖
jar {
from { configurations.runtime.collect{zipTree(it)} } // 打包依赖
manifest {
attributes("Main-Class": "ServerApplication")
}
}
复制jar包
task copyJar(type:Copy){
from ('build\\libs')
into ('.')
}
依赖本地jar文件夹:
compile fileTree(dir:'libs',include:['*.jar'])
多模块gradle配置
- build.gradle
group 'com'
version '1.0-SNAPSHOT'
allprojects {
repositories {
mavenLocal()
mavenCentral()
}
// 多模块最重要这三个,加到allprojects里
apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
}
- setting.gradle
rootProject.name = 'irssystem'
include 'irs'
include 'common'
include 'apigateway'
- 子模块引用其他子模块
dependencies {
compile project(":common")
}