1、修改 pom.xml
<groupId>com.example</groupId>
<artifactId>nms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>//新增此行
<name>nms</name>
<description>Demo project for Spring Boot</description>
<!--打包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!--打包-->
<!--测试类 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
2、修改启动类Application.java
@SpringBootApplication
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
// 注意这里要指向原先用main方法执行的Application启动类
return application.sources(Application.class);
}
}
3、点击右侧 Maven Projects菜单,Lifecycle下clean,清除原有的缓存,再点击package打包。
4、打包过程中会执行test,若出现Bean类无法找到的错误,可通过修改工程目录下test文件夹中的主测试类解决。
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class NmsApplicationTests {
@Test
void contextLoads() {
}
}
5、打包完成后可在target文件夹下找到WAR包。通过window命令窗口 java -jar XXX.war可直接运行。