首先我们先了解标准的步骤:

1.首先 修改pom. 下的打包方式

\"\"

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.spring work.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<!--忽略内嵌tomcat,打包部署到tomcat。注*本地运行的时候要把这一段忽略引入个注释掉,要不然项目启动不了-->
	<exclusions>
		<exclusion>
			<groupId>org.spring work.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: web  attribute is required (or pre-existing WEB-INF/web.  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. 呗。那么我们需要添加这么一段

<build>
	<plugins>
		<plugin>
			<artifactId>maven-war-plugin</artifactId>
			<version>2.6</version>
			<configuration>
				<!--如果想在没有web. 文件的情况下构建WAR,请设置为false。-->
				<failOnMissingWeb >false</failOnMissingWeb >
			</configuration>
		</plugin>
	</plugins>
</build>

还有一种方法是:版本3.0.0的插件 web. 不存在问题,所以可以通过升级插件来解决问题

因为网络问题一直下载不下来所以没试验成功。您也可以试试

<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.Lifecycle .start(Lifecycle .java:162)
        at org.apache.catalina.core.Container .addChildInternal(Container .java:755)
        at org.apache.catalina.core.Container .addChild(Container .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. : org.apache.logg

不需要看那么多啊直接看什么引起的就好:Caused by

很明显tomcat有自己的日志记录,而我的项目中也包含了这个。原因明白了这个好说,排除了就好。我们这么改

<dependency>
	<groupId>org.spring work.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<!--忽略内嵌tomcat,打包部署到tomcat。注*本地运行的时候要把这一段忽略引入个注释掉,要不然项目启动不了-->
		<exclusion>
			<groupId>org.spring work.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</exclusion>
		<!--忽略日志-->
		<exclusion>
			<groupId>org.spring work.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</exclusion>
	</exclusions>
</dependency>

我们添加一段忽略日志的配置就好。

 

然后我这就完美运行了啊。

所以说遇到为不要着急,一定要去寻找出问题的原因。再去解决,不要直接就去网上找。你会被很多人带到坑里面去的。\"\"

收藏 打印