profile是什么?为什么需要profile?
在实际的项目开发中,我们往往需要根据不同的环境进行打包资源,打测试环境的包时需要加入测试环境的配置文件,比如数据库的连接信息等等,打生产环境的包时,需要将生产环境的配置文件打进包内。我们可以人工来处理这些配置文件,比如测试环境和生产环境共用一个数据库连接信息配置文件,那么在打两种环境的包时,就必须手动更改数据库连接信息,想想也是够了,超级麻烦,还容易出错,而有了profile功能后,一切会变得如此简单。
首先,需要在maven的pom文件中定义profile,如下图:
<profiles>
<profile>
<!-- 开发环境 -->
<id>develop</id>
<properties>
<profiles.active>develop</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>product</id>
<properties>
<profiles.active>product</profiles.active>
</properties>
</profile>
</profiles>
在这里,定义了三个profile,develop,test,product分别代表开发环境、测试环境、生产环境,使用maven打包时,指定参数,如:mvn package -P develop,这时,maven就会激活id为develop的profile,如果参数为test,那么就会激活id为test的profile,product也是如此,如果我们不指定参数,那么maven会激活id为develop的profile,因为我们在develop的profile中把activeByDefault属性设置为了true。
假设此时我们通过mvn package -P product来打包,那么生产环境的profile被激活,profiles.active的属性值现在是product,那么接下来的工作就是告诉Springboot哪个环境的配置文件将被激活。
我们先来了解下Spring Boot的profile配置。Profile是spring用来针对不同的环境对不同的配置提供支持的,全局Profile配置使用application-{profile}.properties,如application-product.properties,通过在application.properties中设置spring.profiles.active=product来指定活动的Profile。(application.properties是针对所有环境的公共配置)
在application.properties公共配置文件中设置:spring.profiles.active=@profiles.active@这里的profiles.active要与maven中配置的profiles.active属性名一样,这样,当我们执行mvn package -P product命令时maven就会帮我们将@profiles.active@替换成指定的profile,这里即product。
接下来spring boot就会激活application-product.properties的配置文件了。
至此,还没完毕,我们需要将此生产环境的配置打进包内。如下:
<build>
<finalName>${artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<!--resource的filtering属性用来表示资源文件中的占位符是否需要被替换,true为需要替换-->
<filtering>true</filtering>
<!--引入资源文件信息-->
<includes>
<include>application.properties</include>
<include>log4j.properties</include>
<include>banner.txt</include>
<include>application-${profiles.active}.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<!-- 引入Maven插件重写manifest -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<!--第五步,编写程序运行额主入口信息,该信息必须位于web模块下的main方法要放到目录外层,根据约定哦,-->
<mainClass>com.handu.hapm.config.CoreApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
在这里我们用到了一个插件:org.springframework.boot:spring-boot-maven-plugin 插件
在添加了该插件之后,当运行“mvn package”进行打包时,会打包成一个可以直接运行的 JAR 文件,使用“Java -jar”命令就可以直接运行。这在很大程度上简化了应用的部署,只需要安装了 JRE 就可以运行。
你还可以指定要执行的类,如果不指定的话,Spring会找有这个【public static void main(String[] args)】方法的类,当做可执行的类。
如果你想指定的话,可以用下面两个方法:
1,如果你的POM是继承spring-boot-starter-parent的话,只需要下面的指定就行。
<properties>
<!-- The main class to start by executing java -jar -->
<start-class>com.handu.hapm.config.CoreApplication</start-class>
</properties>
2,如果你的POM不是继承spring-boot-starter-parent的话,那么就需要如上mainClass的方式。