下图红框内容,决定了你是打包为jar还是war。
我以为打包为war为例说明。
1.设定springboot项目的启动类,在pom.xml中加入以下内容
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.2.RELEASE</version>
<configuration>
<mainClass>com.DemoApplication</mainClass>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
2.进入springboot项目的pom.xml所在的目录
执行命令 mvn clean package ,项目会被打包到同级的target文件夹中。
$ mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com:myspringboot >--------------------------
[INFO] Building myspringboot 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ myspringboot ---
[INFO] Deleting D:\myfactory\myspringboot\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myspringboot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myspringboot ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 40 source files to D:\myfactory\myspringboot\target\classes
[WARNING] /D:/myfactory/myspringboot/src/main/java/com/service/OrderServiceImpl.java: D:\myfactory\myspringboot\src\main\java\com\service\OrderServiceImpl.javaʹ▒▒▒▒δ▒▒▒▒▒
3.进入刚刚打好的war包同级目录,执行命令 java -jar myspringboot-xxxxx.war
2018-09-12 22:52:07.405 INFO 4648 --- [ main] .m.m.a.ExceptionHandler ExceptionResolver : Detected @ExceptionHandler methods in urlCheckController
2018-09-12 22:52:07.444 INFO 4648 --- [ main] o.s.w.s.handler.SimpleU rlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [clas s org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-12 22:52:09.513 INFO 4648 --- [ main] o.s.j.e.a.AnnotationMBe anExporter : Registering beans for JMX exposure on startup
2018-09-12 22:52:09.516 INFO 4648 --- [ main] o.s.j.e.a.AnnotationMBe anExporter : Bean with name 'dataSource' has been autodetected for JMX ex posure
2018-09-12 22:52:09.523 INFO 4648 --- [ main] o.s.j.e.a.AnnotationMBe anExporter : Located MBean 'dataSource': registering with JMX server as M Bean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-09-12 22:52:09.644 INFO 4648 --- [ main] s.b.c.e.t.TomcatEmbedde dServletContainer : Tomcat started on port(s): 8080 (http)
2018-09-12 22:52:09.651 INFO 4648 --- [ main] com.DemoApplication : Started DemoApplication in 15.686 seconds (JVM running for 1 6.534)
2018-09-12 22:52:34.232 INFO 4648 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[loc alhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-09-12 22:52:34.232 INFO 4648 --- [nio-8080-exec-1] o.s.web.servlet.Dispatc herServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-09-12 22:52:37.272 INFO 4648 --- [nio-8080-exec-1] o.s.web.servlet.Dispatc herServlet : FrameworkServlet 'dispatcherServlet': initialization complet ed in 3040 ms
4.在浏览器访问localhost:8080就进入了项目首页