最近弄個小分佈式的工程,記錄一次整合過程,最後打包成war包部署到Tomcat當中,持續更新
1.項目結構如圖所示
2.pom.xml
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions><!--去掉有衝突的jar包否則無法啓動Tomcat-->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--打包的時候可以不用包進去,別的設施會提供。事實上該依賴理論上可以參與編譯,測試,運行等週期。
相當於compile,但是打包階段做了exclude操作-->
<scope>provided</scope>
</dependency>
<!-- 熱部署配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency> -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.0</version>
</dependency>
</dependencies>
<!--id 與文件夾的名稱對應-->
<profiles>
<profile>
<id>dev</id> <!--開發環境-->
<properties>
<deploy.type>dev</deploy.type>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>pro</id> <!--生產環境-->
<properties>
<deploy.type>pro</deploy.type>
</properties>
</profile>
<profile>
<id>test</id> <!--測試環境-->
<properties>
<deploy.type>test</deploy.type>
</properties>
</profile>
</profiles>
<build>
<finalName>springboot</finalName><!--打包後的項目名稱-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 打包資源 排除 -->
<excludes>
<exclude>test/*</exclude>
<exclude>pro/*</exclude>
<exclude>dev/*</exclude>
</excludes>
</resource>
<resource>
<!-- ${deploy.type} 對應 profile deploy.type -->
<directory>src/main/resources/${deploy.type}</directory>
</resource>
</resources>
</build>
3.啓動類
/**
* 以下配置是爲了啓動springboot,並且可以打包部署到tomcat當中
*/
@EnableScheduling
@SpringBootApplication
public class App extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(App.class);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
4.打包(profiles中的參數爲test或者pro,爲了根據不同環境打包不同的配置文件)