先看看 maven 定义 profile 的写法 

 

<!-- profiles -->
	<profiles>
		<profile>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<id>dev</id>
			<properties>
				<profile.env>dev</profile.env>
			</properties>
		</profile>
		<profile>
			<id>test</id>
			<properties>
				<profile.env>test</profile.env>
			</properties>
		</profile>
		<profile>
			<id>product</id>
			<properties>
				<profile.env>product</profile.env>
			</properties>
		</profile>
	</profiles>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

  

 

然后在 spring.yml 里面这个么写

 

spring 使用 maven profile_jar

 

 

 但是 spring 在运行的时候 已经和maven 没有关系了 这时候 ${ 变量名的写法 } 已经不生效了

 

备注: 这里 ${ 变量名 } 是maven 引用 变量的 写法。  

比如:

spring 使用 maven profile_jar_02

 

 这里的  ${serverA.version}_${profile.env}    分别是引用自 前面配置文件 中定义的  maven 变量   ${serverA.version} 来自  <properties>    ${profile.env}  来自 profile

 

spring 使用 maven profile_jar_03

 

我们知道 spring运行在maven 打包以后,所以 不能可能spring运行的时候还能读取到 maven的 变量,所以只能是在maven打包的时候替换文件。

 

<resources>
			<resource>
				<directory>src/main/resources/</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

  

  这个 代码 加在 <build> 里面就可以了

 

<!-- 指定jdk 的版本 -->
	<build>
		<resources>
			<resource>
				<directory>src/main/resources/</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

  

 

上面 多了一个 修改  jdk  版本的 配置,我的另一篇 博客里面写了 修改 jdk 版本的  maven的  2 中配置方式。

 

如果需要零时切换  profile  可以在 jar 后面 更上  --spring.profiles.active=dev

 

 

java -jar aaa.jar  --spring.profiles.active=dev

 

 

在 eclipse 中   run  右键  运行配置中指定

 

spring 使用 maven profile_spring_04