国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php框架 > 框架设计 > Webx框架:AutoConfig

Webx框架:AutoConfig

来源:程序员人生   发布时间:2015-01-14 08:29:21 阅读次数:8241次

AutoConfig

AutoConfig是1个小工具,用于解决不同机器之间配置不同的问题。有时候有些配置,比如数据库地址、缓存地址等,这些配置与环境有关。每次在不同的环境中都需要重新修改配置,比较麻烦。解决这个问题有两种方法,1种是通过maven中的filter工具,另外1种是使用autoconfig。maven-filter工具的缺点是,当项目编译打包以后,没法再修改配置文件。而autoconfig可以通过1个脚本指令,直接修改打包过的配置文件,不需要重新编译,这样节省了很多时间。

接下来介绍用法,包括开发阶段和部署阶段。开发阶段首先在META-INF文件夹中增加autoconf/auto-config.xml文件。下面是该文件的示例内容:

<?xml version="1.0" encoding="UTF⑻"?>
<config>
  <group>
    <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" />
    <generate template="WEB-INF/common/resources.xml" />
  </script>
</config>

下面是每一个标签的解释:

property标签。有name/defaultValue/description/required属性。顾名思义,这里就不赘述了。

validator标签。用于验证参数是不是填写正确。支持的验证器有以下几种。

<validator name="boolean" />
<validator name="choice" choice="trace, debug, info, warn, error" />
<validator name="email" />
<validator name="fileExist" [file="WEB-INF/web.xml"/> 文件必须存在
<validator name="hostExist" /> 合法IP
<validator name="keyword" /> 只能由字母数字下划线组成
<validator name="number" />
<validator name="regexp" regexp="..."/>

generate标签。表示替换该文件中的placeholder。有template/destfile/charset/outputCharset参数。

在maven中加入autoconfig

<build>
    <plugins>
        <plugin>
            <groupId>com.alibaba.citrus.tool</groupId>
            <artifactId>autoconfig-maven-plugin</artifactId>
            <version>1.2</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>
    </plugins>
</build>

部署阶段。首先从网上下载autoconfig工具:

  • antx-autoconfig⑴.2.tgz
  • antx-autoexpand⑴.2.tgz

安装到系统目录,比如/usr/local/bin中。

配置利用包。下面的命令将会修改test.war中的配置文件,将placeholder直代替换成属性值。

autoconfig test.war

autoconfig test.war test-configured.war

如果希望有的placeholder不被替换,可使用${D{myprop}}

同享参数。有些利用使用的相同的参数,就能够通过这个选项,1次配置,多方使用。

autoconfig -s share1.properties,share2.properties test.war

同享参数还可以援用网络上的配置文件,可以有多套默许配置。

其他命令。

autoconfig -i on # 交互模式 
autoconfig -i off
autoconfig -u file.xml # 配置file.xml文件 
autoconfig -c GBK # 设置编码 

AutoExpand

用于嵌套展开1个利用包。

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生