SpringBoot Maven打包项目JAR/WAR
安装Maven
1. 登录 http://maven.apache.org/download.cgi
2. 下载 maven 压缩包
3. 解压apache-maven-3.6.0-bin.tar.gz 到一个目录(我这里是MAC,解压到“/Users/shongbing/Applications/apache-maven-3.6.0”)
4. 配置环境变量(MAC环境变量)
cd ~
vim .bash_profile
在最后增加两行:
export MAVEN_HOME=/Users/shongbing/Applications/apache-maven-3.6.0
export PATH=$PATH:$MAVEN_HOME/bin
source .bash_profile
5. 测试是否安装成功
mvn -v
Maven打包命令介绍
mvn clean package 依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)等7个阶段;
mvn clean install 依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)、install等8个阶段;
mvn clean deploy 依次执行了clean、resources、compile、testResources、testCompile、test、jar(打包)、install、deploy等9个阶段。
由上面的分析可知主要区别如下:
package命令完成了项目编译、单元测试、打包功能,但没有把打好的可执行jar包(war包或其它形式的包)布署到本地maven仓库和远程maven私服仓库;
install命令完成了项目编译、单元测试、打包功能,同时把打好的可执行jar包(war包或其它形式的包)布署到本地maven仓库,但没有布署到远程maven私服仓库;
deploy命令完成了项目编译、单元测试、打包功能,同时把打好的可执行jar包(war包或其它形式的包)布署到本地maven仓库和远程maven私服仓库。
配置mainClass
需要在pom.xml中增加configruation配置mainClass
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.vmware.firstappdemo.FirstAppDemoApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
第一种:打包为JAR
1. 打开命令行进入项目根目录,运行命令:mvn -Dmaven.test.skip -U clean package
shongbing-a01:~ shongbing$ cd Downloads/first-app-demo
shongbing-a01:first-app-demo shongbing$ mvn -Dmaven.test.skip -U clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.vmware:first-app-demo >----------------------
[INFO] Building first-app-demo 0.0.-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.:clean (default-clean) @ first-app-demo ---
[INFO]
[INFO] --- maven-resources-plugin:3.1.:resources (default-resources) @ first-app-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying resource
[INFO] Copying resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.:compile (default-compile) @ first-app-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling source files to /Users/shongbing/Downloads/first-app-demo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.:testResources (default-testResources) @ first-app-demo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.:testCompile (default-testCompile) @ first-app-demo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.22.:test (default-test) @ first-app-demo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.1.:jar (default-jar) @ first-app-demo ---
[INFO] Building jar: /Users/shongbing/Downloads/first-app-demo/target/first-app-demo-0.0.-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1..RELEASE:repackage (repackage) @ first-app-demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.278 s
[INFO] Finished at: --13T14::+:
[INFO] ------------------------------------------------------------------------
2. 运行jar包测试, java -jar xxxx.jar
shongbing-a01:first-app-demo shongbing$ cd target/
shongbing-a01:target shongbing$ java -jar first-app-demo-0.0.1-SNAPSHOT.jar . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.2.RELEASE) -- ::42.805 INFO --- [ main] c.v.f.FirstAppDemoApplication : Starting FirstAppDemoApplication v0.0.1-SNAPSHOT on shongbing-a01.vmware.com with PID (/Users/shongbing/Downloads/first-app-demo/target/first-app-demo-0.0.-SNAPSHOT.jar started by shongbing in /Users/shongbing/Downloads/first-app-demo/target)
-- ::42.808 INFO --- [ main] c.v.f.FirstAppDemoApplication : No active profile set, falling back to default profiles: default
-- ::44.419 INFO --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s):
-- ::44.424 INFO --- [ main] c.v.f.FirstAppDemoApplication : Started FirstAppDemoApplication in 2.239 seconds (JVM running for 2.869)
第二种:打包为WAR
1. 在pom.xml的“modelVersion”后添加 <packaging>war</packaging>
2. 在目录 src/main/ 中创建目录 webapp/WEB-INF,然后在WEB-INF中添加 web.xml(内容为空)
3. 打开命令行进入项目根目录,运行命令:mvn -Dmaven.test.skip -U clean package
shongbing-a01:first-app-demo shongbing$ mvn -Dmaven.test.skip -U clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.vmware:first-app-demo >----------------------
[INFO] Building first-app-demo 0.0.-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.:clean (default-clean) @ first-app-demo ---
[INFO] Deleting /Users/shongbing/Downloads/first-app-demo/target
[INFO]
[INFO] --- maven-resources-plugin:3.1.:resources (default-resources) @ first-app-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying resource
[INFO] Copying resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.:compile (default-compile) @ first-app-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling source files to /Users/shongbing/Downloads/first-app-demo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.:testResources (default-testResources) @ first-app-demo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.:testCompile (default-testCompile) @ first-app-demo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.22.:test (default-test) @ first-app-demo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-war-plugin:3.2.:war (default-war) @ first-app-demo ---
[INFO] Packaging webapp
[INFO] Assembling webapp [first-app-demo] in [/Users/shongbing/Downloads/first-app-demo/target/first-app-demo-0.0.-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/shongbing/Downloads/first-app-demo/src/main/webapp]
[INFO] Webapp assembled in [ msecs]
[INFO] Building war: /Users/shongbing/Downloads/first-app-demo/target/first-app-demo-0.0.-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1..RELEASE:repackage (repackage) @ first-app-demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.981 s
[INFO] Finished at: --13T14::+:
[INFO] ------------------------------------------------------------------------
4. 运行WAR包
shongbing-a01:target shongbing$ java -jar first-app-demo-0.0.1-SNAPSHOT.war . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.2.RELEASE) -- ::17.380 INFO --- [ main] c.v.f.FirstAppDemoApplication : Starting FirstAppDemoApplication v0.0.1-SNAPSHOT on shongbing-a01.vmware.com with PID (/Users/shongbing/Downloads/first-app-demo/target/first-app-demo-0.0.-SNAPSHOT.war started by shongbing in /Users/shongbing/Downloads/first-app-demo/target)
-- ::17.386 INFO --- [ main] c.v.f.FirstAppDemoApplication : No active profile set, falling back to default profiles: default
-- ::19.401 INFO --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s):
-- ::19.409 INFO --- [ main] c.v.f.FirstAppDemoApplication : Started FirstAppDemoApplication in 2.594 seconds (JVM running for 3.183)
SpringBoot Maven打包项目JAR/WAR的更多相关文章
- 用maven打包项目成war文件
打开pom.xml,右键选择Run As -> Maven package就可以自动打包成war文件了.
- 关于使用IDEA,使用Maven打包项目
关于使用IDEA,使用Maven打包项目 在近期的一个接口项目中,使用的是SpringBoot + Maven的配置, 由于使用IDEA不久,不太熟悉使用Maven进行项目打包.记录一下. 由于使用的 ...
- spring boot 使用maven和fat jar/war运行应用程序的对比
文章目录 简介 Spring Boot Maven Plugin 使用Maven命令来运行应用程序 作为fat jar/war包运行应用程序 详解War文件 详解jar文件 如何选择 使用maven和 ...
- 使用maven打包项目遇到错误: http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
今天在使用maven打包项目时遇到一个错误: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin ...
- spring-boot子模块打包的jar中去掉BOOT-INF文件夹
1.spring-boot maven打包,一般pom.xml文件里会加 <plugin> <groupId>org.springframework.boot</grou ...
- springboot maven打包插件
<build> <plugins> <!-- springboot maven打包--> <plugin> <groupId>org.spr ...
- springboot项目打包成jar/war包
springboot项目打包过程中包含第三方jar 开发IDE是IntelliJ IDEA,数据库是mysql,内置服务器tomcat. 打包步骤: 1. 确定项目调试运行没问题 2. 将第三方jar ...
- springBoot 项目 jar/war打包 并运行
一:idea 打jar 包 简述:springboor 项目最好的打包方式就是打成jar ,下边就是简单的过程 1.打开idea工具 ,选着要打开的项目, 然后打开view--> too ...
- Maven三种打包方式jar war pom
1.pom工程 用在父级工程或聚合工程中.用来做jar包的版本控制.必须指明这个聚合工程的打包方式为pom 2.war工程 将会打包成war,发布在服务器上的工程.如网站或服务.在SpringBoot ...
随机推荐
- git 显示多个url地址推送
前提 一般来说,我们为git增加远程库,一般都是git remote add origin <url> ( 你可以使用真实的地址来代替 \<url\> ) 但是你可能想要把你的 ...
- ios应用,64位问题,Missing 64-bit support
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmFyb25fYmxvZ3M=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 1020 逆序排列(DP)
1020 逆序排列 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序 ...
- 网络摄像机进行互联网视频直播录像方案的选择,EasyNVS or EasyCloud or EasyGBS?
背景需求 互联网视频直播越来越成为当前大势:直播的需求往往都伴随在录像的需求,对于录像,不同的场景又有不同的方案选择: 本篇博客将会介绍对应的几种录像方案,可以帮助有互联网录像需求的用户进行对应的录像 ...
- HTML+CSS实现简单三级菜单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Machine Learning - week 2 - Multivariate Linear Regression
Multiple Features 上一章中,hθ(x) = θ0 + θ1x,表示只有一个 feature.现在,有多个 features,所以 hθ(x) = θ0 + θ1x1 + θ2x2 + ...
- Ajax 处理json的方法不同
json字符串从从后台传递到前台的方法有两种 1.使用context.Response(); 2.使用webmethod 方法调用静态函数 返回的字符串 前者返回的json是obj类型,而后者返回的是 ...
- <2013 07 22> 游历西欧
从本月11号开始到昨天,10天时间,和其他六位同学畅游了西欧,路经慕尼黑-巴塞罗马-尼斯-马赛-巴黎-阿姆斯特丹,最后回到慕尼黑,每个地方都待了两天,参观了主要的景点和建筑,见识了本地文化与饮食. 令 ...
- nodejs从服务器获取数据
// 从服务器获取数据 request('http://192.168.1.7:8080/getDemo', function(error, response, body) { console.log ...
- LeetCode:矩阵置零【73】
LeetCode:矩阵置零[73] 题目描述 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], ...