[SpringBoot学习]-IDEA创建Gradle多Module结构的SpringBoot项目
2017-10-31 12:57
786 查看
一、前言
上个月由于做了一个小的后台系统,重新拾起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
相关文章推荐
- 使用IDEA+GRADLE创建springboot项目
- springboot学习(一)IDEA创建springboot项目并部署到华为云中
- IDEA下gradle创建springboot项目
- IDEA创建基于springboot的多个module聚合项目,并生成war包
- IDEA下创建Springboot,freemarker,mockserver,Gradle项目
- 使用idea+gradle创建springboot项目
- idea 下gradle创建springboot 项目
- 用Intelij idea 创建 Spring Boot项目一创建Module openfirein-cloud-config-servier(1-2)
- IDEA中使用springBoot+gradle构建多模块项目
- 利用IDEA创建Spring Boot项目
- 快速创建一个基于Gradle构建的SpringBoot Web项目(SpringBoot-01)
- idea创建springboot项目图文教程(四)
- spring boot demo 学习(一) eclipse下创建第一个spring boot项目
- [转] 使用Spring Boot和Gradle创建项目
- 代码学习与实践 (一):Spring Boot 多模块项目创建与配置 注册 登录
- idea创建spring boot+mybatis(oracle)+themeleaf项目
- spring-cloud-build-项目pom结构1.3.4(Dalston.SR3)-boot(1.5.6) 学习笔记
- idea 创建多模块spring boot项目