zoukankan      html  css  js  c++  java
  • spring 使用 maven profile

    先看看 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>
    

      

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

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

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

    比如:

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

     怎么才能让 spring一识别 maven的  表达式呢?我们知道 spring运行在maven 打包以后,所以 不能可能spring运行的时候还能读取到 maven的 变量,所以只能是在maven打包的时候替换文件。 这就是核心思想了。加上下面的代码就可以打包的时候替换了spring.yml 中 maven的 变量了 。

        <resources>
    			<resource>
    				<directory>src/main/resources/</directory>
    				<filtering>true</filtering>
    			</resource>
    		</resources>        
    

      

      这个 代码 加在 <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>
    

      

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

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

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

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

  • 相关阅读:
    浅谈样式表QSS的应用
    从一个笑话看软件开发管理(转帖)
    项目的大小衡量标准,项目架构的方法(填空架子,持续集成,边开发边测试效果)(装贴)
    做项目过程一点心得
    什么时候该写函数,什么时候该写类。
    QWidget属性,函数的学习
    Qt 中一些常用类中文说明
    swing应用中如何保存一个全局变量
    Implicit Linking与Explicit Linking
    qt中设置菜单高度
  • 原文地址:https://www.cnblogs.com/cxygg/p/9319888.html
Copyright © 2011-2022 走看看