一、先創建一個Maven項目
1.打開eclipse,新建一個項目
File->new->Maven Prooject(若Maven Project不存在,可以在other中尋找)
2.這里的界面默認,點擊next,出現如下界面:
3.選擇我們需要構建的webapp,然后點擊next,
4.輸入必要的信息,點擊finish,完成了一個webapp項目的基礎創建。
項目結構如圖所示
發現有紅叉, 右鍵點擊SpringBootProject->Build Path->Configure Build Path
選擇增加運行時環境,就是配置Tomcat服務器
然后next->選擇你的Tomcat版本->finish
項目紅叉就沒了。
二、項目的基本配置
1.配置Deployment Decriptor
點擊Deployment Decriptor(上面圖中)
替換方式:
新建一個Dynamic Web Project
之后打開Dynamic Web Project中的Deployment Decriptor
復制前兩行
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
粘貼到SpringBootProject項目的Deployment Decriptor中進行替換。
選擇Navagator->SpringBootProject->.setting->org.eclipse.wst.common.project.facet.core.xml點擊打開
之后保存。
選擇Navagator->SpringBootProject->.classpath
刪除這一行(在最后面)
<classpathentry kind="output" path="target/classes"/>
保存。然后alt+f5刷新項目
2.配置pom.xml,點擊打開

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent>
添加依賴:

<dependency> <!-- web啟動 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <!-- 開發工具 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>provided</scope> <optional>true</optional> </dependency> <dependency> <!-- 熱加載模板框架 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
保存,之后maven會自動幫你下載依賴jar包(如圖所示)
三、開始一個項目
在src/main/java下創建包com.demo
在包下新建類StartUpApplication,並寫入如下信息

package com.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class StartUpApplication { public static void main(String[] args) { SpringApplication.run(StartUpApplication.class, args); } }
然后在此界面右鍵->run as->java application
出現此信息則說明運行成功(此信息在最下面)
在Java Resource中新建Source Folder並命名為src/main/webapp
然乎右鍵點擊項目->properties->Deployment Assembly,保存成如下
就是刪除了一個多余的webapp,只留下根目錄的那個。
然后在src/main/resources中新建文件夾命名static
在這個目錄下放入靜態的html文件
打開瀏覽器訪問127.0.0.1:8080就可以看到你寫好的頁面了。
四、使用Maven進行程序打包
在pom.xml最下面的build中
<build>
<finalName>MavenProject</finalName>
<!-- 添加打包的插件 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>com.demo.StartUpApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
然后把<packaging>war</packaging>改成
<!-- 變成jar包(用於打包),本來是war包的 -->
<packaging>jar</packaging>
更新項目(在eclipse中alt+F5)更新,
之后在.classpath中刪除這一行(最后面)
<classpathentry kind="output" path="target/classes"/>
注意:每次更新項目,在.classpath中都會重新生成這個東西。
右鍵項目名->run as->Maven install
打包的jar文件在當前目錄->target->MavenProject.jar
打包完成后,可直接點擊運行或者進入到打包該文件的目錄下進入cmd
輸入
java -jar MavenProject.jar
若要刪除jar包
右鍵項目名->run as->Maven clean