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
}

 

posted @ 2020-04-10 09:12  Caesar_the_great  阅读(9766)  评论(0编辑  收藏  举报