Using Gradle In Web
Repository
初始化顺序
- 命令行
- 放一个init.gradle文件到USER_HOME/.gradle/目录下
- 放一个后缀是.gradle的文件到USER_HOME/.gradle/init.d/ 目录下
- 放一个后缀是.gradle的文件到GRADLE_HOME/init.d/ 目录下
init.gradle
allprojects {
repositories {
mavenLocal()
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
}
}
LocalRepository
默认是用户目录下的.gradle文件夹
GRADLE_USER_HOME=C:\Users\percy\.gradle
常用命令
命令 | 描述 |
---|---|
init | 初始化一个项目 |
clean | 清除项目编译缓存 |
build | 构建项目 |
jar | 打包项目 |
bootJar | spring boot项目打包 |
bootRun | spring boot项目运行 |
常用示例
初始化项目
gradle init
根据提示信息创建即可
build.gradle
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building an application
id 'application'
}
dependencies {
// This dependency is found on compile classpath of this component and consumers.
implementation 'com.google.guava:guava:26.0-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
// Define the main class for the application
mainClassName = 'gradle.samples.simple.App'
gradle-samples-boot
使用gradle构建spring boot项目,如下是spring boot init初始化的配置:
build.gradle
buildscript {
ext {
springBootVersion = '2.1.1.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'io.training'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
compileOnly 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
gradle-samples-multi
setting.gradle
build.gradle
--|api
--|--|build.gradle
--|web
--|--|build.gradle
父项目
setting.gradle
rootProject.name = 'gradle-samples-multi'
include 'api', 'web'
build.gradld
plugins {
id "org.springframework.boot" version "2.1.1.RELEASE"
id "io.spring.dependency-management" version "1.0.6.RELEASE"
}
ext['lombok.version'] = "1.18.4"
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
allprojects {
repositories {
mavenCentral()
}
}
subprojects {
apply plugin: "java"
apply plugin: "org.springframework.boot"
apply plugin: "io.spring.dependency-management"
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
def defaultEncoding = 'UTF-8'
tasks.withType(AbstractCompile).each { it.options.encoding = defaultEncoding }
compileJava.dependsOn(processResources)
dependencies {
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
annotationProcessor "org.projectlombok:lombok"
compileOnly "org.springframework.boot:spring-boot-configuration-processor"
compileOnly "org.projectlombok:lombok"
testImplementation "org.springframework.boot:spring-boot-starter-test"
}
}
project(':api') {
dependencies {
compileOnly "org.springframework.boot:spring-boot-starter"
}
}
project(':web') {
dependencies {
implementation project(':api')
implementation "org.springframework.boot:spring-boot-starter-web"
}
}
wrapper {
gradleVersion = "5.0"
}
api
build.gradle
bootJar.enabled=false
jar.enabled=true
web
build.gradle
bootJar {
mainClassName = 'gradle.samples.multi.MultiSamplesBootApplication'
}