maven auto-config 多环境自动打包

来源:互联网 发布:淘宝店铺怎么设置限购 编辑:程序博客网 时间:2024/05/01 11:42

maven有许多优秀的插件,最近在研究打包中替换多重环境的配置,同事介绍使用阿里的auto-config,使用了一下确实不错。

auto-config其实和ant时代的字符替换差不多太多,只是ant需要自己写脚本完成这些事情,但是auto-config通过配置就可以了。

auto-config的核心技术我个人认为是volicty的模板技术,包括里面的占位符oplacehold都是以${}volicty的语法定义的。

auto-config主要涉及的文件有以下部分:auto-config.xml,template.vm,antx.property.

auto-config.xml主要定义那些字符需要替换,他们的默认值是什么,那  些文件需要替换占位符,以及输出的文件。

template.vm 主要定义模板文件,其实就是我们需要的配置文件,只不过这里需要替换的符号都以占位符的形 式输出。

antx.property 定义了占位符中 的具体的值用于替换占位符的值。

如果使用auto-config:

1.安装maven

2.使用auto-config的插件,需要在pom.xml中plugin中插入,后续会详细介绍,这里有必要说一下大陆的长城防火墙,太厉害了,maven中央仓库下载一个jar包需要半个小时,这里可以再maven的setting文件的mirrors增加如下配置:、

<mirror>
  <id>nexus-osc</id>
  <mirrorOf>external:*</mirrorOf>
  <name>Nexus osc</name>
  <url>http://maven.oschina.net/content/groups/public/</url>
</mirror>
  </mirrors>

表示从oschina这个站下载所有的maven文件。

pom.xml增加auto-config的配置

<build>
<finalName>json</finalName>
<plugins>
<plugin>
<groupId>com.alibaba.citrus.tool</groupId>
<artifactId>autoconfig-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<userProperties>${user.dir}/${autoconfig.properties}</userProperties>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>autoconfig</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

这里的配置也是网络上download下来的,我手动添加了一个配置

<configuration>
<userProperties>${user.dir}/${autoconfig.properties}</userProperties>
</configuration>
这个用来干嘛的呢?用于自定义antx.property的文件位置,为什么要自定义文件位置?原因是方便在多环境中指定参数来修改配置。

这里user.dir是当前工程的路径,autoconfig默认的文件位置是在user.home目录下的。


这里说了,在多环境需要修改配置,所以需要在pom.xml中配置多个环境的profile

<profiles>
<profile>
<id>dev</id>
<properties>
<autoconfig.properties>antx-dev.properties</autoconfig.properties>
</properties>
</profile>
<profile>
<id>deploy</id>
<properties>
<autoconfig.properties>antx-pro.properties</autoconfig.properties>
</properties>
</profile>
</profiles>

<profiles>
<profile>
<id>dev</id>
<properties>
<autoconfig.properties>antx-dev.properties</autoconfig.properties>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<autoconfig.properties>antx-pro.properties</autoconfig.properties>
</properties>
</profile>
</profiles>

在实际的编译过程中只需要 mavn package -P dev或者 mavn package -P pro就可以打包生产或者开发下的包了

pom.xml定义好了之后,需要在WEB-INF目录下面增加一个META-INF的文件夹以及在该文件夹下新增autoconf文件夹

在autoconf文件夹下放置auto-config.xml以及所有的模板文件,注意模板文件必须放在这里,我目前没有找到放到其他位置的配置。


auto-config的具体配置:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<group>
<property name="petstore.work" description="应用程序的工作目录" />
<property name="petstore.loggingRoot" defaultValue="${petstore.work}/logs"
description="日志文件目录" required="true"/>
<property name="petstore.upload" defaultValue="${petstore.work}/upload"
description="上传文件的目录" />
<property name="petstore.loggingLevel" defaultValue="warn"
description="日志文件级别">
</property>
</group>
<script>
<generate template="web.vm" destfile="WEB-INF/classes/spring-web.xml" charset="UTF-8"/>
</script>
</config>

这里property定义的名字都是占位符的名字,defaultvalue是默认值

script下面定义的具体的模板,以及输出的文件。


web.vm是模板文件定义了,也就是我们的配置文件,变量通过占位符分割

<?xml version="1.0" encoding="UTF-8"?>
<config>
<property name=${petstore.work}></property>
<property name=${petstore.loggingRoot}></property>
<property name=${petstore.upload}></property>
<property name=${petstore.loggingLevel}></property>
</config>

antx.property是具体配置的落地,可以有多个,通过mvn -p 制定具体的配置文件

petstore.work=1
petstore.loggingRoot=2
petstore.upload=3
petstore.loggingLevel=4

如果没有在pom。xml 通过configuration配置默认的antx.properties是在项目的根目录下,或者在user.home下的,如果没有autoconfig需要你创建。


通过修改四个文件基本上就可以使用auto-config了,打包后的文件已经将占位符修改成我们需要的值了,这个是多环境打包的神器。


3 0
原创粉丝点击