天天看點

IDEA使用Gradle建構SpringBoot項目工程

背景

  • 最近在研究搭建spring源碼調試環境時,接觸到到gradle項目建構工具。由于之前習慣于maven項目的建構,故通過此文記錄相關gradle的項目建構知識。

Gradle

Gradle是一個建構工具,用于管理項目依賴和建構項目工程。Gradle抛棄了Maven的基于XML的繁瑣配置,采用特定語言Groovy的配置,大大簡化了建構代碼的行數。
  • 項目結構
  • IDEA使用Gradle建構SpringBoot項目工程
  • Plugin Sample
pluginManagement {
  repositories {
    gradlePluginPortal()
    maven { url 'https://repo.spring.io/plugins-release' }
  }
}

plugins {
  id "com.gradle.enterprise" version "3.2"
  id "io.spring.gradle-enterprise-conventions" version "0.0.2"
}

include "spring-aop"
include "spring-aspects"
include "spring-beans"
include "spring-context"
include "spring-context-indexer"
include "spring-context-support"
include "spring-core"
include "kotlin-coroutines"
project(':kotlin-coroutines').projectDir = file('spring-core/kotlin-coroutines')
include "spring-expression"      

IDEA使用Gradle建構SpringBoot項目工程

建立SpringBoot項目
IDEA使用Gradle建構SpringBoot項目工程
  • 使用Gradle建構項目
IDEA使用Gradle建構SpringBoot項目工程
  • 項目結構
  • src結構和maven結構一緻;gradle檔案夾 存放gradle wrapper相關檔案;build.gradle相當于maven裡面的pom.xml,setting.gradle用于多子產品的配置。
IDEA使用Gradle建構SpringBoot項目工程
  • build.gradle
  • plugins:插件配置;
  • sourceCompatibility:jdk版本号
  • repositories:倉庫配置,mavenCentral()代表中央倉庫;
  • dependencies:依賴的坐标集合
plugins {
    id 'org.springframework.boot' version '2.3.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}      
添加依賴
IDEA使用Gradle建構SpringBoot項目工程
  • 項目依賴的格式為作用範圍修飾符 ( ‘groupId:artifactId:version’ )
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }

    // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa
     compile ('org.springframework.boot: spring-boot-starter-data-jpa: 2.3.1 ')

}      

gradle打包

IDEA使用Gradle建構SpringBoot項目工程
IDEA使用Gradle建構SpringBoot項目工程