1. 程式人生 > >[SpringBoot學習]-IDEA建立Gradle多Module結構的SpringBoot專案

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

一、前言

上個月由於做了一個小的後臺系統,重新拾起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、整體專案結構
這裡寫圖片描述

相關推薦

[SpringBoot學習]-IDEA建立GradleModule結構SpringBoot專案

一、前言 上個月由於做了一個小的後臺系統,重新拾起Spring,發現之前搞的SSH、SpringMVC已經過時,新的SpringBoot整合了大部分的後端框架,俗稱Spring全家桶,還集成了tomcat更方便開發測試,故在寫專案的同時順便學習一下Spring

Intellij IDEA 建立gradle專案

首先在IDEA裡面建立一個Gradle專案,專案名為Gradle:目錄結構如下 2.接著在Gradle目錄下建立兩個資料夾:FirstModule,SecondModule,並將目錄結構仿照src目錄形式建立,並將原src目錄刪除.如圖: 接著在grad

學習Gradle--用IntelliJ IDEA建立gradle專案

1.在初始介面選擇create new project或在選單欄中選擇File-->New-->project 2.選擇Gradle,SDK中如果安裝了JDK會自動選擇JDK,如果沒有則點選New選擇本地下載的JDK,然後選擇Java,點選NEXT 3.GroupId是組名

eclipse中搭建springboot學習(12)---資料來源

pom.xml新增依賴  <dependency>              <groupId>org.springframework.boot</groupId&

springboot學習(六): 建立自定義的springboot starter

說明 在學習中,希望能建立自己的依賴,讓springboot通過<dependency>引用。springboot提供了starter來引用,所以通過建立自定義的starter來實現。有關springboot starter的知識詳見Spring B

springboot 入門-Idea建立helloworld工程

一:準備環境 1.JDK環境:JDK1.8 開發環境:IDEA2018.3 二:建立IDEA專案工程 三:HelloWorld例項 目錄結構: 編寫controllor程式碼 package com.controller; import or

Idea建立Maven模組+Spring-boot cassandra-CqlTemplate的使用

1、建立多模組的專案請參考https://blog.csdn.net/sunxiaoju/article/details/84343002 2、建立一個cassandra模組,並建立好相應的目錄包以及對應的檔案,如下圖所示: 3、然後配置pom.xml,需要新增對應的依賴包,如下程

Idea建立Maven模組+Spring-boot cassandra-CassandraTemplate的使用

1、建立多模組的專案請參考https://blog.csdn.net/sunxiaoju/article/details/84343002 2、建立一個cassandra模組,並建立好相應的目錄包以及對應的檔案,如下圖所示: 3、然後配置pom.xml,需要新增對應的依賴包,如下程

建立一對結構例項 /操作的三種方式

例 1.註冊App01  完成各項配置 2. 寫完後自動生成一個id自增列(主鍵) 如果不想生成 自己寫   建立兩張表    3.執行建立語句 (其中還進行了一個小修改)   4.按照之前的方法 開啟資料庫 並輸入資料 &

建立GradleModel模組

這步加不加入倉庫jar包的優先順序 都可以 在GradelParent工程選擇new model 此時新建完的工程 預設都是gradle Java工程 要改成web工程: 新增元件war 在main下面建立webapp

Java之idea建立gradle專案預設沒有src和resources解決方法

在專案根目錄下的build.gradle中新增建立目錄的任務,如下: task "create-dirs" << { sourceSets*.java.srcDirs*.each{ it.mkdirs() }

idea建立Gradle專案無法自動生成src目錄

1.在用idea建立好自己的專案時,發現目錄結構如下: 2.這時會不會很苦惱為什麼沒有自動建立src目錄呢 這時因為新建的資料夾不是Source Dir,所以需要自己在gradle裡面新增一個Tas

用IntelliJ IDEA建立Gradle專案簡單入門

Gradle和Maven一樣,是Java用得最多的構建工具之一,在Maven之前,解決jar包引用的問題真是令人抓狂,有了Maven後日子就好過起來了,而現在又有了Gradle,Maven有的功能它都有,且看起來更漂亮,咱麼就用起來吧。 裝好Intellij IDEA之

idea建立gradle專案失敗 執行環境上下文不一致導致的錯誤

最近有空就瞭解下gradle構建工具 也正好熟悉使用idea,之前一直都是eclipse,覺得這是趨勢,業餘先熟練起來 用idea建立gradle專案 自己配置的java_home都是指向的jdk build一下說是上下文不一致,指向的是jre 搞了兩三天一直琢磨,不知道哪

SpringBoot學習第一篇:構建第一個SpringBoot工程

https://www.fangzhipeng.com/springboot/2017/07/11/springboot1 本文出自方誌朋的部落格   簡介spring boot 它的設計目的就是為例簡化開發,開啟了各種自動裝配,你不想寫各種配置檔案,引入相關的依賴就能迅速搭建起一

springboot學習小筆記(一)---新建springboot專案

springboot學習中會遇到一些細節上的問題,總結一下 idea新建springboot專案 當你新建springboot專案是,有一步驟你費解過沒? 在使用Spring Initializr 建立專案時,這些需要怎麼填,Group(陣列)應該怎麼填,Artifact(標識)應

idea建立和匯入cp原始碼出售專案

IntelliJ IDEA 可以讓您建立cp原始碼出售《企娥21717 93408》專案或將 Maven 支援新增到任何現有專案。 啟動“新建專案(New Project)”嚮導。如果當前 IntelliJ IDEA 中未開啟任何專案,請單擊歡迎螢幕上的 Create New Project;否則,請從主選

如何使用IDEA建立Maven無模板的javaweb專案

背景 最近開始學習Maven工具,由於參考的尚矽谷視訊用的是Eclipse教學,而本人使用的是IDEA,程式碼的編寫和編譯器都存在差異。 所以,遇到的第一個問題便是IDEA如何建立無模板的Maven專案。 剛開始去百度了很多,基本上都是直接通過IDEA的wepapp模板建立一個m

IDEA上搭建模組的Maven專案(相互依賴)

需求:idea上搭建maven專案 MainModule,MainModule依賴ModuleA和 ModuleB,ModuleB依賴ModuleA.ModuleA 和ModuleB中只有serve層到資料庫層的檔案,即這兩個模組的maven專案,只有src/main/jav