首先我們先了解標準的步驟:
1.首先 修改pom.xml下的打包方式
2.添加servlet-api依賴
<!--添加servlet-api的依賴-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
3.去除springboot 內嵌的tomcat模塊
因爲你是使用的外部的tomcat所以這個包還是去掉的好。一是爲了不引起不必要的衝突,二也是可以使打出來的war包小一點
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--忽略內嵌tomcat,打包部署到tomcat。注*本地運行的時候要把這一段忽略引入個註釋掉,要不然項目啓動不了-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
4.還有一步特別的重要那就是【繼承SpringBootServletInitializer並重寫configure這個方法】原因自己查
一種方法是:直接修改啓動類
@SpringBootApplication
public class WxWebApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(WxWebApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WxWebApplication.class);
}
}
第二種就是:在啓動類的同目錄下新建一個ServletInitializer的類繼承SpringBootServletInitializer並重寫configure這個方法
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebApplication.class);
}
}
這兩種方式你選哪種都可以的。
其實到這裏就差不多了,現在你可以試着打包看看。
假如你在打包期間出現了這個錯誤
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.6:war (default-war) on project wjyb-wx-web: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
其實意思很明白,找不到web.xml唄。那麼我們需要添加這麼一段
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!--如果想在沒有web.xml文件的情況下構建WAR,請設置爲false。-->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
還有一種方法是:版本3.0.0的插件 web.xml不存在問題,所以可以通過升級插件來解決問題
因爲網絡問題一直下載不下來所以沒試驗成功。您也可以試試
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
現在應該是打包沒有什麼問題了,現在我們拿着war包去tomcat裏去運行。
這裏交代一下springBoot最低使用的tomcat8.這裏不具體討論需要哪個版本的最好。
如果需要配置支持tomcat7也是有點麻煩的,雖然也是可以。但是兼容什麼的都不好。
現在運行war包後我這又出現了一個錯誤,來看。
ase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wjyb-wx-web-0.0.1-SNAPSHOT]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:755)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:731)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:973)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1849)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/C:/D/apache-tomcat-8.0.51/webapps/wjyb-wx-web-0.0.1-SNAPSHOT/WEB-INF/lib/log4j-slf4j-impl-2.10.0.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.apache.logg
不需要看那麼多啊直接看什麼引起的就好:Caused by
很明顯tomcat有自己的日誌記錄,而我的項目中也包含了這個。原因明白了這個好說,排除了就好。我們這麼改
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--忽略內嵌tomcat,打包部署到tomcat。注*本地運行的時候要把這一段忽略引入個註釋掉,要不然項目啓動不了-->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<!--忽略日誌-->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
我們添加一段忽略日誌的配置就好。
然後我這就完美運行了啊。
所以說遇到爲不要着急,一定要去尋找出問題的原因。再去解決,不要直接就去網上找。你會被很多人帶到坑裏面去的。