SpringBoot之项目打war包和jar包-yellowcong

朱雀 2022-05-27 06:36 420阅读 0赞

这一章,介绍关于SpringBoot如何打成war包和jar包的,具体步骤:1、配置pom.xml的tomcat,2、在pom.xml配置springboot的启动类,3、配置打包方式,4、配置启动类情况。我这个地方是吃了一个亏啊,就是打包的时候,仓库出问题了,导致死活打不了,后来将本地仓库地址修改了一下,就可以打上包了,这算误伤啊,坑爹。

打war包

1、删除pom.xml的内置tomcat

删除内置的tomcat,不然打成war包,tomcat和springboot内置的tomcat搞一起,会打架的。

  1. <!--把内置的tomcat给注释掉 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-tomcat</artifactId>
  5. <scope>provided</scope>
  6. </dependency>

2、配置pom.xml的启动类

配置Springboot的启动类

  1. <build>
  2. <plugins>
  3. <!-- 添加spring的插件, 就可以直接通过 mvn spring-boot:run 运行了 -->
  4. <plugin>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-maven-plugin</artifactId>
  7. <configuration>
  8. <mainClass>com.yellowcong.ConfigMain</mainClass>
  9. </configuration>
  10. <executions>
  11. <execution>
  12. <goals>
  13. <goal>repackage</goal>
  14. </goals>
  15. </execution>
  16. </executions>
  17. <!--在这里添加 springloader plugin,热部署 -->
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>springloaded</artifactId>
  22. <version>1.2.4.RELEASE</version>
  23. </dependency>
  24. </dependencies>
  25. </plugin>
  26. <plugin>
  27. <groupId>org.apache.maven.plugins</groupId>
  28. <artifactId>maven-compiler-plugin</artifactId>
  29. <configuration>
  30. <source>1.7</source>
  31. <target>1.7</target>
  32. </configuration>
  33. </plugin>
  34. </plugins>
  35. </build>

3、修改打包方式

修改打包的方式为<packaging>war</packaging>方式。

  1. <groupId>yelllowcong.com</groupId>
  2. <artifactId>springboot-sso</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>war</packaging>
  5. <name>springboot-yellowcong</name>
  6. <url>http://maven.apache.org</url>

4、修改启动类,继承SpringBootServletInitializer

添加继承的类为SpringBootServletInitializer ,然后复写里面的configure方法,设定启动类。

  1. @SpringBootApplication
  2. @EnableAutoConfiguration
  3. public class ConfigMain extends SpringBootServletInitializer {
  4. @Override
  5. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  6. return application.sources(ConfigMain.class);
  7. }
  8. public static void main(String[] args) {
  9. SpringApplication.run(ConfigMain.class, args);
  10. }
  11. }

5、打包测试

通过命令mvn clean package 打包

这里写图片描述

打jra包

1、添加内置tomcat

添加内置的tomcat

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-tomcat</artifactId>
  4. <scope>provided</scope>
  5. </dependency>

2、配置启动类

配置启动类

  1. <build>
  2. <plugins>
  3. <!-- 添加spring的插件, 就可以直接通过 mvn spring-boot:run 运行了 -->
  4. <plugin>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-maven-plugin</artifactId>
  7. <configuration>
  8. <mainClass>com.yellowcong.ConfigMain</mainClass>
  9. </configuration>
  10. <executions>
  11. <execution>
  12. <goals>
  13. <goal>repackage</goal>
  14. </goals>
  15. </execution>
  16. </executions>
  17. <!--在这里添加 springloader plugin,热部署 -->
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>springloaded</artifactId>
  22. <version>1.2.4.RELEASE</version>
  23. </dependency>
  24. </dependencies>
  25. </plugin>
  26. <plugin>
  27. <groupId>org.apache.maven.plugins</groupId>
  28. <artifactId>maven-compiler-plugin</artifactId>
  29. <configuration>
  30. <source>1.7</source>
  31. <target>1.7</target>
  32. </configuration>
  33. </plugin>
  34. </plugins>
  35. </build>

3、配置打包方式

修改打包的方式为 <packaging>jar</packaging> ,这样打包的方式就是jar了

  1. <groupId>yellowcong.com</groupId>
  2. <artifactId>springboot-sso</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>jar</packaging>
  5. <name>springboot-test</name>
  6. <url>http://maven.apache.org</url>

4、设定启动类

启动类,比较的简洁,没有啥继承的类,就是一个普通的main函数。

  1. @SpringBootApplication
  2. @EnableAutoConfiguration
  3. public class ConfigMain{
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigMain.class, args);
  6. }
  7. }

5、测试

通过命令mvn clean package 打包
这里写图片描述

常见错误

出现这个问题,多半是本地仓库有问题,解决的办法,就是修改本地仓库的 <localRepository>D:/maven/apache-maven-3.5.0/storage2</localRepository>路径

  1. ERROR] 28054
  2. java.lang.ArrayIndexOutOfBoundsException: 28054
  3. at org.codehaus.plexus.util.xml.pull.MXParser.parsePI(MXParser.java:2502)
  4. at org.codehaus.plexus.util.xml.pull.MXParser.parseEpilog(MXParser.java:1604)
  5. at org.codehaus.plexus.util.xml.pull.MXParser.nextImpl(MXParser.java:1434)
  6. at org.codehaus.plexus.util.xml.pull.MXParser.next(MXParser.java:1131)

发表评论

表情:
评论列表 (有 0 条评论,420人围观)

还没有评论,来说两句吧...

相关阅读