springboot打war包發(fā)布

1. 將SpringBoot的項(xiàng)目的打包方式設(shè)置為war

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

2. 移除內(nèi)嵌的tomcat模塊,但是為了我們?cè)诒緳C(jī)測(cè)試方便,我們還需要引入它,所以配置如下

<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. 修改入口方法 繼承一個(gè)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插件,用來(lái)自定義打包以后的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. 修改項(xiàng)目的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>
 * 本地配置文件加載
 * 如果存在系統(tǒng)變量DEMO_CONFIG_FILE,且有值則使用該值中指定的配置文件
 * 如果未指定DEMO_CONFIG_FILE系統(tǒng)變量或該變量的值為空,
 * 則根據(jù)操作系統(tǒng)讀取"D:/DEMO/application.properties"或"/DEMO/application.properties"的配置文件
 * 如果最終都未讀取到任何外部配置文件,則使用jar包中的默認(rèn)配置
 * </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");// 從系統(tǒng)環(huán)境變量中讀取運(yùn)營(yíng)中心的外部配置文件路徑
        System.out.println("外部配置文件:"+tmp);
        if (!StringUtils.isEmpty(tmp)) {// 系統(tǒng)環(huán)境中有配置,則使用系統(tǒng)環(huán)境中配置的配置文件
            locationApplicationFile = tmp;
        } else { // 當(dāng)前系統(tǒng)中為指定配置文件,則根據(jù)操作系統(tǒng)到指定目錄去讀取
            String os = System.getProperty("os.name");
            if (!os.toLowerCase().startsWith("win")) {
                // 當(dāng)前為linux系統(tǒng)
                locationApplicationFile = "/DEMO/application.properties";
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("使用可能的外部配置文件: " + locationApplicationFile);
        }
        File file = new File(locationApplicationFile);
        if (file.exists()) {// 外部配置文件存在,則優(yōu)先使用外部配置文件的配置
            MutablePropertySources propertySources = environment.getPropertySources();
            Properties localProperties = loadProperties(file);// 讀取外部配置文件的配置信息
            if (log.isDebugEnabled()) {
                log.debug("\r\n######################\r\n外部配置文件內(nèi)容為:\r\n" + localProperties.toString()
                        + "\r\n######################\r\n");
            }
            propertySources.addFirst(new PropertiesPropertySource("LocalConfig", localProperties));// addFirst表示如果存在同名配置屬性,優(yōu)先使用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);
        }
    }

}

參考:
http://www.itdecent.cn/p/d5f3223ca904

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容