autoconfig-maven-plugin的使用

        我们的java应用需要可能部署在开发dev环境、测试test环境、uat环境、生产prd环境上,举个例子,加入我们的java应用需要连接mysql数据库的话,那么这四种环境的数据库地址一般也要使用不同的;日志级别各个环境也不一样;缓存配置等都可能不一样,为了避免在不同的环境下我们使用相同配置文件需要修改的麻烦,我们可以给每一种环境使用不同名字的配置文件,打war包时使用对应的配置文件打包上到对应环境就行了,autoconfig-maven-plugin这个maven插件就是干这件事的。

一、看看它的使用,新建个maven web应用test_autoconfig

二、新建applicationContext.xml,放入src/main/resources下面,文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-4.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
			<property name="driverClass" value="com.mysql.jdbc.Driver" />
			<property name="jdbcUrl" value="${url}" />
			<property name="user" value="${user}" />
			<property name="password" value="${pwd}" />
	</bean>
</beans>

可以看到,我虽然使用${xxx}的方式获取值,但是我在这个applicationContext.xml文件里面并没有指定配置文件。

三、这个时候,修改pom,xml,修改后的内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.roadjava</groupId>
  <artifactId>test_autoconfig</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
  	<dependency>
		    <groupId>com.mchange</groupId>
		    <artifactId>c3p0</artifactId>
		    <version>0.9.2.1</version>
		</dependency>
  </dependencies>
  <build>
		<finalName>test_autoconfig</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>utf-8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>com.alibaba.citrus.tool</groupId>
				<artifactId>autoconfig-maven-plugin</artifactId>
				<version>1.2</version>
				<configuration>
					<!-- 不同的环境只需要换成不同的配置文件即可,这里加入是开发环境,自然就使用 
					antx_dev.properties-->
					<userProperties>${user.home}/antx_dev.properties</userProperties>
				</configuration>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>autoconfig</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

这里我们配置了

<configuration>
					<!-- 不同的环境只需要换成不同的配置文件即可,这里加入是开发环境,自然就使用 
					antx_dev.properties-->
					<userProperties>${user.home}/antx_dev.properties</userProperties>
				</configuration>

我们并不需要到这个目录下去新建antx_dev.properties,等会autoconfig这个maven插件会帮我们自己创建。

四、在src/main/webapp/META-INF目录下新建autoconfig目录,autoconfig目录里面新建一个auto-config.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<config description="自动配置" xmlns:j="jelly:core">
	<!-- mvn使用 -->
	
    <group name="basic">
        <property name="domain" defaultValue="www.roadjava.com" description="应用域名" />
        <property name="workdir" defaultValue="/user/local/admin" description="应用部署路径" />
        <property name="app.mode" defaultValue="dev" description="应用的部署状态(prd,test,dev)" />
    </group>
    
    <group name="spec-setting">
        <property name="url" defaultValue="jdbc:mysql://localhost:3306/test_00" description="" />
        <property name="user" defaultValue="root" description="" />
        <property name="pwd" defaultValue="root" description="" />
    </group>
    
       
	<!-- ========================================================== -->
	<!-- 生成配置文件的脚本。 -->
	<!-- ========================================================== -->
	<script>
		<generate template="WEB-INF/classes/applicationContext.xml" charset="UTF-8" />
	</script>
</config>

解释:这个xml里面的标签不是你随便起的,这个xml将会在你对maven工程执行package命令的时候被autoconfig-maven-plugin读取。解释下各个标签的含义:

group:用于给多个property分组

property:用于定义不同的书写,name属性就是key,defaultValue就是默认值

script:template属性用于指定含有"${xxx}"表达式的文件,这里指定的文件之后,里面的“${xxx}”在执行package命令的时候将被替换为xxx对应的defaultValue,如果antx_dev.properties里面的xxx="2",而这里的defaultValue=“1”,那么将使用2替换,这个优先级要知道,意思就是你可以修改生成的antx_dev.properties里面的值。

五、执行package命令吧,看看是个什么效果:

右击pom.xml,run as--->maven build....,如下:

image.png

点击run之后,控制台输出:

[INFO] -------------------------------------------------
Loading file:/C:/Users/zhao/antx_dev.properties
Not exists: file:/C:/Users/zhao/antx_dev.properties
User-defined properties: file:/C:/Users/zhao/antx_dev.properties

╭───────────────────────┈┈┈┈
│
│ 您的配置文件需要被更新:
│
│ file:/C:/Users/zhao/antx_dev.properties
│
│ 这个文件包括了您个人的特殊设置,
│ 包括服务器端口、您的邮件地址等内容。
│
└───────┈┈┈┈┈┈┈┈┈┈┈

 如果不更新此文件,可能会导致配置文件的内容不完整。
 您需要现在更新此文件吗? [Yes][No]

输入“yes”按回车:

您需要现在更新此文件吗? [Yes][No] yes


╭──────┬─ Step 1 of 2 ────────┈┈┈┈
│            │
│Description │ 自动配置
│┈┈┈┈┈┈│┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
│Descriptor  │ jar:file:/D:
│            │   /workspace3/test_autoconfig/target/test_autoconfig.war!
│            │   /META-INF/autoconf/auto-config.xml
│┈┈┈┈┈┈│┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
│Properties  │ file:/C:/Users/zhao/antx_dev.properties
│            │
└──────┴┈┈┈┈┈┈┈┈┈┈┈

 (? - 该值在用户配置文件中不存在,* - 必填项,S - 覆盖共享默认值,s - 共享值)

 * 1 - domain    = www.roadjava.com    # 应用域名
 * 2 - workdir   = /user/local/admin   # 应用部署路径
 * 3 - app.mode  = dev                 # 应用的部署状态(prd,test,dev)

 请选择[1-3][Quit][Next]

输入“quit”按回车:

即将保存到文件"file:/C:/Users/zhao/antx_dev.properties"中, 确定? [Yes][No]

输入“yes”按回车,之后就可以看到打包完成了。

六、那我们去用户目录下去找找是不是有个antx_dev.properties呢?它的内容又是什么呢?如下:

app.mode  = dev

domain   = www.roadjava.com
pwd      = root
url      = jdbc:mysql://localhost:3306/test_00
user     = root
workdir  = /user/local/admin

七、去生成的war包中看看:

image.png

可以看到,war包中的applicationContext.xml中的值已经被替换为pom.xml中使用的配置文件antx_dev.properties了。如果我们需要打包上生产,只需要再拷贝一份antx_dev.properties命名为不同的名字,加入叫做“antx_prd.properties”,并修改响应的配置完成之后,在pom.xml改一个:

<userProperties>${user.home}/antx_prd.properties</userProperties>

就可以了。

八、总结下autoconfig-maven-plugin的工作流程

①、在pom中配置的过程(上面例子中为package)被执行时,autoconfig也被触发,autoconfig会去META-INF/autoconfig目录下找一个叫做auto-config.xml的文件。

②。找到该文件里面script标签的template属性对应的文件,autoconfig会替换这个文件里面的“${xxx}”,首先用“<userProperties>${user.home}/antx_dev.properties</userProperties>”中userProperties配置的文件来替换,如果该文件不存在(autoconfig会自动创建它),或者没有相应的key,则使用auto-config.xml里面该key对应的defaultValue来替换。

③、最终打包完成的war包就是经过autoconfig替换过以后的文件。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值