如何把springboot jar項目 改為war項目
https://www.jb51.net/article/174353.htm
http://www.imooc.com/article/21468?block_id=tuijian_wz
這篇文章主要介紹了如何把springboot jar項目 改為war項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
啟動類JeewxBootApplication 添加繼承SpringBootServletInitializer
@SpringBootApplication public class JeewxBootApplication extends SpringBootServletInitializer { public final static Logger log = LoggerFactory.getLogger(JeewxBootApplication.class); public static void main(String[] args) { ConfigurableApplicationContext application = SpringApplication.run(JeewxBootApplication.class, args); Environment env = application.getEnvironment(); String ip = InetAddress.getLocalHost().getHostAddress(); String port = env.getProperty("server.port"); String path = env.getProperty("server.servlet.context-path"); log.info("\n----------------------------------------------------------\n\t" + "Application is running! Access URLs:\n\t" + "Local: \t\thttp://localhost:" + port + path + "/\n\t" + "External: \thttp://" + ip + ":" + port + path + "/\n\t" + "----------------------------------------------------------"); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(JeewxBootApplication.class); } }
pom文件添加插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
pom文件添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency>
https://blog.csdn.net/qq_33689414/article/details/81812761
Spring Boot(三)之jar包改成war包模式
修改pom.xml文件
將jar包改成war包
<!--<packaging>jar</packaging>-->
<packaging>war</packaging>
添加Spring Boot 的tomcat依賴
<!--添加tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
添加Servlet依賴,只在編譯時有效
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
實現SpringBootServletInitializer,重寫configure()方法
@SpringBootApplication
@MapperScan("com.zhang.springbootmybatisdemo.dao")
public class SpringbootMybatisDemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisDemoApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootMybatisDemoApplication.class);
}
}
編譯打包
mvn clean package
可以在target目錄下看到打出war包了。
修改war包和target目錄下項目名
在中使用,指定最后項目名稱和war名稱
<build>
<!-- 最后項目名稱,war包名稱 -->
<finalName>springbootdemo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
再次打包:mvn clean package