目录
- 概述
- 入门示例
- 快速教程
[一]、概述
Apache Ivy是一个优秀的管理(记录、跟踪、解析和报告)项目依赖的工具,可与Apache Ant紧密集成。
官网:http://ant.apache.org/ivy/index.html
截止:2012-06-06最新版本为:2.3.0-rc1
下载解压后,把ivy-2.3.0-rc1.jar文件复制到%ANT_HOME%/lib/ 下。
[二]、入门示例
创建目录:G:\test\ant\ivydemo\ ,在该目录创建文件:build.xml,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
<project name="go-ivy" default="go" xmlns:ivy="antlib:org.apache.ivy.ant"> <!-- here is the version of ivy we will use. change this property to try a newer version if you want --> <property name="ivy.install.version" value="2.0.0-beta1" /> <property name="ivy.jar.dir" value="${basedir}/ivy" /> <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" /> <property name="build.dir" value="build" /> <property name="src.dir" value="src" /> <target name="download-ivy" unless="skip.download"> <mkdir dir="${ivy.jar.dir}"/> <!-- download Ivy from web site so that it can be used even without any special installation --> <echo message="installing ivy..."/> <get src="http://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar" dest="${ivy.jar.file}" usetimestamp="true"/> </target> <!-- ================================= target: install-ivy this target is not necessary if you put ivy.jar in your ant lib directory if you already have ivy in your ant lib, you can simply remove this target and the dependency the 'go' target has on it ================================= --> <target name="install-ivy" depends="download-ivy" description="--> install ivy"> <!-- try to load ivy here from local ivy dir, in case the user has not already dropped it into ant's lib dir (note that the latter copy will always take precedence). We will not fail as long as local lib dir exists (it may be empty) and ivy is in at least one of ant's lib dir or the local lib dir. --> <path id="ivy.lib.path"> <fileset dir="${ivy.jar.dir}" includes="*.jar"/> </path> <taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path"/> </target> <!-- ================================= target: go Go ivy, go! ================================= --> <target name="go" depends="install-ivy, generate-src" description="--> resolve dependencies, compile and run the project"> <echo message="using ivy to resolve commons-lang 2.1..."/> <!-- here comes the magic line: asks ivy to resolve a dependency on commons-lang 2.1 and to build an ant path with it from its cache --> <ivy:cachepath organisation="commons-lang" module="commons-lang" revision="2.1" pathid="lib.path.id" inline="true"/> <echo message="compiling..."/> <mkdir dir="${build.dir}" /> <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="lib.path.id" /> <echo> We are now ready to execute our simple program with its dependency on commons-lang. Let's go! </echo> <java classname="example.Hello"> <classpath> <path refid="lib.path.id" /> <path location="${build.dir}" /> </classpath> </java> </target> <!-- ================================= target: generate-src 'Generates' the class source. It actually just echo a simple java source code to a file. In real life this file would already be present on your file system, and this target wouldn't be necessary. ================================= --> <target name="generate-src"> <mkdir dir="${src.dir}/example" /> <echo file="${src.dir}/example/Hello.java"> package example; import org.apache.commons.lang.WordUtils; public class Hello { public static void main(String[] args) { String message = "hello ivy !"; System.out.println("standard message : " + message); System.out.println("capitalized by " + WordUtils.class.getName() + " : " + WordUtils.capitalizeFully(message)); } } </echo> </target> <!-- ================================= target: clean ================================= --> <target name="clean" description="--> clean the project"> <delete includeemptydirs="true" quiet="true"> <fileset dir="${src.dir}" /> <fileset dir="${build.dir}" /> </delete> </target> <!-- ================================= target: clean-ivy ================================= --> <target name="clean-ivy" description="--> clean the ivy installation"> <delete dir="${ivy.jar.dir}"/> </target> <!-- ================================= target: clean-cache ================================= --> <target name="clean-cache" depends="install-ivy" description="--> clean the ivy cache"> <ivy:cleancache /> </target> </project> |
亦可参考:http://ant.apache.org/ivy/history/latest-milestone/samples/build.xml
在控制台中切换到目录G:\test\ant\ivydemo\下,运行ant命令,结果如下图:
[三]、快速教程
创建目录名称:G:\test\ant\hello-ivy ,该目录下的文档结构如下:
1 2 3 4 5 6 7 8 9 10 |
G:\test\ant\hello-ivy>tree /F 卷 other 的文件夹 PATH 列表 卷序列号为 D238-BE47 G:. │ build.xml │ ivy.xml │ └─src └─example Hello.java |
build.xml 内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<project name="hello-ivy" default="run" xmlns:ivy="antlib:org.apache.ivy.ant"> <!-- some variables used --> <property name="lib.dir" value="lib" /> <property name="build.dir" value="build" /> <property name="src.dir" value="src" /> <!-- paths used for compilation and run --> <path id="lib.path.id"> <fileset dir="${lib.dir}" /> </path> <path id="run.path.id"> <path refid="lib.path.id" /> <path location="${build.dir}" /> </path> <!-- ================================= target: resolve ================================= --> <target name="resolve" description="--> retreive dependencies with ivy"> <ivy:retrieve/> </target> <!-- ================================= target: report ================================= --> <target name="report" depends="resolve" description="--> generates a report of dependencies"> <ivy:report todir="${build.dir}"/> </target> <!-- ================================= target: run ================================= --> <target name="run" depends="resolve" description="--> compile and run the project"> <mkdir dir="${build.dir}" /> <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="lib.path.id" includeAntRuntime="false"/> <property name="msg" value="hello ivy !"/> <java classpathref="run.path.id" classname="example.Hello"> <arg value="-message"/> <arg value="${msg}"/> </java> </target> <!-- ================================= target: clean ================================= --> <target name="clean" description="--> clean the project"> <delete includeemptydirs="true"> <fileset dir="${basedir}"> <exclude name="src/**" /> <exclude name="build.xml" /> <exclude name="ivy.xml" /> </fileset> </delete> </target> <!-- ================================= target: clean-cache ================================= --> <target name="clean-cache" description="--> clean the ivy cache"> <ivy:cleancache /> </target> </project> |
ivy.xml 内容如下:
1 2 3 4 5 6 7 |
<ivy-module version="2.0"> <info organisation="org.apache" module="hello-ivy"/> <dependencies> <dependency org="commons-lang" name="commons-lang" rev="2.0"/> <dependency org="commons-cli" name="commons-cli" rev="1.0"/> </dependencies> </ivy-module> |
Hello.java 内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package example; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.Option; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.lang.WordUtils; /** * Simple example to show how easy it is to retrieve transitive libs with ivy !!! */ public final class Hello { public static void main(String[] args) throws Exception { Option msg = OptionBuilder.withArgName("msg") .hasArg() .withDescription("the message to capitalize") .create("message"); Options options = new Options(); options.addOption(msg); CommandLineParser parser = new GnuParser(); CommandLine line = parser.parse(options, args); String message = line.getOptionValue("message", "hello ivy !"); System.out.println("standard message : " + message); System.out.println("capitalized by " + WordUtils.class.getName() + " : " + WordUtils.capitalizeFully(message)); } private Hello() { } } |
打开控制台,切换到hello-ivy的根目录下,运行ant 命令,结果如下:
编译后的目录结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
G:\test\ant\hello-ivy>tree /F 卷 other 的文件夹 PATH 列表 卷序列号为 D238-BE47 G:. │ build.xml │ ivy.xml │ ├─build │ └─example │ Hello.class │ ├─lib │ commons-cli-1.0-javadoc.jar │ commons-cli-1.0-sources.jar │ commons-cli-1.0.jar │ commons-lang-2.0-javadoc.jar │ commons-lang-2.0-sources.jar │ commons-lang-2.0.jar │ commons-logging-1.0.jar │ └─src └─example Hello.java |
更详细的教程可以参考:http://ant.apache.org/ivy/history/latest-milestone/tutorial.html
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/software/build/apache-ivy-start/
0 条评论。