您的位置:首页 > 编程语言 > Java开发

Maven,Gradle分别建立Spring-boot的demo工程

2017-06-29 11:42 405 查看
1、Maven建立spring-boot工程

Maven虽然没有官方的Wrapper,但是有一个第三方的Wrapper可以使用。
命令如下:`mvn -N io.takari:maven:wrapper`


2、创建mvn项目

mvn archetype:generate  自动化生成项目


3、在pom.xml文件中添加spring-boot包依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>


整体代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>hello</groupId>
<artifactId>world</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>world</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>


4、在Java文件中修改App.java

package HelloWorld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
/**
* Hello world!
*
*/
@SpringBootApplication
@Controller
@EnableAutoConfiguration
public class App
{

@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}

public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}


5、启动项目

./mvnw spring-boot:run  OR  mvn spring-boot:run
./mvnw 是一个maven版本的第三方管理工具,类似于gradel的gradlew




6、gra
4000
dle构建spring-boot项目

安装好gradle环境之后,在项目目录下面填写gradle配置文件build.gradle

buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}

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


之后填写App.java文件,然后运行

./gradlew bootRun
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: