[SpringBoot学习]-IDEA创建Gradle多Module结构的SpringBoot项目

版权声明:本文为博主原创文章,欢迎转载,转载请注明博主和原文链接。 https://blog.csdn.net/u014077888/article/details/78402193

一、前言

上个月由于做了一个小的后台系统,重新拾起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

阅读更多

没有更多推荐了,返回首页