转自:http://wind-bell27.iteye.com/blog/1969930
先讲基本步骤,如下:
去GitHub中下载spring framework的源码,https://github.com/spring-projects/spring-framework/releases
下载3.2.X其中的一个版本;
安装Gradle软件,官网下载,解压即可,设置GRADLE_HOME,和PATH。
命令行中运行gradle -v,检查一下是否正确安装配置;
命令行中运行spring framework根目录下的import-into-eclipse.bat
连续两次回车,就可以开始自动构建;
中间出现停止不前的情况里,按ctrl+c终止,再运行就可以进行下去;
过程需要花些时间(几个小时都有可能),视下载各种依赖jar包和dom文件的速度而定;
在Eclipse菜单File->import选择General下面的Exsiting projects into workspace,这样就导入完成了。
如果你按上述步骤能一次成功的话,那不仅是你运气,而且RP极高。反正我没有,而且还折腾了好多次才成功能。
问题有以下几个:
问题一:spring-framework-4.0.0.M3要用jdk1.8
如果你习愦于什么都下最新版的,那么你肯定想都没想就下了spring-framework-4.0.0.M3,而且又那么不好采,正好没看README,那么问题马上就来了,直接报"-XX:MaxMetaspaceSize=1024m"及"could not create the java Virtual machine"的错误。
查看build.gradle和gradlew.bat中,可以找出原因:
build.gradle中:
compileJava {
sourceCompatibility=1.6
targetCompatibility=1.6
}
compileTestJava {
sourceCompatibility=1.8
targetCompatibility=1.8
}
可以看出这个版本的test代码要求用1.8。
gradlew.bat中
set GRADLE_OPTS=-XX:MaxMetaspaceSize=1024m -Xmx1024m -XX:MaxHeapSize=256m %GRADLE_OPTS%
其中-XX:MaxMetaspaceSize是1.8的写法。
所以如果下载的是4.0的版本,最好把jdk也升级到1.8,要不就下载一下3.2.x的版本就好了。
问题2:在build过程中,出现heap内存溢出情况
错误信息如下:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failedfortask':referencePdf'.
> GC overhead limit exceeded
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failedfortask ':referen
cePdf'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.ex
ecuteActions(ExecuteActionsTaskExecuter.java:68)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.ex
ecute(ExecuteActionsTaskExecuter.java:46)
at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExec
uter.execute(PostExecutionAnalysisTaskExecuter.java:34)
at org.gradle.api.internal.changedetection.CacheLockHandlingTaskExecuter
$1.run(CacheLockHandlingTaskExecuter.java:34)
at org.gradle.internal.Factories$1.create(Factories.java:22)
at org.gradle.cache.internal.DefaultCacheAccess.longRunningOperation(Def
aultCacheAccess.java:179)
at org.gradle.cache.internal.DefaultCacheAccess.longRunningOperation(Def
aultCacheAccess.java:232)
at org.gradle.cache.internal.DefaultPersistentDirectoryStore.longRunning
Operation(DefaultPersistentDirectoryStore.java:138)
at org.gradle.api.internal.changedetection.DefaultTaskArtifactStateCache
Access.longRunningOperation(DefaultTaskArtifactStateCacheAccess.java:83)
at org.gradle.api.internal.changedetection.CacheLockHandlingTaskExecuter
.execute(CacheLockHandlingTaskExecuter.java:32)
at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.exec
ute(SkipUpToDateTaskExecuter.java:55)
at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execut
e(ValidatingTaskExecuter.java:57)
at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecu
ter.execute(SkipEmptySourceFilesTaskExecuter.java:41)
at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter
.execute(SkipTaskWithNoActionsExecuter.java:51)
at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execut
e(SkipOnlyIfTaskExecuter.java:52)
at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter
.execute(ExecuteAtMostOnceTaskExecuter.java:42)
at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailur
e(AbstractTask.java:247)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.executeTask(De
faultTaskPlanExecutor.java:52)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.processTask(De
faultTaskPlanExecutor.java:38)
at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(Defaul
tTaskPlanExecutor.java:30)
at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(Defau
ltTaskGraphExecuter.java:83)
at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTask
ExecutionAction.java:29)
at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecute
r.java:61)
at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExec
uter.java:23)
at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecu
ter.java:67)
at org.gradle.api.internal.changedetection.TaskCacheLockHandlingBuildExe
cuter$1.run(TaskCacheLockHandlingBuildExecuter.java:31)
at org.gradle.internal.Factories$1.create(Factories.java:22)
at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAcc
ess.java:124)
at org.gradle.cache.internal.DefaultCacheAccess.useCache(DefaultCacheAcc
ess.java:112)
at org.gradle.cache.internal.DefaultPersistentDirectoryStore.useCache(De
faultPersistentDirectoryStore.java:130)
at org.gradle.api.internal.changedetection.DefaultTaskArtifactStateCache
Access.useCache(DefaultTaskArtifactStateCacheAccess.java:79)
at org.gradle.api.internal.changedetection.TaskCacheLockHandlingBuildExe
cuter.execute(TaskCacheLockHandlingBuildExecuter.java:29)
at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecute
r.java:61)
at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExec
uter.java:23)
at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecu
ter.java:67)
at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildEx
ecutionAction.java:32)
at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecute
r.java:61)
at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecute
r.java:54)
at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(Default
GradleLauncher.java:158)
at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradle
Launcher.java:113)
at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLaun
cher.java:81)
at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.jav
a:38)
at org.gradle.launcher.exec.InProcessGradleLauncherActionExecuter.execut
e(InProcessGradleLauncherActionExecuter.java:39)
at org.gradle.launcher.exec.InProcessGradleLauncherActionExecuter.execut
e(InProcessGradleLauncherActionExecuter.java:25)
at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50)
at org.gradle.api.internal.Actions$RunnableActionAdapter.execute(Actions
.java:137)
at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.
execute(CommandLineActionFactory.java:201)
at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.
execute(CommandLineActionFactory.java:174)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(
CommandLineActionFactory.java:170)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(
CommandLineActionFactory.java:139)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionRep
ortingAction.java:33)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionRep
ortingAction.java:22)
at org.gradle.launcher.Main.doAction(Main.java:48)
at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
at org.gradle.launcher.Main.main(Main.java:39)
at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBoots
trap.java:50)
at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.j
ava:32)
at org.gradle.launcher.GradleMain.main(GradleMain.java:26)
at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.ja
va:33)
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:130)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:48)
Caused by: java.lang.OutOfMemoryError: GC overhead limit exceeded
at org.apache.fop.area.inline.TextArea.addWord(TextArea.java:68)
at org.apache.fop.layoutmgr.inline.TextLayoutManager.createTextArea(Text
LayoutManager.java:483)
at org.apache.fop.layoutmgr.inline.TextLayoutManager.addAreas(TextLayout
Manager.java:361)
at org.apache.fop.layoutmgr.inline.LineLayoutManager.addInlineArea(LineL
ayoutManager.java:1716)
at org.apache.fop.layoutmgr.inline.LineLayoutManager.addAreas(LineLayout
Manager.java:1568)
at org.apache.fop.layoutmgr.BlockLayoutManager.addAreas(BlockLayoutManag
er.java:395)
at org.apache.fop.layoutmgr.BlockLayoutManager.addAreas(BlockLayoutManag
er.java:395)
at org.apache.fop.layoutmgr.BlockLayoutManager.addAreas(BlockLayoutManag
er.java:395)
at org.apache.fop.layoutmgr.BlockLayoutManager.addAreas(BlockLayoutManag
er.java:395)
at org.apache.fop.layoutmgr.AreaAdditionUtil.addAreas(AreaAdditionUtil.j
ava:121)
at org.apache.fop.layoutmgr.FlowLayoutManager.addAreas(FlowLayoutManager
.java:305)
at org.apache.fop.layoutmgr.PageBreaker.addAreas(PageBreaker.java:241)
at org.apache.fop.layoutmgr.AbstractBreaker.addAreas(AbstractBreaker.jav
a:508)
at org.apache.fop.layoutmgr.AbstractBreaker.addAreas(AbstractBreaker.jav
a:371)
at org.apache.fop.layoutmgr.PageBreaker.doPhase3(PageBreaker.java:262)
at org.apache.fop.layoutmgr.AbstractBreaker.doLayout(AbstractBreaker.jav
a:346)
at org.apache.fop.layoutmgr.AbstractBreaker.doLayout(AbstractBreaker.jav
a:264)
at org.apache.fop.layoutmgr.PageSequenceLayoutManager.activateLayout(Pag
eSequenceLayoutManager.java:106)
at org.apache.fop.area.AreaTreeHandler.endPageSequence(AreaTreeHandler.j
ava:234)
at org.apache.fop.fo.pagination.PageSequence.endOfNode(PageSequence.java
:123)
at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.endElement(FOTreeBuilde
r.java:340)
at org.apache.fop.fo.FOTreeBuilder.endElement(FOTreeBuilder.java:169)
at org.apache.xalan.transformer.TransformerIdentityImpl.endElement(Trans
formerIdentityImpl.java:1050)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source
)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknow
n Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContent
Dispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Un
known Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xalan.transformer.TransformerIdentityImpl.transform(Transf
ormerIdentityImpl.java:432)
BUILD FAILED
那就把gradlew.bat的内存设置调大一点,我做实验时,机器的内存是8G、64位系统,直接使用
set GRADLE_OPTS=-XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=1024m %GRADLE_OPTS%
(我下载的是spring-framework-3.2.1.RELEASE版本,jdk1.7)
问题3:gradle命令的使用
在spring源码的根目录下,提供gradlew.bat和
import-into-eclipse.bat这两个命令,都可以build源码,但如果使用前者就不会生成eclipse的环境需要的一些文件,如下图所示:
另外,在使用import-into-eclipse.bat这个命令build时,可能会遇到一个问题,错误日志如下:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failedfortask':spring-webmvc-tiles3:eclipseClasspath'.
> Could not resolve all dependenciesforconfiguration'detachedConfiguration2'.
> Module version group:org.springframework, module:spring-webmvc-tiles3, vers
ion:3.2.1.RELEASE, configuration:detachedConfiguration2 declares a dependency on
configuration'runtimeMerge'which is not declared in the module descriptorfor
group:org.springframework, module:spring-web, version:3.2.1.RELEASE
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.
从日志中可以看出是在build spring-webmvc-tiles3项目时,生成eclipse的.classpath文件时出错,打开其它正常导入的项目的.classpath看下这个文件包含了哪些信息,如打开spring-aop项目的:
而spring-webmvc-tiles3下根本就没有.classpath的影子,没得比较,虽然上面的错误日志的E文都认识,但还是看不出它在讲什么,不过大体能看得出是依赖出了问题,那只能是想办法顺藤摸瓜了。重新打开build.gradle,找到有关spring-webmvc-tiles3项目的描述,如下:
project("spring-webmvc-tiles3") {
description ="Spring Framework Tiles3 Integration"
merge.into = project(":spring-webmvc")
dependencies {
provided(project(":spring-context"))
provided(project(":spring-web"))
provided("javax.el:el-api:1.0")
provided("javax.servlet:jstl:1.2")
provided("javax.servlet.jsp:jsp-api:2.1")
optional("org.apache.tiles:tiles-request-api:1.0.1")
optional("org.apache.tiles:tiles-api:3.0.1")
optional("org.apache.tiles:tiles-core:3.0.1") {
exclude group:"org.slf4j", module:"jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-servlet:3.0.1") {
exclude group:"org.slf4j", module:"jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-jsp:3.0.1") {
exclude group:"org.slf4j", module:"jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-extras:3.0.1") {
exclude group:"org.slf4j", module:"jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-el:3.0.1") {
exclude group:"org.slf4j", module:"jcl-over-slf4j"
}
provided("javax.servlet:javax.servlet-api:3.0.1")
testCompile("org.slf4j:slf4j-jcl:${slf4jVersion}")
}
}
打开C:/Users/so/.gradle/caches/artifacts-15/filestore,挨个依赖进行检查,jar包倒是最下载下来了,但是"org.apache.tiles:tiles-jsp:3.0.1"、"org.apache.tiles:tiles-extras:3.0.1"、"org.apache.tiles:tiles-el:3.0.1"三个却没有下载到source,其它包都有jar、pom、source三个目录,这三个没有source目录,只有jar、pom目录,问题应该是出现在这里。
后来终于让我找到了这么一个东西:http://issues.gradle.org/browse/GRADLE-1157,正好证实我前面的猜想,所以按照issue中的解决办法,在build.gradle中加上如下部分:
eclipseClasspath {
downloadSources =false;// required for eclipseClasspath to work
}
关于spring-webmvc-tiles3项目的脚本、就变成这样了:
project("spring-webmvc-tiles3") {
description ="Spring Framework Tiles3 Integration"
merge.into = project(":spring-webmvc")
dependencies {
provided(project(":spring-context"))
provided(project(":spring-web"))
provided("javax.el:el-api:1.0")
provided("javax.servlet:jstl:1.2")
provided("javax.servlet.jsp:jsp-api:2.1")
optional("org.apache.tiles:tiles-request-api:1.0.1")
optional("org.apache.tiles:tiles-api:3.0.1")
optional("org.apache.tiles:tiles-core:3.0.1") {
exclude group:"org.slf4j", module:"jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-servlet:3.0.1") {
exclude group:"org.slf4j", module:"jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-jsp:3.0.1") {
exclude group:"org.slf4j", module:"jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-extras:3.0.1") {
exclude group:"org.slf4j", module:"jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-el:3.0.1") {
exclude group:"org.slf4j", module:"jcl-over-slf4j"
}
provided("javax.servlet:javax.servlet-api:3.0.1")
testCompile("org.slf4j:slf4j-jcl:${slf4jVersion}")
}
eclipseClasspath {
downloadSources =false;// required for eclipseClasspath to work
}
}
重新运行命令,这一次终于让我看到了BUILD SUCCESSFUL的字样了……
成功build完成后,可以看到spring目录下的spring-webmvc-tiles3项目也正常生成了导入eclipse需要的文件。打开.classpath查看一下:
果然没有sourcepath的信息。
折腾了那么久,终于到了可以看一下成果的时候了。
Spring Tool Suite说简单一点就是Spring家的eclipse,去官网下一个最新版,安装上。
打开STS,菜单File->import选择General下面的Exsiting projects into workspace,勾选要导入的子项目,确定即可。
成果如上,齐齐整整,没有错误,挺好!