autoconfig是阿里巴巴提供的一款开源web项目配置工具,源代码svn地址为 http://code.taobao.org/svn/webx/citrus-tool/trunk/。在checkout的源代码的根目录下运行 mvn clean install -Dmaven.test.skip。因为autoconfig使用maven build,安装以前必须先安装maven。html
提供项目配置文件中的placeholder(¥{****})占位符替换功能 ,支持编译时替换,运行时替换。java
例如在webx中定义的placeholder
web
<services:webx-configuration />正则表达式
<services:webx-configuration >app
<services:productionMode> ${productionMode:true}</services:productionMode>框架
</services:webx-configuration> webapp
在启动时能够加入 jvm 参数:-productionMode=false|true 指定使用哪种模式,默认值为true 在${productionMode:true}中已经设置了默认值。jvm
autoconfig优势:maven
配置文件是静态的,只须要在编写时加入占位符。在具体时环境中替换不一样的参数自己不须要修改。 svn
能够随时改变参数的值,只须要在启动时指定不一样的jvm参数或指定不一样的properties文件便可。
配置文件与程序分离,不须要在程序中加入特定逻辑,低耦合便于应用。
不足:
并非全部的框架都支持autoconfig
支持该技术的框架各有不一样的用法。例如:Spring和Log4j都支持placeholder替换,然则它们的作法是彻底不一样的。Spring经过PropertyPlaceholderConfigurer
类来配置,而Log4j则须要在DomConfigurator
中把参数传进去。
autoconfig 使用:
1:集成在maven中
在maven中加入plugins 做为一个外部插件集成 加入配置以下:
<plugin> <groupId>com.alibaba.citrus.tool</groupId> <artifactId>autoconfig-maven-plugin</artifactId> <version>${autoconfig-plugin-version}</version> <configuration> <!-- 要进行AutoConfig的目标文件,默认为${project.artifact.file}。 <dest>${project.artifact.file}</dest> --> <!-- 配置后,是否展开目标文件,默认为false,不展开。 <exploding>true</exploding> --> <!-- 展开到指定目录,默认为${project.build.directory}/${project.build.finalName}。 <explodedDirectory> ${project.build.directory}/${project.build.finalName} </explodedDirectory> --> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>autoconfig</goal> </goals> </execution> </executions> </plugin>
每次mvn package 或 mvn install 执行时都会自动引用 autoconfig 根据auto-config.xml配置的信息进行替换。
2:独立使用
autoconfig-1.2.tgz
根据提示安装完成,最简单操做 autoconfig test.war
autoconfig 配置:
核心配置文件:auto-config.xml 默认文件位置/src/main/resources/META-INF/autoconf/auto-config.xml
。或/META-INF/autoconf/auto-config.xml(是否能够修改还不知道,大神们知道的求指导)
autoconfig结构
---第一行xml格式,即制定编码格式
<?xml version="1.0" encoding="UTF-8"?>
<config>
<group>
-- 组册属性,每个用到的属性(${***})都须要在这里注册,property 完整格式
<property
name="..." ---名称 [defaultValue="..."] --默认值 [description="..."] --描述 [required="true|false"] --是否必须 > <validator name="..." /> --验证规则 <validator name="..." />-- 能够有多个 ... </property>
********************************************
<property name="petstore.work" description="应用程序的工做目录" />
<property name="petstore.loggingRoot" defaultValue="${petstore.work}/logs" description="日志文件目录" />
<property name="petstore.upload" defaultValue="${petstore.work}/upload" description="上传文件的目录" />
<property name="petstore.loggingLevel" defaultValue="warn" description="日志文件级别">
<validator name="choice" choice="trace, debug, info, warn, error" />
</property> </group> <script> <generate template="WEB-INF/web.xml" />
--指定对那些.xml 进行替换 <generate template="WEB-INF/common/resources.xml" /> </script> </config>
autoconfig 属性(auto-config.xml中的property)中的验证规则
<validator name="boolean" /> 取值只能时 false 或 true
<validator name="chioce" chioce="trace,debug,info,warn,error"/> 取值只能是限定的chioce选项
<validator name="emial"/> 邮件格式验证
<validator name="fileExist" [file="WEB-INF/web.xml ,WEB-INF/conf/"] /> 配置的文件或路径必须已经存在, file是可选项用于配置选择文件或文件夹的位置
<validator name="hostExist" /> 值必须为合法的IP地址,或者能够解析获得的域名。
<validator name="keyword" /> 值必须为字母、数字、下划线的组合。
<validator namae="number" /> 值必须是munber
<validator name="regexp" regexp="..." [mode="exact|prefix|contain"] />。
值必须符合regexp所指的正则表达式。
其中,mode为匹配的方法:
彻底匹配exact
前缀匹配prefix
包含contain
如未指定mode,默认mode为contain
<validator name="url" [checkHostExist="false",protocols="http,https",endsWithSlash="true"] 指定是否验证主机,限制协议,假如指定了endsWithSlash=true
,那么URL必须以/结尾。
auto-config 中generate的配置选项
<generate
template="..." --指定须要修改的文件 [destfile="..."] --修改后的文件名 默认与原文件同名 [charset="..."] -- 指定读取文件的编码格式 若是是xml 或读取xml配置charset编码格式 ,对其它文件必须指定charset
[outputCharset="..."] --输出文件编码格式 默认和原文件相同编码格式 >
定义完auto-config.xml
描述文件之后,就能够建立模板了。模板放在哪里呢?举例说明。
例 13.9. 模板文件的位置
假设在一个典型的WEB应用中,你的auto-config.xml
中包含指定了以下模板:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<group>
...
</group>
<script>
<generate template="WEB-INF/classes/file1.xml" /> <generate template="WEB-INF/classes/file2.xml" /> <generate template="WEB-INF/file3.xml" /> </script> </config>
那么,你能够把file1.xml
、file2.xml
、file3.xml
放在下面的位置:
war-project(源目录结构) -> war-project.war(目标目录结构)
│ pom.xml
│
└─src
└─main
├─java
├─resources -> /WEB-INF/classes │ file1.xml file1.xml - 建议放在这里 │ file2.xml file2.xml - 建议放在这里 │ └─webapp ├─META-INF │ └─autoconf │ │ auto-config.xml │ │ │ └─WEB-INF -> /WEB-INF │ │ file3.xml file3.xml - 也能够放在这里 │ │ │ └─classes -> /WEB-INF/classes │ file1.xml file1.xml - 也能够放在这里 │ file2.xml file2.xml - 也能够放在这里 │ └─WEB-INF -> /WEB-INF file3.xml file3.xml - 建议放在这里
AutoConfig的寻找模板的逻辑是:
若是在auto-config.xml
所在的目录下发现模板文件,就使用它;
不然在包的根目录中查找模板文件;若是两处均未找到,则报错。
书写模板是很简单的事,你只要:
把须要配置的点替换成placeholder:“${property.name}
”。固然,你得确保property.name被定义在auto-config.xml
中。
假如模板中包含不但愿被替换的运行时的placeholder“$
{...}
”,须要更改为“${D}
{...}
” 。
例 13.10. 模板示例
...
<context-param>
<param-name>loggingRoot</param-name>
<param-value>${petstore.loggingRoot}</param-value>
</context-param>
<context-param>
<param-name>loggingLevel</param-name>
<param-value>${petstore.loggingLevel}</param-value> </context-param> ... ${D}{runtime.placeholder}
此外,AutoConfig模板实际上是由Velocity模板引擎来渲染的。所以,全部的placeholder必须可以经过velocity的语法。
参考网址:http://www.openwebx.org/docs/autoconfig.html