gradle項目中build.gradle配置文件詳細講解


新建gradle項目默認生成的build.gradle文件:
 1 plugins {
 2     id 'org.springframework.boot' version '2.2.6.RELEASE'
 3     id 'io.spring.dependency-management' version '1.0.9.RELEASE'
 4     id 'java'
 5 }
 6 
 7 group = 'com.example'
 8 version = '0.0.1-SNAPSHOT'
 9 sourceCompatibility = '1.8'
10 
11 repositories {
12     mavenCentral()
13 }
14 
15 dependencies {
16     implementation 'org.springframework.boot:spring-boot-starter-web'
17     testImplementation('org.springframework.boot:spring-boot-starter-test') {
18         exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
19     }
20 }
21 
22 test {
23     useJUnitPlatform()
24 }
View Code

ref: https://my.oschina.net/u/3101282/blog/1837493?from=groupmessage

標簽list: 

1 plugins # 定義一些插件

2 apply plugin: 'xxx'  # 使用插件

3 sourceCompatibility = "1.8"  # jdk版本聲明

 targetCompatibility = "1.8"

4 buildscript  # 聲明gradle腳本本身需要使用的資源

  repositories

PS: 需要注意的是,在build.gradle文件中,buildscript塊和plugin塊必須作為前兩個塊存在,否則會報錯的.

5 repositories  # 使用的倉庫優先級

  mavenCentral()

  jcenter()

  mavenLocal()

6 ext  xxxVersion = xx

7 configurations {
  mybatisGenerator
 }

// configure(allprojects) { project ->}表示內部的配置將會應用於所有的項目,在下面還會出現configure(subprojects){},
其表示 配置的數據將會應用在所有的子項目內,兩者的區別主要就是allprojects和subprojects

6 configure(allprojects)
  apply plugin:
  repositories
    mavenLocal()

7 configure(subprojects)  # 所有子項目的通用配置
  dependencyManagement
    imports
  dependencies
    compile project(":mybatisplus-demo")  # 要依賴的模塊
    implementation
    annotationProcessor
    compileOnly
    compile
    testImplementation
    testCompile
  compileJava
  task sourcesJar
  task javadocJar

8 dependencies  # 引入相關Jar包

  implementation
  testImplementation
  exclude

9 xxx = xxx

10 test

  useJUnitPlatform

11 //指定項目編碼
tasks.withType(JavaCompile) {
options.encoding = "${custom.encoding.OPTIONS}"
}

12 

/**
* 添加gradle wrapper包裝器
* 生成gradlew文件,統一gradle版本,避免因為版本不同產生的問題
*/
task wrapper(type: Wrapper) {
gradleVersion = "4.8" //這里的版本根據自己的需求變更.
}

13 

// 引入jar包依賴管理插件,統一管理所有項目的依賴,來盡量避免jar包沖突的問題.
plugins {
id 'org.springframework.boot' version '2.0.1.RELEASE' //spring提供的spring boot插件,主要用到了其依賴管理的功能.
}
然后在configure(allprojects) { project ->...}代碼塊的apply代碼區域新增:

apply plugin: 'org.springframework.boot' //spring boot插件
apply plugin: 'io.spring.dependency-management' //實現maven的依賴統一管理功能
並在頂部添加:

import org.springframework.boot.gradle.plugin.SpringBootPlugin

// 使用該插件完成依賴管理的功能
jar {
enabled = true
}
bootJar {
launchScript()
archiveName = "${project.group}_${project.name}_${project.version}.jar"
}

14  屏蔽父項目的構建jar包功能.

因為父項目不提供具體的業務,所以也就不需要打成jar包了,在父項目/build.gradle的底部加入下列代碼.

/**
* 關閉父項目的打包功能
*/
bootJar{
enabled=false
}
/**
* 關閉父項目的打包功能
*/
jar{
enabled=false
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM