当前位置:   article > 正文

springboot利用maven打包成war,部署在tomcat教程_maven-war-plugin配置

maven-war-plugin配置

一、在pom.xml中做如下修改:

(一)、添加packaging

<packaging>war</packaging>

(二)、添加相关插件依赖

  1. <build>
  2. <!--打包名字-->
  3. <finalName>kunyuan</finalName>
  4. <plugins>
  5. <plugin>
  6. <groupId>org.apache.maven.plugins</groupId>
  7. <artifactId>maven-compiler-plugin</artifactId>
  8. <configuration>
  9. <source>1.8</source>
  10. <target>1.8</target>
  11. </configuration>
  12. </plugin>
  13. <plugin>
  14. <groupId>org.apache.maven.plugins</groupId>
  15. <artifactId>maven-war-plugin</artifactId>
  16. <version>3.2.0</version>
  17. <!--打war包时WEB-INF/web.xml报错要在下面添加以下配置-->
  18. <configuration>
  19. <failOnMissingWebXml>false</failOnMissingWebXml>
  20. </configuration>
  21. </plugin>
  22. </plugins>
  23. </build>

 (三)、添加启动依赖

  1. <!-- 这个依赖让你能够在程序入口类:xxxAppAplication中直接执行main方法启动tomcat -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-tomcat</artifactId>
  5. <!-- 但是这里一定要设置为provided -->
  6. <scope>provided</scope>
  7. </dependency>

(四)、排除springboot自带的tomcat

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <version>2.7.7</version>
  5. <scope>runtime</scope>
  6. <exclusions>
  7. <exclusion>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-tomcat</artifactId>
  10. </exclusion>
  11. </exclusions>
  12. </dependency>

本人测试完整的pom.xml文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com</groupId>
  7. <artifactId>kunyuan</artifactId>
  8. <version>1.0.0</version>
  9. <packaging>war</packaging>
  10. <dependencies>
  11. <dependency>
  12. <groupId>com.baomidou</groupId>
  13. <artifactId>mybatis-plus-boot-starter</artifactId>
  14. <version>3.5.3.2</version>
  15. <scope>runtime</scope>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.baomidou</groupId>
  19. <artifactId>mybatis-plus-extension</artifactId>
  20. <version>3.5.3.2</version>
  21. <scope>compile</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>mysql</groupId>
  25. <artifactId>mysql-connector-java</artifactId>
  26. <scope>runtime</scope>
  27. <version>8.0.28</version>
  28. </dependency>
  29. <!-- Jackson -->
  30. <dependency>
  31. <groupId>com.fasterxml.jackson.core</groupId>
  32. <artifactId>jackson-databind</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.apache.httpcomponents</groupId>
  36. <artifactId>httpclient</artifactId>
  37. <version>4.5.13</version>
  38. <scope>runtime</scope>
  39. </dependency>
  40. <dependency>
  41. <groupId>joda-time</groupId>
  42. <artifactId>joda-time</artifactId>
  43. <version>2.9.1</version>
  44. <scope>runtime</scope>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-starter-aop</artifactId>
  49. <scope>runtime</scope>
  50. </dependency>
  51. <!-- 引入log4j日志依赖 -->
  52. <dependency>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-starter-log4j</artifactId>
  55. <version>1.3.8.RELEASE</version>
  56. <exclusions>
  57. <exclusion>
  58. <groupId>ch.qos.reload4j</groupId>
  59. <artifactId>reload4j</artifactId>
  60. </exclusion>
  61. <exclusion>
  62. <groupId>org.slf4j</groupId>
  63. <artifactId>slf4j-reload4j</artifactId>
  64. </exclusion>
  65. </exclusions>
  66. </dependency>
  67. <dependency>
  68. <groupId>org.springframework.boot</groupId>
  69. <artifactId>spring-boot-starter-web</artifactId>
  70. <version>2.7.7</version>
  71. <scope>runtime</scope>
  72. <exclusions>
  73. <exclusion>
  74. <groupId>org.springframework.boot</groupId>
  75. <artifactId>spring-boot-starter-tomcat</artifactId>
  76. </exclusion>
  77. </exclusions>
  78. </dependency>
  79. <dependency>
  80. <groupId>org.springframework.boot</groupId>
  81. <artifactId>spring-boot-autoconfigure</artifactId>
  82. <version>2.7.7</version>
  83. </dependency>
  84. <dependency>
  85. <groupId>org.springframework</groupId>
  86. <artifactId>spring-web</artifactId>
  87. <version>5.3.24</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>com.alibaba</groupId>
  91. <artifactId>fastjson</artifactId>
  92. <version>1.2.83_noneautotype</version>
  93. </dependency>
  94. <!-- 这个依赖让你能够在程序入口类:xxxAppAplication中直接执行main方法启动tomcat -->
  95. <dependency>
  96. <groupId>org.springframework.boot</groupId>
  97. <artifactId>spring-boot-starter-tomcat</artifactId>
  98. <!-- 但是这里一定要设置为provided -->
  99. <scope>provided</scope>
  100. </dependency>
  101. <!-- Spring Web -->
  102. <dependency>
  103. <groupId>org.springframework</groupId>
  104. <artifactId>spring-webmvc</artifactId>
  105. </dependency>
  106. </dependencies>
  107. <build>
  108. <!--打包名字-->
  109. <finalName>kunyuan</finalName>
  110. <plugins>
  111. <plugin>
  112. <groupId>org.apache.maven.plugins</groupId>
  113. <artifactId>maven-compiler-plugin</artifactId>
  114. <configuration>
  115. <source>1.8</source>
  116. <target>1.8</target>
  117. </configuration>
  118. </plugin>
  119. <plugin>
  120. <groupId>org.apache.maven.plugins</groupId>
  121. <artifactId>maven-war-plugin</artifactId>
  122. <version>3.2.0</version>
  123. <!--打war包时WEB-INF/web.xml报错要在下面添加以下配置-->
  124. <configuration>
  125. <failOnMissingWebXml>false</failOnMissingWebXml>
  126. </configuration>
  127. </plugin>
  128. </plugins>
  129. </build>
  130. <dependencyManagement>
  131. <dependencies>
  132. <dependency>
  133. <groupId>org.springframework.cloud</groupId>
  134. <artifactId>spring-cloud-dependencies</artifactId>
  135. <version>Hoxton.SR3</version>
  136. <scope>import</scope>
  137. <type>pom</type>
  138. </dependency>
  139. <dependency>
  140. <groupId>org.springframework.boot</groupId>
  141. <artifactId>spring-boot-dependencies</artifactId>
  142. <version>2.7.7</version>
  143. <scope>import</scope>
  144. <type>pom</type>
  145. </dependency>
  146. </dependencies>
  147. </dependencyManagement>
  148. </project>

二、启动类修改如下:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
  4. import org.springframework.boot.builder.SpringApplicationBuilder;
  5. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  6. @SpringBootApplication(exclude= {SecurityAutoConfiguration.class})
  7. // tomcat部署需要继承SpringBootServletInitializer
  8. public class KunyuanApplication extends SpringBootServletInitializer {
  9. public static void main(String[] args) {
  10. SpringApplication.run(KunyuanApplication.class, args);
  11. System.out.println("系统启动成功!");
  12. }
  13. // tomcat部署重写该方法
  14. @Override
  15. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  16. return builder.sources(KunyuanApplication.class);
  17. }
  18. }

做好以上步骤之后呢就可以在idea中按如下步骤打包成war包啦:

然后就会在我们的项目目录下生成一个target文件夹,里面就包含maven打出来的war包。

三、tomcat部署

(一)、本地搭建号tomcat环境后,把我们的war包丢进webapps下:

(二)、在tomcat的bin下点击startup.bat就可以启动我们的war包啦

(三)、启动成功后,我们就可以测试我们的接口是否能访问,这时候要注意当我们没对tomcat做什么特殊配置的时候,访问接口时需要带上我们war包名才可以访问。

127.0.0.1:8080/war包名/接口名
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/147067?site
推荐阅读
相关标签
  

闽ICP备14008679号