1. 程式人生 > >Gradle構建SpringBoot並打包可運行的jar配置

Gradle構建SpringBoot並打包可運行的jar配置

tasks odi ant manifest dea 1.8 dem static gradle

使用Gradle構建項目,繼承了Ant的靈活和Maven的生命周期管理,不再使用XML作為配置文件格式,采用了DSL格式,使得腳本更加簡潔。
構建環境:

  1. jdk1.6以上,此處使用1.8
  2. Gradle 4.4.1
  3. SpringBoot
  4. idea

一、下載並安裝Gradle

Gradle官網

技術分享圖片 Gradle官網

1.下載Gradle

下載Gradle

技術分享圖片 Gradle版本下載

2.解壓Gradle

下載之後解壓到你想存放的目錄

技術分享圖片 Gradle解壓

3.設置Gradle環境變量

  1. 創建一個環境變量 GRADLE_HOME,並指向你的Grdle的安裝路徑:
    技術分享圖片
    Gradle環境變量
  2. 添加 %GRADLE_HOME%\bin 到你的PATH 環境變量:
    技術分享圖片 Gradle環境變量

4.檢測配置

技術分享圖片 Gradle配置檢測

二、創建項目

1.選擇Gradle -> 勾選Java -> 選擇SDK(jdk1.8)

技術分享圖片 創建項目1
2.設置groupId和artifactid
技術分享圖片 創建項目2
3.選擇我們本地的Gradle,並設置JVM
![創建項目3]](//upload-images.jianshu.io/upload_images/7228029-4560fdecebd1979d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

三、配置項目

1.創建項目後沒自動生成src等文件夾

技術分享圖片 配置項目1
我們自己創建文件夾
技術分享圖片 配置項目2
2.配置build.gradle

allprojects {
    group ‘com.chen‘
    version ‘1.0-SNAPSHOT‘
    repositories {
        maven { url ‘http://maven.aliyun.com/nexus/content/groups/public/‘ }
    }
}
buildscript {
    ext {
        springIOVersion = ‘1.0.0.RELEASE‘
        springBootVersion = ‘1.5.9.RELEASE‘
    }
    repositories {
        jcenter()
        mavenLocal()
        mavenCentral()
        maven { url "http://repo.spring.io/release" }
        maven { url "http://repo.spring.io/milestone" }
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "io.spring.gradle:dependency-management-plugin:${springIOVersion}"
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

apply plugin: ‘idea‘
apply plugin: ‘java‘
apply plugin: ‘spring-boot‘
apply plugin: ‘io.spring.dependency-management‘

sourceCompatibility = 1.8
targetCompatibility = 1.8

jar {
    baseName = ‘gradle-demo‘
    version = ‘0.0.1‘
    manifest {
        attributes "Manifest-Version": 1.0,
                   ‘Main-Class‘: ‘com.chen.GradleDemoApplication‘
    }
}

repositories {
    jcenter()
    mavenLocal()
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom ‘io.spring.platform:platform-bom:Brussels-SR6‘
        mavenBom ‘org.springframework.cloud:spring-cloud-dependencies:Brixton.SR4‘
    }
}

ext {
    junitVersion = ‘4.12‘
}

dependencies {
    compile ‘org.springframework:spring-core‘
    compile ‘org.springframework.boot:spring-boot-starter-web‘
    compile ‘org.springframework.boot:spring-boot-autoconfigure‘
    compile ‘org.springframework.boot:spring-boot-starter-tomcat‘
    testCompile ‘org.springframework.boot:spring-boot-starter-test‘
    testCompile "junit:junit:${junitVersion}"
}

3.創建SpringBoot啟動類

@Controller
@SpringBootApplication
public class GradleDemoApplication {
  public static void main(String[] args) {
      SpringApplication.run(GradleDemoApplication.class, args);
  }

  @ResponseBody
  @GetMapping("/")
  public String hello() {
      return "Hello World!";
  }
}

四、啟動項目

  1. 先build項目
    技術分享圖片 啟動項目1
    技術分享圖片 啟動項目2
  2. 啟動項目,點擊bootRun
    技術分享圖片 啟動項目3
    技術分享圖片 啟動項目4

五、測試項目

技術分享圖片 測試項目

六、打包jar項目

技術分享圖片 打包項目1
技術分享圖片 打包項目2

七、執行jar

執行命令:java -jar build/libs/gradle-demo-0.0.1.jar

技術分享圖片 執行jar

作者:CatalpaFlat
鏈接:https://www.jianshu.com/p/9231b1f598c5
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並註明出處。

Gradle構建SpringBoot並打包可運行的jar配置