加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

build.xml详解

发布时间:2020-12-16 07:39:25 所属栏目:百科 来源:网络整理
导读:? xml version = "1.0" ? !-- 默认任务 default 可以指定直接执行ant 命令的时候默认的任务可以通过指定任务 名称(target name)运行不同的任务 比如: ant package -- project name = "tax-calculator" default "package" basedir "." !-- 设置property 可以
<?xml version="1.0" ?>
<!--
默认任务 default 可以指定直接执行ant
命令的时候默认的任务可以通过指定任务
名称(target name)运行不同的任务 比如:
ant package
-->
<project name="tax-calculator" default"package" basedir".">

<!--
设置property 可以在其他地方通过${xxx} 的方式引用 也可以通过 property file设 置具体的properties文件
这里有个优先级 的问题 首先你可以在执行ant命令 这个优 先级是最大的 其次的是properties 文件 中的属性
最后的优先级是写在build.xml 中的property(很怪)
-->
<property "project.name" value"exampleant" />
"project.version" "snapshot" />

"base.dir" "." />
"dist.dir" "${base.dir}/dist" />
"wardist.dir" "${base.dir}/wardist" />

"testlog.dir" "${base.dir}/testlog" />
"testlog.html.dir" "${testlog.dir}/html" />

"source.dir" "${base.dir}/src" />
"webroot.dir" "${base.dir}/web" />

"build.dir" "${base.dir}/build" />
"build.classes.dir" "${build.dir}/classes" />
"build.test.classes.dir" "${build.dir}/test-classes" />
"build.classes.main" "com.javapowertools.taxcalculator.Main" />
"source.main.dir" "${source.dir}/main" />
"source.test.dir" "${source.dir}/test" />
"lib.dir" "${base.dir}/lib" />

<!--
taskdef 可以用taskdef自定义一个任务 比如远程部署等(回去有空的话再研究下cargo)
http://hi.baidu.com/giter/blog/item/d9fcd6431446f41673f05d1f.html
-->
<!--
ant 引用其他构建文件中的任务(加载顺序由上到下 尤其要注意不要引用不存在的property 这里用的是basedir)
再者当前构建文件内的任务无法直接引用其他构建文件的任务(比如前置任务) 分开文件固然比较好 但是这里依旧要注意

antfile:构建文件位置
target:任务名称
-->
<ant antfile"${base.dir}/targets/init.xml" target"init" />
"${base.dir}/targets/bootstrap-findbugs.xml" "display-properties" />

<!--
target 任务
name:任务名称
depends:前置任务(强调该任务执行前必须执行depends指定的任务)
description:描述
-->

<!--
设置类路径
-->
<path id"compile.classpath">
<fileset dir"${lib.dir}" includes"*.jar" />
<pathelement path"${build.classes.dir}" />
</path>

"test.compile.classpath">
refid"compile.classpath" />
"${build.test.classes.dir}" />
</path>

<!--
javac 编译Java源文件
srcdir:源文件目录
destdir:输出目录
classpathref:classpath信息
includes:告诉Ant编译过程中编译哪些位置的源文件(其他位置被忽视)
excludes:告诉Ant编译过程中忽略那些位置的源文件
debug:是否开启调试信息
source:强调开发源代码的JDK版本
target:强调编译后的Class文件应该支持的JDK版本
compiler:指定编译器(不一定非得使用Sun的标准编译器进行编译)
-->
<target "compile" description"Compile Java code">
<javac srcdir"${source.main.dir}/resources" destdir"${build.classes.dir}" source"1.6" debug"true" "com.javapowertools.taxcalculator.**" />
"${source.main.dir}/java" />
</target>

"compile-test" depends"Compile Unit Tests">
"${source.test.dir}/resources" classpathref"test.compile.classpath" />
"${source.test.dir}/java" />
</target>

<!--
jar 打包Java文件
destfile:指定输出文件
basedir:类文件所在位置

tstamp 生成时间戳
format:设置指定格式
manifest 设置MANIFEST.MF信息
attribute 属性对象
name:属性名
value:属性值
-->
"compile,test,test-report" "Create Jar file">
<tstamp>
<format property"build.date" pattern"yyyy-MM-dd" />
"build.time" "hh:mm:ss" />
</tstamp>
<jar destfile"${dist.dir}/${project.name}-${project.version}.jar" "${build.classes.dir}">
<manifest>
<attribute "Main-Class" "${build.classes.main}" />
"Built-By" "${user.name}" />
"Specification-Title" "${project.name}" />
"Specification-Version" "${project.version}" />
"Specification-Vendor" "ACME Incorporated" />
"Implementation-Title" "common" />
"Implementation-Version" "${project.version} - built at ${build.time} on ${build.date}" />
"Implementation-Vendor" />
</manifest>
</jar>
</target>

<!--
junit 单元测试
printsummary:是否显示单元测试类列表,如果是否的话Ant将运行测试但不显示测试结果,除非有单元测试失败
haltonfailure:是否测试失败就停止构建,默认为否,也就是即使测试失败也继续构建
fork:是否重新启用一个JVM进程进行测试(默认为false 正常情况下重新启用一个JVM进程进行测试会有意想不到的效果)
failureproperty:创建一个指定名称的变量 变量值为false或true 取决于junit的测试结果 并可以在其他target中被fail标签
assertions 指定断言机制
enable 该属性为所有非系统类激活断言
package:指定某一个包下的类激活断言
class:指定具体某一个类激活断言


test 指定单元测试的类
name:类名

batchtest 批量测试

todir:指定日志输出目录

fileset 文件信息
dir:所在目录
includes:包含的测试类
formatter 指定详细信息输出格式

type:有三种格式 brief只提供失败测试的详细信息 plain提供了通过的和失败的测试数量,并列出实际成功的测试
xml则用于生成报告
usefile:是否创建文件,默认为true
-->
"test" "Run unit tests">
<junit printsummaryhaltonfailure"false" forkfailureproperty"test.failures">
<assertions>
<enable />
</assertions>
<classpath "test.compile.classpath" />
<formatter type"xml" usefile"true" />
<!--
<test name="com.javapowertools.taxcalculator.TaxRateTest" />
-->
<batchtest todir"${testlog.dir}">
"${build.test.classes.dir}" "**/*Test.class" />
</batchtest>
</junit>
</target>

<!--
关于生成测试报告的建议
如果开发人员选择了生成测试报告 也就表示不能在测试出现问题的时候简单的停止构建(这样会影响报告的完整性)
但是将haltonfailure属性设置为true并不是好的解决方式,另一方面,如果测试失败就停止构建是个好主意,
事实上持续构建系统是依赖这一点的
解决方式参考Java开发超级工具集中文版50页
-->

<!--
junitreport 生成报告(通过打包在ant-junit.jar内的xsl样式表来生成 有必要的话可以自行更改)
todir:源文件(XML)目录

report HTML生成模板信息定义
format:指定是否使用Frame 有noframes和frames两种
todir:指定HTML文件输出位置
styledir:可以自己指定HTML使用的样式
-->
"test-report" "Generate HTML unit test reports">
<junitreport >
>
<include "TEST-*.xml" />
</fileset>
<report format"frames" "${testlog.html.dir}" />
</junitreport>
<fail if"test.failures" message"There were test failures." />
</target>
<!--
war 打包war文件
destfile:指定输出文件
webxml:指定web.xml位置

classes 指定类文件位置

lib 指定lib文件位置


-->
"war" "Generate WAR file">
<war "${wardist.dir}/${project.name}-${project.version}.war" webxml"${webroot.dir}/WEB-INF/web.xml">
/>
<classes />

<lib "${lib.dir}">
/>
</lib>
</war>
</target>

<!--
copy 复制文件
file:被复制的文件(也可以用fileset指定多个被复制的目录,具体用法参考前面fileset的用法)
todir:目标目录
tofile:目标文件(当原始文件需要重命名的时候)
-->
"depoly" "Depoly to local Tomcat instance">
<copy file"D:/Tomcat6/webapps" />
</target>

"clean" "Deletes generated dire ctories">
<delete "${build.dir}" />
"${dist.dir}" />
"${testlog.dir}" />
</target>
</project>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读