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 }
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
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决