ant是最为古老的build工具。在他那个时代,他的竞争产品是C语言的make。和make采用shell脚本和makefile执行不同,ant采用java语言依照build.xml进行构建。这是一个跨时代的进步,意味着你可以写出平台无关的build的流程。但是ant的现在嘛…阿里基于ant魔改了一套antx。
sofa应用打包的概述
方案一
assembly目录下的pom文件负责指定生成ACE文件格式。
maven-antrun-plugin 插件,用于拷贝资源依赖,将conf目录下的所有文件拷贝至ACE中;
maven-dependency-plugin 插件,用于拷贝jar包依赖,在拷贝jar包依赖时又区分不同的执行阶段,分别是copy-libs和copy-core,将jar包依赖拷贝至不同目录。
方案二
在主POM中配置如下
<plugin>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-maven-plugin</artifactId>
<version>1.0.3</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>ace</goal>
</goals>
</execution>
</executions>
</plugin>
SOFA4工程的完整打包构建流程
本文章内容只设计SOFA4的使用。
首先,在ace模块的build中,要有如下插件
第一个起作用的插件如下是maven-dependency-plugin
<!-- 复制库和本工程代码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>create-structure</id>
<phase>process-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.alipay.agdsccalipay</groupId>
<artifactId>agdsccalipay-assembly-template</artifactId>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/${assembly.name}.ace</outputDirectory>
</configuration>
</execution>
<execution>
<id>copy-libs</id>
<phase>process-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems></artifactItems>
</configuration>
</execution>
<execution>
<id>copy-core</id>
<phase>process-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems></artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<!-- 复制模块设置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy-config</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="${project.build.directory}/${assembly.name}.ace">
<fileset dir="src/main/resources"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
主要是负责把所依赖的包都集中在.ace文件夹中
接下来是maven-assembly-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<descriptors>
<descriptor>src/main/assemble/ace.xml</descriptor>
</descriptors>
<attach>false</attach>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>process-resources</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy-config</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="${project.build.directory}/${assembly.name}.ace">
<fileset dir="src/main/resources"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
对于一个assembly的写法如下
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
一个典型的ace.xml文件如下
<assembly>
<id>ace</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>target/assembly-ace.ace</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
这步骤就是保证了将target/assembly-ace.ace的文件达成zip包来提供部署。
最后存在下面的插件maven-antrun-plugin。这个插件是maven提供的执行ant脚本的模块。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>rename-zip</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy
file="${project.build.directory}/${assembly.name}-assembly-ace-${project.version}-ace.zip"
tofile="${project.build.directory}/${assembly.name}-${project.version}.ace"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
这步骤是把打完的zip包改了个名??
一个典型的Sofa工程的构建-配置-部署脚本如下
#!/bin/bash
echo -------------------------------------------------------------------------
echo Sofa Build/Config/Deploy Script for Linux
echo -------------------------------------------------------------------------
# build
if [ "$1" == "build" ]; then
mvn clean install -Dmaven.test.skip=true -Denv=$ENV
elif [ "$1" == "qbuild" ]; then
mvn clean -o install -Dmaven.test.skip=true
fi
# config
CURRENT_DIR=`pwd`
cd assembly/ace/target/agdsccalipay.ace/config
antxconfig -I -d autoconf/auto-config.xml
cd $CURRENT_DIR
# call additional ant tasks
java -classpath ./deploy/miniant/lib/ant-launcher.jar -Dant.home=./deploy/miniant org.apache.tools.ant.launch.Launcher -buildfile all.xml $1 $2 $3 $4 $5 $6 $7 $8 $9
可以看到,仍然是使用mvn完成构建。
配置
sofa和antx的配置是史上最混乱的。一般,这些配置有以下作用
- sofa 框架的配置文件,配置特定的属性,可以改变框架行为。
- 在 Spring 配置文件中使用,进行变量替换。借助了Velocity模版引擎。
#if ($is_test == true)
<bean id="springBean" class="com.alipay.sofademo.web.home.SpringBeanImplOne"/>
#else
<bean id="springBean" class="com.alipay.sofademo.web.home.SpringBeanImplTwo"/>
#end
- 在代码中获取配置信息。
@Controller
public class SampleController {
@AppConfig
private AppConfiguration appConfiguration;
@RequestMapping(value="/sample",method = RequestMethod.GET)
public void doGet(ModelMap modelMap) {
modelMap.put("hello", appConfiguration.getPropertyValue("app_name"));
}
}
sofa-test-config.properties
本地开发环境或者测试框架运行需要依赖的全量antx配置
代码中路径在 $projectPath/app/test/src/test/resources/sofaconfig/sofa-test-config.propertie
此文件中所有配置的antx属性间隔都使用下划线"_"
在测试中,只需要配置这一个文件就可以了。
auto-config.xml
指定变量的默认值。
代码中路径在 $projectPath/conf/autoconf/auto-config.xml。
这个文件打包后会被移动target目录和ace包中。
在开发和测试机器上,执行ccbin/build.sh,会执行abtx的变量替换。这时候会读取auto-config.xml文件(这个文件会被打入ace包中),这个文件会提供变量的默认值。然后执行script中的generate。
<script>
<generate template="sofa-config.properties.vm" destfile="sofa-config.properties" charset="GBK"/>
</script>
上述template后的文件名是模版文件。是变量模版文件(也会被打如ace包中)。定义最终生效的变量有那些。如果变量没有在这个文件配置,则不会生效。上述的destfile是最后产生的。本地编译是不会产生的。线上构建会最终会产生生效的sofa-config.properties。如果你要检查是否使用了正确的配置,请检查这个文件。
antx.properties
/home/admin/antx.properties和/home/admin/antx.properties.remote是编译依赖的配置文件。线上服务器会初始配置这些文件。
和上面类似通过auto-configm.xml来进行。注意,该些文件是初始配置,不是最终配置。此文件结合代码中相关的几个antx文件,通过编译脚本才能生成最后被框架生效的antx配置文件。
日志
本地测试时,日志的配置使用app/test/src/test/resources/sofaconfig/sofa-log4j.xml
注意,文件名不能变化。