maven+springboot打包
在spring打包时其实是用的maven的环境参数,springboot默认有自己的环境配置,springboot默认找自己路劲下的配置文件
具体看org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#loadForFileExtension类中的加载和实现
maven的相关操作是将文件打包到springboot需要的路径下,这样可以在不同的环境下可以使用不同的配置文件。
具体的maven配置如下
<profiles>
<profile>
<id>dev</id>
<properties>
<spring.atcive>dev</spring.atcive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>pro</id>
<properties>
<spring.atcive>pro</spring.atcive>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<!--指定资源路径-->
<directory>${basedir}/profiles</directory>
<!--这个声明是否需要替换-->
<filtering>true</filtering>
<includes>
<!--指定打包时候要包含的文件,这里的变量在打包时候会被替换-->
<include>application-${spring.atcive}.yml</include>
</includes>
<targetPath>${basedir}/target/classes</targetPath>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<!--指定打包要排除的文件-->
<exclude>application.yml</exclude>
</excludes>
<includes>
<!--指定打包要包含的文件-->
<include>bootstrap.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果开发工具可以选择对应的profile项,如果是用命令执行可以参考如下maven命令, -P pro就是告诉maven要使用名字为pro的profile来打包,即所有的配置文件都使用生产环境(prod是自己定义的,在这里自定义为生产环境)。
//指定打包生产环境配置
mvn clean package -Dmaven.test.skip=true -P prod
maven结合springboot的profile功能
maven支持profile功能,当使用maven profile打包时,可以打包指定目录和指定文件,且可以修改文件中的变量。spring boot也支持profile功能,只要在application.properties文件中指定spring.profiles.active=xxx 即可,其中xxx是一个变量,当maven打包时,修改这个变量即可。
常见问题
打包后的jar不能执行
配置打包插件
<plugins>
<!-- Spring boot maven 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
clean package之后
执行启动java -jar project.jar时候报错说“没有主清单属性”,调整项目的打包
<plugins>
<!-- Spring boot maven 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--repackage:创建一个自动可执行的jar或war文件。它可以替换常规的artifact,或者用一个单独的classifier附属在maven构建的生命周期中。-->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
执行clean package
打包后的编译文件不是自己想要的
这个可以调整maven的配置,看看是不是自己没有完全理解配置文件中的配置的意思。
<build>
<resources>
<resource>
<!--指定资源路径-->
<directory>${basedir}/profiles</directory>
<!--这个声明是否需要替换-->
<filtering>true</filtering>
<includes>
<!--指定打包时候要包含的文件,这里的变量在打包时候会被替换-->
<include>application-${spring.atcive}.yml</include>
</includes>
<targetPath>${basedir}/target/classes</targetPath>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<!--指定打包要排除的文件-->
<exclude>application.yml</exclude>
</excludes>
<includes>
<!--指定打包要包含的文件-->
<include>bootstrap.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
maven+普通spring项目
打包为war包,操作和上述的一样。
<build>
<finalName>cn-appoint</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--maven打包的插件 start-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archiveClasses>true</archiveClasses>
</configuration>
</plugin>
<!--maven打包的插件 end-->
</plugins>
</build>
<profiles>
<profile>
<!--开发环境-->
<id>development</id>
<build>
<filters>
<filter>profiles/filter-development.properties</filter>
</filters>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!--预发环境-->
<id>prerelease</id>
<build>
<filters>
<filter>profiles/filter-prerelease.properties</filter>
</filters>
</build>
</profile>
<profile>
<!--生产环境-->
<id>production</id>
<build>
<filters>
<filter>profiles/filter-production.properties</filter>
</filters>
</build>
</profile>
</profiles>