idea使用gradle搭建SpringBoot

首先介绍一下gradle,Gradle 是以 Groovy 语言为基础,面向Java应用为主.基于DSL(领域特定语言)语法的自动化构建工具,目前来说应该算是比较新的自动化构建工具了。为什么不用Maven要用Gradle呢,两个字:简洁!我们来对比一下

Maven定义

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Gradle定义

compile 'org.springframework.boot:spring-boot-starter-web'

对,你没看错,就仅仅是一行就定义好了。比原来繁琐的xml简单了很多。

好言归正传,我们来看看同过idea怎么构建springBoot的gradle项目的。


第一步,创建gradle项目,这个地方直接上图。

165816_TTNO_1760373.png

创建gradle项目

165816_L0gq_1760373.png

设置项目名称后点击finish

165816_ifpk_1760373.png

第二步,配置build.gradle

待项目创建完成后,就要对build.gradle进行修改,添加springBoot的依赖

170140_W3fN_1760373.png

以下是添加的代码

buildscript {
    repositories {
        mavenLocal()//maven的本地仓
        jcenter()//官方仓
    }
    dependencies {
        classpath("org.gradle.api.plugins:gradle-tomcat-plugin:1.2.5")
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

version = '1.0'
buildDir = 'target'

jar {
    baseName = 'product_catering'
    version =  '1.0'
}

configurations {
    //不进行依赖传递,否则要下载很多依赖项
    //例如 flex-framework
    starling.transitive = false
}

repositories {
    mavenLocal()
    jcenter()
}

[compileJava, javadoc, compileTestJava]*.options*.encoding = 'UTF-8'

dependencies {
    //compile : 编译时需要的依赖
    //runtime : 运行时需要的依赖,包括compile 
    //testCompile : testCase编译时需要的依赖
    //testRuntime : testCase运行时需要的依赖,包括testCompile 
    //还有两个不常用的archives、defaul
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework:spring-jdbc:4.1.4.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.hsqldb:hsqldb'
    compile 'org.apache.tomcat:tomcat-jdbc:8.0.18'
    testCompile 'junit:junit:4.11'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

idea.project {
    jdkName = '1.8'
    languageLevel = '1.8'
}


第三步,导入依赖

在idea右手的侧边栏找到gradle的面板,点开后,刷新项目就能自动下载对应的依赖包了

170813_mkQ9_1760373.png

第四步,创建springBoot示例

在项目中创建Maven约定风格的目录结构,例如

src\main\java

src\main\Resources 等等

添加包,例如

com.test

172009_hJAM_1760373.png

最后添加springBoot的启动类和controller


Main.java

/**
 * server 主入口
 * Created by Kenry.xian on 2015/5/27 0016.
 */
@Configuration //标明这个是SpringBoot的配置类,可以通过类配置代替原有的XML配置方式
@EnableAutoConfiguration //启动bean自动注入
@ComponentScan //启动自动搜索bean
public class Main extends WebMvcConfigurerAdapter {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Main.class);
    }
}


DemoConrtallor.jva

/**
 * Created by Kenry on 2015/5/27 0016.
 */
@RestController //标注此类为Controller
@EnableAutoConfiguration //启动自动注入

@RequestMapping("/app")
public class DemoController {

    @RequestMapping("/demo")
    @ResponseBody
    public Map<String, String> version(){
        HashMap<String, String> result = new HashMap<String, String>();
        result.put("show", "Hello world");
        return result;
    }
}

最后直接运行Main.java就会自己启动集成的tomcat了,访问:http://localhost:8080/app/demo就能看到效果,轻轻松松构建完成

转载于:https://my.oschina.net/kenryxian/blog/420681

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值