轉(zhuǎn)自:http://wind-bell27.iteye.com/blog/1969930
先講基本步驟畏鼓,如下:
去GitHub中下載spring framework的源碼雾叭,https://github.com/spring-projects/spring-framework/releases
下載3.2.X其中的一個(gè)版本;
安裝Gradle軟件澜汤,官網(wǎng)下載,解壓即可伞梯,設(shè)置GRADLE_HOME,和PATH蛛蒙。
命令行中運(yùn)行g(shù)radle -v,檢查一下是否正確安裝配置寸士;
命令行中運(yùn)行spring framework根目錄下的import-into-eclipse.bat
連續(xù)兩次回車(chē)檐什,就可以開(kāi)始自動(dòng)構(gòu)建碴卧;
中間出現(xiàn)停止不前的情況里,按ctrl+c終止乃正,再運(yùn)行就可以進(jìn)行下去住册;
過(guò)程需要花些時(shí)間(幾個(gè)小時(shí)都有可能),視下載各種依賴(lài)jar包和dom文件的速度而定瓮具;
在Eclipse菜單File->import選擇General下面的Exsiting projects into workspace荧飞,這樣就導(dǎo)入完成了。
如果你按上述步驟能一次成功的話名党,那不僅是你運(yùn)氣叹阔,而且RP極高。反正我沒(méi)有兑巾,而且還折騰了好多次才成功能条获。
問(wèn)題有以下幾個(gè):
問(wèn)題一:spring-framework-4.0.0.M3要用jdk1.8
如果你習(xí)憒于什么都下最新版的,那么你肯定想都沒(méi)想就下了spring-framework-4.0.0.M3蒋歌,而且又那么不好采帅掘,正好沒(méi)看README,那么問(wèn)題馬上就來(lái)了堂油,直接報(bào)"-XX:MaxMetaspaceSize=1024m"及"could not create the java Virtual machine"的錯(cuò)誤修档。
查看build.gradle和gradlew.bat中,可以找出原因:
build.gradle中:
compileJava?{
sourceCompatibility=1.6
targetCompatibility=1.6
}
compileTestJava?{
sourceCompatibility=1.8
targetCompatibility=1.8
}
可以看出這個(gè)版本的test代碼要求用1.8府框。
gradlew.bat中
set?GRADLE_OPTS=-XX:MaxMetaspaceSize=1024m?-Xmx1024m?-XX:MaxHeapSize=256m?%GRADLE_OPTS%
其中-XX:MaxMetaspaceSize是1.8的寫(xiě)法吱窝。
所以如果下載的是4.0的版本,最好把jdk也升級(jí)到1.8迫靖,要不就下載一下3.2.x的版本就好了院峡。
問(wèn)題2:在build過(guò)程中,出現(xiàn)heap內(nèi)存溢出情況
錯(cuò)誤信息如下:
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的內(nèi)存設(shè)置調(diào)大一點(diǎn)系宜,我做實(shí)驗(yàn)時(shí)照激,機(jī)器的內(nèi)存是8G、64位系統(tǒng)盹牧,直接使用
set GRADLE_OPTS=-XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=1024m %GRADLE_OPTS%
(我下載的是spring-framework-3.2.1.RELEASE版本俩垃,jdk1.7)
問(wèn)題3:gradle命令的使用
在spring源碼的根目錄下,提供gradlew.bat和
import-into-eclipse.bat這兩個(gè)命令汰寓,都可以build源碼口柳,但如果使用前者就不會(huì)生成eclipse的環(huán)境需要的一些文件,如下圖所示:
另外有滑,在使用import-into-eclipse.bat這個(gè)命令build時(shí)跃闹,可能會(huì)遇到一個(gè)問(wèn)題,錯(cuò)誤日志如下:
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項(xiàng)目時(shí),生成eclipse的.classpath文件時(shí)出錯(cuò)辣卒,打開(kāi)其它正常導(dǎo)入的項(xiàng)目的.classpath看下這個(gè)文件包含了哪些信息掷贾,如打開(kāi)spring-aop項(xiàng)目的:
而spring-webmvc-tiles3下根本就沒(méi)有.classpath的影子,沒(méi)得比較荣茫,雖然上面的錯(cuò)誤日志的E文都認(rèn)識(shí)想帅,但還是看不出它在講什么,不過(guò)大體能看得出是依賴(lài)出了問(wèn)題啡莉,那只能是想辦法順藤摸瓜了港准。重新打開(kāi)build.gradle,找到有關(guān)spring-webmvc-tiles3項(xiàng)目的描述咧欣,如下:
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}")
}
}
打開(kāi)C:/Users/so/.gradle/caches/artifacts-15/filestore浅缸,挨個(gè)依賴(lài)進(jìn)行檢查,jar包倒是最下載下來(lái)了魄咕,但是"org.apache.tiles:tiles-jsp:3.0.1"衩椒、"org.apache.tiles:tiles-extras:3.0.1"、"org.apache.tiles:tiles-el:3.0.1"三個(gè)卻沒(méi)有下載到source哮兰,其它包都有jar毛萌、pom、source三個(gè)目錄喝滞,這三個(gè)沒(méi)有source目錄阁将,只有jar、pom目錄右遭,問(wèn)題應(yīng)該是出現(xiàn)在這里做盅。
后來(lái)終于讓我找到了這么一個(gè)東西:http://issues.gradle.org/browse/GRADLE-1157,正好證實(shí)我前面的猜想窘哈,所以按照issue中的解決辦法吹榴,在build.gradle中加上如下部分:
eclipseClasspath?{
downloadSources?=false;//?required?for?eclipseClasspath?to?work
}
關(guān)于spring-webmvc-tiles3項(xiàng)目的腳本、就變成這樣了:
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
}
}
重新運(yùn)行命令滚婉,這一次終于讓我看到了BUILD SUCCESSFUL的字樣了……
成功build完成后腊尚,可以看到spring目錄下的spring-webmvc-tiles3項(xiàng)目也正常生成了導(dǎo)入eclipse需要的文件。打開(kāi).classpath查看一下:
果然沒(méi)有sourcepath的信息满哪。
折騰了那么久,終于到了可以看一下成果的時(shí)候了劝篷。
Spring Tool Suite說(shuō)簡(jiǎn)單一點(diǎn)就是Spring家的eclipse哨鸭,去官網(wǎng)下一個(gè)最新版,安裝上娇妓。
打開(kāi)STS像鸡,菜單File->import選擇General下面的Exsiting projects into workspace,勾選要導(dǎo)入的子項(xiàng)目,確定即可只估。
成果如上志群,齊齊整整,沒(méi)有錯(cuò)誤蛔钙,挺好锌云!