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

来源:互联网 发布:网络直播电视版 编辑:程序博客网 时间:2024/05/18 23:12

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@EnableAutoConfigurationpublic 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、gradle构建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.8repositories {    mavenCentral()}dependencies {    compile('org.springframework.boot:spring-boot-starter-web')    testCompile('org.springframework.boot:spring-boot-starter-test')}

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

./gradlew bootRun
阅读全文
2 0
原创粉丝点击