SpringBoot Maven打包项目JAR/WAR

安装Maven

  1. 登陆 http://maven.apache.org/download.cgijava

  2. 下载 maven 压缩包web

  3. 解压apache-maven-3.6.0-bin.tar.gz 到一个目录(我这里是MAC,解压到“/Users/shongbing/Applications/apache-maven-3.6.0”)spring

  4. 配置环境变量(MAC环境变量)apache

    cd ~vim

    vim .bash_profilebash

      在最后增长两行:app

      export MAVEN_HOME=/Users/shongbing/Applications/apache-maven-3.6.0webapp

      export PATH=$PATH:$MAVEN_HOME/binmaven

    source .bash_profilespring-boot

  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.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ first-app-demo --- [INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ first-app-demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ first-app-demo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 5 source files to /Users/shongbing/Downloads/first-app-demo/target/classes [INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ first-app-demo --- [INFO] Not copying test resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ first-app-demo --- [INFO] Not compiling test sources [INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ first-app-demo --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ first-app-demo --- [INFO] Building jar: /Users/shongbing/Downloads/first-app-demo/target/first-app-demo-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.1.2.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: 2019-01-13T14:18:25+08:00 [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) 2019-01-13 14:18:42.805  INFO 31960 --- [           main] c.v.f.FirstAppDemoApplication            : Starting FirstAppDemoApplication v0.0.1-SNAPSHOT on shongbing-a01.vmware.com with PID 31960 (/Users/shongbing/Downloads/first-app-demo/target/first-app-demo-0.0.1-SNAPSHOT.jar started by shongbing in /Users/shongbing/Downloads/first-app-demo/target) 2019-01-13 14:18:42.808  INFO 31960 --- [           main] c.v.f.FirstAppDemoApplication            : No active profile set, falling back to default profiles: default
2019-01-13 14:18:44.419  INFO 31960 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
2019-01-13 14:18:44.424  INFO 31960 --- [           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.1-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ first-app-demo --- [INFO] Deleting /Users/shongbing/Downloads/first-app-demo/target [INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ first-app-demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ first-app-demo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 5 source files to /Users/shongbing/Downloads/first-app-demo/target/classes [INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ first-app-demo --- [INFO] Not copying test resources [INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ first-app-demo --- [INFO] Not compiling test sources [INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ first-app-demo --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-war-plugin:3.2.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.1-SNAPSHOT] [INFO] Processing war project [INFO] Copying webapp resources [/Users/shongbing/Downloads/first-app-demo/src/main/webapp] [INFO] Webapp assembled in [173 msecs] [INFO] Building war: /Users/shongbing/Downloads/first-app-demo/target/first-app-demo-0.0.1-SNAPSHOT.war [INFO] [INFO] --- spring-boot-maven-plugin:2.1.2.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: 2019-01-13T14:54:28+08:00 [INFO] ------------------------------------------------------------------------

4. 运行WAR包

shongbing-a01:target shongbing$ java -jar first-app-demo-0.0.1-SNAPSHOT.war . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.2.RELEASE) 2019-01-13 14:56:17.380  INFO 33405 --- [           main] c.v.f.FirstAppDemoApplication            : Starting FirstAppDemoApplication v0.0.1-SNAPSHOT on shongbing-a01.vmware.com with PID 33405 (/Users/shongbing/Downloads/first-app-demo/target/first-app-demo-0.0.1-SNAPSHOT.war started by shongbing in /Users/shongbing/Downloads/first-app-demo/target) 2019-01-13 14:56:17.386  INFO 33405 --- [           main] c.v.f.FirstAppDemoApplication            : No active profile set, falling back to default profiles: default
2019-01-13 14:56:19.401  INFO 33405 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
2019-01-13 14:56:19.409  INFO 33405 --- [           main] c.v.f.FirstAppDemoApplication            : Started FirstAppDemoApplication in 2.594 seconds (JVM running for 3.183)
相关文章
相关标签/搜索