Apache Ant中并没有直接提供依赖管理,这也是后来Maven和Gradle之类的Java构建工具能够快速抢占地盘的重要原因之一,Apache Ivy是Apache Ant的一个子项目,用来给Apache Ant提供依赖管理,这篇文章通过一个简单的Spring MVC的demo应用,结合Ant和Ivy,来介绍如何在Ant的项目中使用Ivy进行依赖管理。
代码下载:git clone http://github.com/liumiaocn/easypack.git
代码位置:easypack/ant/ivy目录下
liumiaocn:ivy liumiao$ ls build.xml ivy.xml lib resources src war liumiaocn:ivy liumiao$ tree . . ├── build.xml ├── ivy.xml ├── lib ├── resources │ └── logback.xml ├── src │ └── com │ └── liumiao │ └── controller │ └── HelloController.java └── war ├── META-INF │ └── MANIFEST.MF ├── WEB-INF │ ├── lib │ ├── mvc-dispatcher-servlet.xml │ ├── pages │ │ └── index.jsp │ └── web.xml └── resources └── theme1 └── css └── core.css 14 directories, 9 files liumiaocn:ivy liumiao$构建结果
在build.xml中记载了所使用到的ivy文件,使用ant ivy命令则可下载所需要的2.5.0版本的Apache Ivy的jar文件。
liumiaocn:ivy liumiao$ ant ivy Buildfile: /Users/liumiao/easypack/ant/ivy/build.xml ivy: [get] Getting: http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.5.0/ivy-2.5.0.jar [get] To: /Users/liumiao/.ant/lib/ivy.jar BUILD SUCCESSFUL Total time: 5 seconds liumiaocn:ivy liumiao$
从构建日志中即可看到Ant中是如何使用Ivy进行依赖管理的。直接在build.xml所在的目录执行ant命令,则可以将Spring MVC所依赖的jar文件下载至lib目录下,同时生成war文件。执行日志示例如下所示:
liumiaocn:ivy liumiao$ ant Buildfile: /Users/liumiao/easypack/ant/ivy/build.xml clean: init: [mkdir] Created dir: /Users/liumiao/easypack/ant/ivy/target [mkdir] Created dir: /Users/liumiao/easypack/ant/ivy/war/WEB-INF/classes resolve: [echo] Getting dependencies... [ivy:retrieve] :: Apache Ivy 2.5.0 - 20191020104435 :: https://ant.apache.org/ivy/ :: [ivy:retrieve] :: loading settings :: url = jar:file:/Users/liumiao/.ant/lib/ivy.jar!/org/apache/ivy/core/settings/ivysettings.xml [ivy:retrieve] :: resolving dependencies :: org.apache#WebProject;working@liumiaocn [ivy:retrieve] confs: [build, runtime, test] [ivy:retrieve] found org.slf4j#slf4j-api;1.7.6 in public [ivy:retrieve] found jstl#jstl;1.2 in public [ivy:retrieve] found ch.qos.logback#logback-classic;1.1.2 in public [ivy:retrieve] found ch.qos.logback#logback-core;1.1.2 in public [ivy:retrieve] found org.springframework#spring-core;4.1.3.RELEASE in public [ivy:retrieve] found commons-logging#commons-logging;1.2 in public [ivy:retrieve] found org.springframework#spring-beans;4.1.3.RELEASE in public [ivy:retrieve] found org.springframework#spring-context;4.1.3.RELEASE in public [ivy:retrieve] found org.springframework#spring-aop;4.1.3.RELEASE in public [ivy:retrieve] found aopalliance#aopalliance;1.0 in public [ivy:retrieve] found org.springframework#spring-expression;4.1.3.RELEASE in public [ivy:retrieve] found org.springframework#spring-web;4.1.3.RELEASE in public [ivy:retrieve] found org.springframework#spring-webmvc;4.1.3.RELEASE in public [ivy:retrieve] :: resolution report :: resolve 350ms :: artifacts dl 17ms --------------------------------------------------------------------- | | modules || artifacts | | conf | number| search|dwnlded|evicted|| number|dwnlded| --------------------------------------------------------------------- | build | 13 | 0 | 0 | 0 || 13 | 0 | | runtime | 13 | 0 | 0 | 0 || 13 | 0 | | test | 13 | 0 | 0 | 0 || 13 | 0 | --------------------------------------------------------------------- [ivy:retrieve] :: retrieving :: org.apache#WebProject [ivy:retrieve] confs: [build, runtime, test] [ivy:retrieve] 13 artifacts copied, 0 already retrieved (5920kB/28ms) build: [javac] Compiling 1 source file to /Users/liumiao/easypack/ant/ivy/war/WEB-INF/classes [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7 [javac] 1 warning copy-resources: [copy] Copying 1 file to /Users/liumiao/easypack/ant/ivy/war/WEB-INF/classes package: [ivy:retrieve] :: retrieving :: org.apache#WebProject [ivy:retrieve] confs: [runtime] [ivy:retrieve] 0 artifacts copied, 13 already retrieved (0kB/4ms) [war] Building war: /Users/liumiao/easypack/ant/ivy/target/antivydemo.war main: BUILD SUCCESSFUL Total time: 1 second liumiaocn:ivy liumiao$
ant命令执行之后,目录和文件的结果如下所示:
liumiaocn:ivy liumiao$ tree . . ├── build.xml ├── ivy.xml ├── lib │ ├── aopalliance-1.0.jar │ ├── commons-logging-1.2.jar │ ├── jstl-1.2.jar │ ├── logback-classic-1.1.2.jar │ ├── logback-core-1.1.2.jar │ ├── slf4j-api-1.7.6.jar │ ├── spring-aop-4.1.3.RELEASE.jar │ ├── spring-beans-4.1.3.RELEASE.jar │ ├── spring-context-4.1.3.RELEASE.jar │ ├── spring-core-4.1.3.RELEASE.jar │ ├── spring-expression-4.1.3.RELEASE.jar │ ├── spring-web-4.1.3.RELEASE.jar │ └── spring-webmvc-4.1.3.RELEASE.jar ├── resources │ └── logback.xml ├── src │ └── com │ └── liumiao │ └── controller │ └── HelloController.java ├── target │ └── antivydemo.war └── war ├── META-INF │ └── MANIFEST.MF ├── WEB-INF │ ├── classes │ │ ├── com │ │ │ └── liumiao │ │ │ └── controller │ │ │ └── HelloController.class │ │ └── logback.xml │ ├── lib │ ├── mvc-dispatcher-servlet.xml │ ├── pages │ │ └── index.jsp │ └── web.xml └── resources └── theme1 └── css └── core.css 19 directories, 25 files liumiaocn:ivy liumiao$
可以看到target目录下生成了最终的war文件,同时lib下也下载到了所需要的所有的jar文件,而这些正是通过Ivy所起到的作用。