[SpringBoot學習]-IDEA建立Gradle多Module結構的SpringBoot專案

天盟發表於2017-10-31

一、前言

上個月由於做了一個小的後臺系統,重新拾起Spring,發現之前搞的SSH、SpringMVC已經過時,新的SpringBoot整合了大部分的後端框架,俗稱Spring全家桶,還整合了tomcat更方便開發測試,故在寫專案的同時順便學習一下SpringBoot。由於本人目前主攻Android方向,所以對Intellj家族和Gradle更為喜歡,所有使用 IDEA、Gradle 、Gradle多Module 來開發SpringBoot,也使得Android開發習慣無縫遷移到SpringBoot開發上。相信IDEA、Gradle會是Web開發的主流環境。

二、建立專案

1、使用IEAD建立Gradle專案

這裡寫圖片描述

2、輸入工程名,GroupId不用寫

這裡寫圖片描述

3、使用gradle wrapper

這裡寫圖片描述

4、建立名為app的主module

這裡寫圖片描述

5、建立名為common的module

6、將根目錄settings.gradle 改為

include 'app'
include 'common'

7、將根目錄的build.gradle 改為

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        //AndroidStudio中開發springboot 使用apply plugin: 'org.springframework.boot'會有問題,必須新增一個AndroidModule才能正常。
        //使用Intellj 開發純java工程或者 java web 更方便
        //classpath 'com.android.tools.build:gradle:2.3.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://jitpack.io"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

###8、將app內的build.gradle改為

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
    }
}

repositories {
    jcenter()
    maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
    maven { url "http://repo.maven.apache.org/maven2" }
}

apply plugin: 'org.springframework.boot'
apply plugin: 'java'
apply plugin: 'maven'

group = 'com.masonliu.cc'
version = '1.0.0'
description = """"""

sourceCompatibility = 1.8
targetCompatibility = 1.8

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

sourceSets {
    main {
        //output.resourcesDir default is  'build/resources/main'
        //output.classesDir default is  'build/classes/main'
        //合併 classes 和 resources的輸出路徑,方便 直接執行 main方法時,在當前classpath下能找到 resources裡的配置檔案
        output.resourcesDir = 'build/classes/main'
        output.classesDir   = 'build/classes/main'
    }
}

configurations {
    deployerJars
}

//https://stackoverflow.com/questions/36923288/how-to-run-bootrun-with-spring-profile-via-gradle-task
//def profiles = 'dev'
//bootRun {
//    args = ["--spring.profiles.active=" + profiles]
//}

task pro << {
    bootRun.systemProperty 'spring.profiles.active', 'pro'
}

task dev << {
    bootRun.systemProperty 'spring.profiles.active', 'dev'
}

dependencies {
    compile project(":common")
    compile('org.xerial:sqlite-jdbc:3.8.11.2')
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version:'1.5.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-aop', version:'1.5.4.RELEASE'
    compile group: 'org.springframework', name: 'spring-context-support', version:'4.3.9.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version:'1.5.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version:'1.5.4.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version:'1.5.4.RELEASE'
    compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version:'1.3.0'
    compile group: 'mysql', name: 'mysql-connector-java', version:'5.1.38'
    compile group: 'com.alibaba', name: 'druid', version:'1.0.28'
    compile(group: 'org.quartz-scheduler', name: 'quartz', version:'2.3.0') {
        exclude(module: 'c3p0')
    }
    compile group: 'commons-lang', name: 'commons-lang', version:'2.6'
    compile group: 'commons-fileupload', name: 'commons-fileupload', version:'1.3.1'
    compile group: 'commons-io', name: 'commons-io', version:'2.5'
    compile group: 'commons-codec', name: 'commons-codec', version:'1.10'
    compile group: 'commons-configuration', name: 'commons-configuration', version:'1.10'
    compile group: 'org.apache.shiro', name: 'shiro-core', version:'1.3.2'
    compile group: 'org.apache.shiro', name: 'shiro-spring', version:'1.3.2'
    compile group: 'com.github.axet', name: 'kaptcha', version:'0.0.9'
    compile group: 'com.qiniu', name: 'qiniu-java-sdk', version:'[7.2.0, 7.2.99]'
    compile group: 'com.aliyun.oss', name: 'aliyun-sdk-oss', version:'2.5.0'
    compile(group: 'com.qcloud', name: 'cos_api', version:'4.4') {
        exclude(module: 'slf4j-log4j12')
    }
    testCompile(group: 'org.springframework.boot', name: 'spring-boot-starter-test', version:'1.5.4.RELEASE') {
        exclude(module: 'commons-logging')
    }
    compile('com.h2database:h2')
    deployerJars "org.apache.maven.wagon:wagon-ssh:2.2"
}

9、同步gradle下載依賴庫

這裡寫圖片描述

10、環境好了,新增測試controller

package com.masonliu.cc; /**
 * Created by liumeng02 on 2017/10/30.
 */
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration

public class TestController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(TestController.class, args);
    }
}

三、啟動

1、命令列啟動

gradlew bootRun

2、執行TestController的Main方法

四、效果

1、在瀏覽器內開啟http://localhost:8080

這裡寫圖片描述

2、整體專案結構

這裡寫圖片描述


程式碼地址:http://download.csdn.net/download/u014077888/10046447

相關文章