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

相關文章
相關標籤/搜索