安装
下载,解压,配置环境变量GRADLE_HOME,加入%GRADLE_HOME\bin%到path
gradle依赖于java,确保Java已经配置
gradle -v
Gradle 6.8
Build time: 2021-01-08 16:38:46 UTC Revision: b7e82460c5373e194fb478a998c4fcfe7da53a7e
Kotlin: 1.4.20 Groovy: 2.5.12 Ant: Apache Ant? version 1.10.9 compiled on September 27 2020 JVM: 1.8.0_271 (Oracle Corporation 25.271-b09) OS: Windows 10 10.0 amd64
hello world
新建build.gradle文件,并执行gradle projects命令,则会执行build.gradle脚本
print("hello world")
构建空项目
gradle init
类型: basic
脚本: groovy
生成的项目结构
- .gradle
- gradle
- .gitattributes
- .gitignore
- build.gradle
- gradlew
- gradlew.bat
- settings.gradle
关于gradlew
gradlew是对gradle的封装,兼容gradle不同的版本.
在项目根目录都会有gradlew,如果没有,则执行gradle wrapper生成
gradle wrapper
避免重复下载对gradle/wrapper/gradle-wrapper.properties修改,并复制压缩包到同级目录
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
#distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-bin.zip
distributionUrl=gradle-6.8-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
测试版本号
gradlew -v
构建不再使用gradle,而是gradlew
gradlew build
关于settings.gradle
rootProject.name = 'hello'
任务
gradlew tasks
Build Setup tasks init //初始化 wrapper //包装
Help tasks
buildEnvironment //构建环境 dependencies //依赖 dependencyInsight //内部依赖 help //帮助 javaToolchains //java信息 outgoingVariants //向外扩展? projects //列举所有项目 properties //列举所有配置 tasks //列举所有任务
多个子项目
设置settings.gradle
rootProject.name = 'hello'
include 'aaa','bbb'
查看
gradlew projects
Root project ‘hello’ ±-- Project ‘:aaa’ — Project ‘:bbb’
对子项目构建
gradlew :bbb:build
构建java项目
在空项目基础上追加src/main/java/com/aa/Main.java
当然gradle init中也能生成java,但是不太能够抓住本质
package com.aa;
public class Main{
public static void main(String[] args){
System.out.println("hello java");
}
}
build.gradle加入java插件
plugins{
id 'java'
}
构建
gradlew build
生成build文件夹
-
classes //class字节码目录 -
generated //未知? -
libs //jar包目录 -
tmp //未知?
插件java
在引入了插件java后,执行gradlew tasks查看所有任务时,增加了一部分
plugins{
id 'java'
}
apply plugin: 'java'
gradlew tasks
Build tasks
assemble //编译和创建jar,但不测试
build //完整编译,包含测试
buildDependents //依赖
buidNeeded //需求
classes //编译class
clean //清除build
jar 打包jar
testClasses //编译测试
Documentation tasks
javadoc //生成文档
Verification tasks
check //检查
test //测试
插件application
除了具备java插件的任务,还额外增加了任务
plugins{
id 'application'
}
Application tasks
run //运行,指定mainClassName=“com.aa.Main”
Distribution tasks
assembleDist
distTar //生成tar
distZip //生成zip
installDist //生成执行文件
插件java-lib
跟java插件似乎没区别
插件war
提供war任务,将项目打包为war,war区别去jar,用于发布web应用到tomcat
build.gradle
项目归属和版本信息
group 'org.example'
version '1.0-SNAPSHOT'
buildscript
可配置gradle自身所需的依赖和插件仓库,国内镜像能够加快下载速度
buildscript应当放在最前面,至少在plugins前面
buildscript {
repositories {
maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
}
}
plugins {
id 'java'
id "org.springframework.boot" version "2.4.5"
}
sourceSet,源集,基本应该用不上
sourceSets{
main{
}
}
java编译版本信息,源 ≤ 目标 ≤ gradle ≤ jvm
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_8
对jar任务的配置,当然也能对其他任务进行配置
jar{
baseName "com.xxx"
version '1.0-beta'
manifest{
attributes 'hello': 'world','Main-Class': 'com.aa.Main'
}
}
多模块构建
一般而言,项目由一个父工程和多个子模块
父工程拥有build.gradle和settings.gradle,子工程只有build.gradle
settings.gradle
rootProject.name = 'hello'
include 'm1','m2'
graph TB
h["hello"]--->m1 & m2
在父工程中,包含自身和子模块的所有任务.
如: 执行父工程jar任务,子模块的jar任务也会执行(除非子模块没有jar任务)
子模块之间则是独立的.
all和sub
对父工程及其所有子模块引入插件
allprojects {
apply plugin: 'java'
dependencies {
implementation group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
}
}
只对子模块引入插件
subprojects {
apply plugin: 'java'
}
依赖
对于依赖,一般用于war包
dependencies {
}
很多,一般只用implementation,compile,compileOnly,已经够用了
implementation
compile
compileOnly
compileClasspath
providedCompile
runtimeOnly
runtime
runtimeOnly
runtimeClasspath
runtimeElements
providedRuntime
testRuntime
testruntimeClasspath
testRuntimeOnly
testCompile
testCompileClasspath
testCompileOnly
testImplementation
apiElements
annotationProcessor
testAnnotationProcessor
项目依赖
模块依赖
implementation project(':m1')
本地依赖
implementation files('libs/gson-2.8.5.jar')
implementation fileTree(dir: 'libs', include: ['*.jar'])
远程依赖
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.25'
implementation 'mysql:mysql-connector-java:8.0.25'
镜像配置
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/' }
maven { url 'https://maven.aliyun.com/repository/spring-plugin' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/central' }
mavenLocal()
mavenCentral()
}
构建Spring Boot项目
插件dependency-management
用于管理依赖,在制定了Springboot版本后,其他的依赖也已经固定了,不是很推荐
plugins {
id 'java'
id 'org.springframework.boot' version '2.3.10.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
|