Webx使用Autoconfig总结

1,860 阅读1分钟

Webx使用Autoconfig总结

1. Webx使用placeholder两种方法

1.1 Maven Filter

Maven Filtering是一个build时进行的过程,与采用的技术和框架无关, 在build结束后,替换目标其中的placeholders,在pom.xml配置如下:

<build>
    <filters>
        <filter>${user.home}/antx.properties</filter> 
    </filters>
    <resources>
        <resource>
            <directory>src/main/resources</directory> 
            <includes>
                <include>**.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**.xml</exclude>
            </excludes>
        </resource>
    </resources>
		...
</build>

1.2 Autoconfig

Autoconfig是一种类似于Maven Filter的工具,与采用的技术和框架无关。也可以把Autoconfig看作是Maven Filter的替代品,但也是有区别的。

1.2.1 Autoconfig配置

Autoconfig核心为:建立auto-config.xml描述文件,auto-config.xml文件所在的位置在/META-INF/autoconf/auto-config.xml,主要包含两部分内容:

  • 定义properties
  • 指定模板文件

auto-config.xml配置文件内容

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<group>
        <property name="..." [defaultValue="..."] [description="..."]
                  [required="true|false"] >
            <validator name="..." />
            <validator name="..." />
            ...
		</property>
		<property name="buc_hsf_version" defaultValue="1.0.0.daily"
			description="xxx" />
    	...
	</group>
	
	<group>
		<property name="masterdata.server.address" 
                  defaultValue="http://10.125.13.210:9600" description="xxx" />
		<property name="masterdata.client.clientid" 
                  defaultValue="jicaiApp" description="xxx" />
		...
    </group>
	
	<script>
        <generate template="component-beans.xml.vm" destfile="component-beans.xml" 
                  charset="UTF-8" />
        <generate template="ceres-hsf.xml.vm" destfile="beans/ceres-hsf.xml"
                  charset="UTF-8" />
        ...
	</script>
</config>

文件目录结构

Autoconfig是使用Velocity模板引擎来渲染,书写按照Velocity规则,把需要替换的placholder写成"``${placeholder}``",如果模板中包含不想被替换的${xxx},书写成${D}{xxx}.
  【注意】在auto-config.xml定义的,因为Velocity不支持用"."作为命名,在需要被替换的模板中需要书写成"$!{xxx_xxx_xxx}"

1.2.2 在maven中调用AutoConfig plugin

Autoconfig的使用方法有两种:

  • 在命令行中使用AutoConfig
  • 在mavn中调用Autoconfig plugin

这里介绍第二种方法---在maven中调用Autoconfig plugin,在pom.xml配置如下:

<profiles>
		<profile>
			<build>
				<plugins>
					<plugin>
						<groupId>com.alibaba.citrus.tool</groupId>
						<artifactId>autoconfig-maven-plugin</artifactId>
						<version>1.2</version>
						<executions>
							<execution>
								<phase>package</phase>
								<goals>
									<goal>autoconfig</goal>
								</goals>
							</execution>
						</executions>
						<configuration>
							<exploding>true</exploding>
						</configuration>
					</plugin>
				</plugins>
			</build>
		</profile>
	</profiles>

1.2.3  在Webx中定义placeholders

在webx-xxx.xml中使用placeholder替换

<!-- 支持${xxx}替换。 -->
<services:property-placeholder>
    ...
    <property key="component">common</property>
    ...
</services:property-placeholder>

1.2.4 auto-config.xml 与 antx.properteis

  1. 若 auto-config.xml 有新增 key-value,则会同步到 antx.properties 中
  2. 若 antx.properties 中更改 value 值,不会被 auto-config.xml 中原有信息覆盖
  3. 若 auto-config.xml 中更改 value 值 ,不会同步到 antx.properties 中

1.2.5 Autoconfig部署

在buid时使用 mvn install``-Dautoconfig.userProperties=D:/alibaba/pur_ceres/antx_win.properties来指定文件所在位置

2 Maven Filter 与 Autoconfig对比

Maven Filter Autoconfig
文件中palceholder替换 需要获取源码重新build时替换源文件 不需要获取源码,即可对文件中placeholder进行替换,配置时不依赖于项目源文件
placeholder值的验证机制 在auto-config.xml中配置对应的property验证规则