如何把springboot jar项目 改为war项目


如何把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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM