1. 项目结构及说明
1 2 3 4 5 6 7 8 9 10 11
| gradle/ wrapper/ gradle-wrapper.jar gradle-warpper.properties build.gradle gradlew gradlew.bat gradle.properties setting.properties
|
2. 配置说明
2.1. gradle-warpper.properties
1 2 3 4 5 6 7 8 9 10
| # 用户目录 distributionBase=GRADLE_USER_HOME # 下载位置 distributionPath=wrapper/dists #distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip # 下载地址 distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-6.9.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists
|
2.2. build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| plugins { id 'java' }
apply from: 'grammar.gradle' apply from: 'logger.gradle'
repositories { maven { url "https://maven.aliyun.com/nexus/content/groups/public" } mavenCentral() }
buildscript { repositories { maven { url "https://maven.aliyun.com/nexus/content/groups/public" } mavenCentral() } dependencies { classpath("io.spring.gradle:dependency-management-plugin:0.5.3.RELEASE") classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RC1") } } subprojects { 配置结构和父工程相似 }
|
2.3. gradle.properties
1 2 3 4
| springBootVersion=1.2.1
systemProp.systemVersion=1.0.0
|
2.4. setting.gradle
1 2 3 4
| include 'common' include 'web'
|
3. 语法说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| ext { springVersion="2.2.5.RELEASE" classesDir = new File('build/classes') }
task check2 { println "------------check2-----------" println "project.name: "+project.name println "project.springVersion: "+project.springVersion println "systemVersion: " + System.properties['systemVersion'] if(project.hasProperty('springVersion')){ println 'has spring version' } else { println 'has not spring version' } }
task compile2(dependsOn: 'resources') { if (project.classesDir.isDirectory()) { println 'The class directory exists. I can operate' } }
|
4. 插件
4.1. java插件
java相关插件
1 2 3 4 5 6
| apply plugin: 'java'
compileJava.destinationDir = file("target/classes")
|
4.2. management插件
依赖管理插件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| plugins { id "io.spring.dependency-management" version "1.0.8.RELEASE" id 'java' }
dependencyManagement { dependencies { dependency 'commons-io:commons-io:2.11.0' dependency 'commons-collections:commons-collections:3.2' } imports { mavenBom 'org.springframework.boot:spring-boot-dependencies:2.3.5.RELEASE' mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR7' } }
|
5. 多模块工程
5.1. 父模块
build.gradle 额外配置subprojects方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| plugins { id "io.spring.dependency-management" version "1.0.8.RELEASE" }
allprojects { apply plugin: 'java' apply plugin: 'idea' }
subprojects { sourceCompatibility = 1.8 targetCompatibility = 1.8 version = '0.0.1' buildscript { repositories { maven { url "https://maven.aliyun.com/nexus/content/groups/public" } mavenCentral() } dependencies { classpath("io.spring.gradle:dependency-management-plugin:0.5.3.RELEASE") classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.0.RC1") } } apply plugin: 'io.spring.dependency-management' dependencyManagement { dependencies { dependency 'commons-io:commons-io:2.11.0' dependency 'commons-collections:commons-collections:3.2' } imports { mavenBom 'org.springframework.boot:spring-boot-dependencies:2.3.5.RELEASE' mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR7' } } repositories { maven { url "https://maven.aliyun.com/nexus/content/groups/public" } mavenCentral() } tasks.withType(JavaCompile) { options.encoding = "UTF-8" } }
|
setting.gradle
1 2 3
| include 'common' include 'web'
|
5.2. 子模块
build.gradle
1 2 3 4 5 6 7 8
| dependencies { compile group: 'commons-collections', name: 'commons-collections' compile 'org.springframework.boot:spring-boot-starter-web' compile 'org.springframework.boot:spring-boot-starter-test' compile 'commons-io:commons-io' implementation(project(':common')) }
|