Spring Boot(1-2) 使用Gradle构建Spring Boot项目

  

准备工做

  1. 已安装JDK1.8;
  2. 开发工具Intelj idea;
  3. 安装Gradle构建工具。

构建Spring Boot项目

第一步:建立Gradle项目

  1. 打开Intelj IDEA ,File -> New -> Project
  2. 按照以下图步骤,建立Gradle项目,点击Next下一步,填写GroupId、ArtifactId、Version
  3. 项目目录结构

第二步:build.gradle添加Spring Boot相关依赖

group 'www.muzi.com'
version '1.0-SNAPSHOT'

buildscript{
    ext{
        nexusUrlPrefix = "http://ip:8081"
        springBootVersion = '1.5.8.RELEASE'

    }
    repositories {
        mavenLocal()  //添加Maven本地资源库
        maven{
            url "${nexusUrlPrefix}/nexus/content/groups/public/"
        }
    }
    dependencies{
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java' //添加Java插件
apply plugin: 'maven'//添加Maven插件
apply plugin: 'maven-publish'//添加Maven发布插件
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar{
    baseName = "test-boot"
    version = "${version}"
}

//指定编码环境
sourceCompatibility = 1.8
//指定编译环境
targetCompatibility = 1.8
//设置编码格式
tasks.withType(JavaCompile){
    options.encoding = "UTF-8"
}

//添加资源库
repositories {
    mavenLocal()  //添加Maven本地资源库
    maven{
        url "${nexusUrlPrefix}/nexus/content/groups/public/"
    }
}
//默认发布到Maven Nexus私服的发行库
def nexusUrl="${nexusUrlPrefix}/nexus/content/repositories/releases/"
//若是为快照版本发布到Maven Nexus私服的快照库
if (version.endsWith("-SNAPSHOT")){
    nexusUrl="${nexusUrlPrefix}/nexus/content/repositories/snapshots/"
}
//上传nexus资源配置
uploadArchives {
    repositories{
        mavenDeployer{
            //上传资源到Maven私服
            repository(url:nexusUrl){
                authentication(userName:"admin",password:"admin123")
            }
            pom.version ="$project.version"
            pom.artifactId ="tools-mz"
            pom.groupId ="$project.group"
        }
    }
}

dependencies {
    compile(
            ["org.springframework.boot:spring-boot-starter-web"]
    )
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

第三步:建立一个HelloController

package www.muzi.com;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Date:2017/3/23 14:25
 */

/**
 * 在这里咱们使用@RestController(等价于@Controller和@RequestBody)
 */
@RestController
public class HelloController {
	/**
	 * 在这里使用@RequestMapping创建映射请求:http://127.0.0.1:8080/hello
	 */
	@RequestMapping("/haha")
	public String hello(){
		return "哈哈哈";
	}
}

第四步:编写Spring Boot项目启动

package www.muzi.com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */

/**
 * 在这里使用@SpringBootApplication指定这是一个SpringBoot的应用程序
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
		/**
		 * 在main方法中进行启动咱们的应用程序
		 */
		SpringApplication.run(App.class, args);
	}
}

@SpringBootApplication开启了Spring 的组件扫描和Spring Boot的自动配置功能。实际
上,@SpringBootApplication将三个有用的注解组合在了一块儿。 java

  1. Spring 的@Configuration:标明该类使用Spring 基于Java 的配置。
  2. Spring 的@ComponentScan:启用组件扫描,这样你写的Web控制器类和其余组件才能被
    自动发现并注册为Spring 应用程序上下文里的Bean。 
  3. Spring Boot的@EnableAutoConfiguration :这个不起眼的小注解也能够称为@Abracadabra,就是这一行配置开启了Spring Boot自动配置的魔力,让你不用再写成篇的配置了。

第五步:测试

第一种运行方式:右键Run'App.main()',而后在浏览器输入测试访问地址:http://127.0.0.1:8080/haha  (项目默认端口8080)web

第二种运行方式:点击Gradle Projects->Tasks->application->bootRunspring

相关文章
相关标签/搜索