---
title: "ant构建spring工程"
author: "helloman0907"
date: "2014-08-11T10:17:05Z"
tags: []
categories: ["开源软件"]
slug: "ant-spring"
draft: false
lastmod: "2014-08-11T10:17:05Z"
id: 2102332
url: "https://helloman0907.iteye.com/blog/2102332"
---

# [spring3.1.x源码编译并导入eclipse](http://blog.csdn.net/hingyi/article/details/10243493)

分类： [Spring](http://blog.csdn.net/Feb13/article/category/1184183)2013-08-24 00:17 889人阅读 [评论](http://blog.csdn.net/hingyi/article/details/10243493#comments)(3) 收藏 [举报](http://blog.csdn.net/hingyi/article/details/10243493#report "举报")

[spring 3.1.x](http://www.csdn.net/tag/spring%203.1.x)[源码](http://www.csdn.net/tag/%e6%ba%90%e7%a0%81)[编译](http://www.csdn.net/tag/%e7%bc%96%e8%af%91)[导入](http://www.csdn.net/tag/%e5%af%bc%e5%85%a5)[eclipse](http://www.csdn.net/tag/eclipse)

**spring3.1.x源码编译并导入eclipse**

最近在学习spring的时候想将源码编译下，然后呢就去网上找找方法。参考了以下内容：

[http://blog.springsource.com/2009/03/03/building-spring-3/](http://blog.springsource.com/2009/03/03/building-spring-3/)

[http://zachary-guo.iteye.com/blog/661839](http://zachary-guo.iteye.com/blog/661839)

[http://blog.sina.com.cn/s/blog\_49fd52cf0100r2rn.html](http://blog.sina.com.cn/s/blog_49fd52cf0100r2rn.html)

知道spring编译方法从原来的ant变成了Gradle。但是我下载的版本是3.1.4，所以以下的是spring3.1.4源码编译，虽然很多都和参考资料中相同，但确实是自己验证过的。

**1、  通过SVN从spring服务器上下载源码**

spring的源码从SVN搬到github上。

地址：[https://github.com/SpringSource/spring-framework](https://github.com/SpringSource/spring-framework)

**2、  通过ant编译源码**

**1）对于JDK的要求**

spring的编译要求是至少java 6。我机子上安装的是jdk1.6.0\_13。

设置jdk环境变量：

**\[plain]** [view plain](http://blog.csdn.net/hingyi/article/details/10243493 "view plain")[copy](http://blog.csdn.net/hingyi/article/details/10243493 "copy")

1. JAVA\_HOME = C:\\ProgramFiles (x86)\\Java\\jdk1.6.0\_13
2. JAVA\_OPTS = -Xms512m -Xmx1024m -XX:MaxNewSize=512m -XX:MaxPermSize=1024m

**2）对于ANT的要求**

ant版本要求至少要1.7。这里用的是1.8.4

设置ant的环境变量：

**\[plain]** [view plain](http://blog.csdn.net/hingyi/article/details/10243493 "view plain")[copy](http://blog.csdn.net/hingyi/article/details/10243493 "copy")

1. ANT\_HOME = C:\\MyApplication\\apache-ant-1.8.4
2. ANT\_OPTS = -Xms256m -Xmx768m -XX:MaxNewSize=256m-XX:MaxPermSize=512m

3）build源码

通过ant来build源码，通过ivy下载spring所需的第3方jar包。

用命令提示符方式进入到源码的build-spring-framework目录下，然后ant。如下

**\[plain]** [view plain](http://blog.csdn.net/hingyi/article/details/10243493 "view plain")[copy](http://blog.csdn.net/hingyi/article/details/10243493 "copy")

1. --&gt;cd {SRC\_HOME}/build-spring-framework
2. --&gt;ant
3. ….

在这边我无力吐槽网速什么的，反正build的时间非常长。而且中间还断断续续的出现连接超时情况。终于，到了test的时候，挂了…..

找了些资料，都说test可以不需要。好吧，为了能build success，咱也就去掉test吧！

方法：在{SRC\_HOME}\\build-spring-framework\\build.xml 中找到

**\[html]** [view plain](http://blog.csdn.net/hingyi/article/details/10243493 "view plain")[copy](http://blog.csdn.net/hingyi/article/details/10243493 "copy")

1. &lt;targetnametargetname="precommit" depends="clean, clean-integration,test"/&gt;

 然后去掉test后改成了

**\[html]** [view plain](http://blog.csdn.net/hingyi/article/details/10243493 "view plain")[copy](http://blog.csdn.net/hingyi/article/details/10243493 "copy")

1. &lt;target name="precommit" depends="clean,clean-integration"/&gt;

 继续ant，没过多久出现了盼望已久的BUILD　SUCCESS。

**3、  把编译好的源码导入eclipse**

**1） 在eclipse中新建一个workspace**

参考Spring Team Blog 上的这篇文章，我发现新建一个workspace还是很有必要的。因为spring源码的工程比较多，个人不习惯所有项目混合在一起，既然要看源码，那就干干净净地看呗。

**2） 设置IVY\_CACHE环境变量**

因为spring整个工程都依赖与IVY\_CACHE。所以必须设置该环境变量。其实呢，它就是ant在build时创建的ivy-cache，里面是spring工程依赖的jar包。

设置方法如下：

Window -&gt; Preferences -&gt; Java -&gt; Build Path -&gt;Classpath Variables -&gt; New

然后填写：

            NAME:IVY\_CACHE

            PATH: ${SRC\_HOME}/ivy-cache/repository

**3） 设置workspace的jre**

这里至少需要jre 6以上。

设置方法如下：

Window -&gt; Preferences -&gt; Java -&gt; Installed JREs 添加相关6以上版本的JDK。

在Window -&gt; Preferences -&gt; Java -&gt; Complier的Compilercompliance level选择相关选择。

**4） 将源码导入到eclipse**

File -&gt; Import -&gt; Existing Projects into workspace，rootdirectory 选择 ${SRC\_HOME}。

**5） 补充jar包**

在eclipse中导入源码后，你会发现项目上还有红叉叉或红色感叹号。通过查看eclipse的Makers，我们知道了缺少了相关的Jar包。为了消除这些错误，我根据Makers中的提示，到[http://ebr.springsource.com/repository/app/bundle](http://ebr.springsource.com/repository/app/bundle)中下载缺失的Jar包，并将它们放到对应的位置。

**6） 继续消除错误**

按理说，在引入jar包后工程应该不会报错了吧，可是呢，我发现几乎所有的项目都是好的，除了org.springframework.oxm项目。查看了下，原来是test的目录中少了Flight等类。想想，还是上网找吧，可是我却没有找到相关的类，真的不知道是spring的疏忽呢还是我的原因。后来，干脆在.classpath中屏蔽了对test目录的引入，一切OK。

**4、  总结**

虽然编译成功了，但还是发现了很多奇怪的问题。比如，在引入缺失的jar时发现有一些没有被下载到，还有一些版本号不对。没有被下载到，可以被理解，毕竟被限制的网络什么都有可能发生；但是，第三方jar包版本号不对让我很奇怪呀！不知道是不是因为我checkout的版本问题呢？我下载的是branches 中的3.1.x版本，会不会在下载Relsease会好些呢？等下次有时间再试下。

还有，过段时间去学习编译下3.2.x版本的源码，那又是一种新的方式啊！