美文网首页
springboot打war包发布

springboot打war包发布

作者: 码农梦醒 | 来源:发表于2017-12-28 10:25 被阅读936次

1. 将SpringBoot的项目的打包方式设置为war

<groupId>com.example</groupId>  
<artifactId>demo</artifactId>  
<version>1.0.0</version>  
<packaging>war</packaging>

2. 移除内嵌的tomcat模块,但是为了我们在本机测试方便,我们还需要引入它,所以配置如下

<dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-web</artifactId>  
        <exclusions>  
            <exclusion>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-starter-tomcat</artifactId>  
            </exclusion>  
        </exclusions>  
</dependency>  
<dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-tomcat</artifactId>  
        <scope>provided</scope>  
</dependency>

3. 添加tomcat-servelt-api依赖

<dependency>  
      <groupId>org.apache.tomcat</groupId>  
      <artifactId>tomcat-servlet-api</artifactId>  
      <version>7.0.42</version>  
      <scope>provided</scope>  
</dependency>

4. 修改入口方法 继承一个SpringBootServletInitializer类,并且覆盖configure方法

package com.pzy.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


@SpringBootApplication(exclude = { JmxAutoConfiguration.class })
@EnableFeignClients
public class StatisticsApplication extends SpringBootServletInitializer {
  
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    return builder.sources(StatisticsApplication.class);
  }

  public static void main(String[] args) {
    SpringApplication.run(StatisticsApplication.class, args);
  }
}

5. 添加war插件,用来自定义打包以后的war包的名称

<plugin>  
      <groupId>org.apache.maven.plugins</groupId>  
      <artifactId>maven-war-plugin</artifactId>  
      <configuration>  
          <warSourceExcludes>src/main/resources/**</warSourceExcludes>  
          <warName>springboot</warName> 
          <failOnMissingWebXml>false</failOnMissingWebXml> 
      </configuration>  
</plugin>

6. 修改项目的context-path与warName一致

server:
  context-path: /springboot

读取外部配置文件:

package com.free.DEMO.web;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.StringUtils;

import lombok.extern.log4j.Log4j;

/**
 * <pre>
 * 本地配置文件加载
 * 如果存在系统变量DEMO_CONFIG_FILE,且有值则使用该值中指定的配置文件
 * 如果未指定DEMO_CONFIG_FILE系统变量或该变量的值为空,
 * 则根据操作系统读取"D:/DEMO/application.properties"或"/DEMO/application.properties"的配置文件
 * 如果最终都未读取到任何外部配置文件,则使用jar包中的默认配置
 * </pre>
 * 
 * @author pan
 *
 */
@Log4j
public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String locationApplicationFile = "D:/DEMO/application.properties";
        String tmp = System.getenv().get("DEMO_CONFIG_FILE");// 从系统环境变量中读取运营中心的外部配置文件路径
        System.out.println("外部配置文件:"+tmp);
        if (!StringUtils.isEmpty(tmp)) {// 系统环境中有配置,则使用系统环境中配置的配置文件
            locationApplicationFile = tmp;
        } else { // 当前系统中为指定配置文件,则根据操作系统到指定目录去读取
            String os = System.getProperty("os.name");
            if (!os.toLowerCase().startsWith("win")) {
                // 当前为linux系统
                locationApplicationFile = "/DEMO/application.properties";
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("使用可能的外部配置文件: " + locationApplicationFile);
        }
        File file = new File(locationApplicationFile);
        if (file.exists()) {// 外部配置文件存在,则优先使用外部配置文件的配置
            MutablePropertySources propertySources = environment.getPropertySources();
            Properties localProperties = loadProperties(file);// 读取外部配置文件的配置信息
            if (log.isDebugEnabled()) {
                log.debug("\r\n######################\r\n外部配置文件内容为:\r\n" + localProperties.toString()
                        + "\r\n######################\r\n");
            }
            propertySources.addFirst(new PropertiesPropertySource("LocalConfig", localProperties));// addFirst表示如果存在同名配置属性,优先使用localProperties中的
        }
    }

    private Properties loadProperties(File f) {
        FileSystemResource resource = new FileSystemResource(f);
        try {
            return PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException ex) {
            throw new IllegalStateException("外部配置文件加载失败: " + f.getAbsolutePath(), ex);
        }
    }

}

参考:
https://www.jianshu.com/p/d5f3223ca904

相关文章

网友评论

      本文标题:springboot打war包发布

      本文链接:https://www.haomeiwen.com/subject/lwemgxtx.html