學習Hibernate原始碼一_下載hibernate原始碼並編譯為eclipse專案

bsr1983發表於2013-08-23

學習Hibernate原始碼一

1.下載hibernate原始碼,並構建起eclipse專案

1hibernate的原始碼採用git管理,安裝git客戶端以後,通過點選https://github.com/hibernate/hibernate-orm頁面右側的“Clone in Desktop”可啟動客戶端將程式碼clone到本地。當然也可以直接使用

git clone git://github.com/hibernate/hibernate-orm.git

命令下載程式碼。

下載完畢後,開啟根目錄下的readme檔案,可以看到詳細的把原始碼構建為不同IDE專案的說明;以及其他的一些資訊。

對應文件的網站連結:

https://community.jboss.org/wiki/BuildingHibernateORM4x5x

構建IDE部分:

Eclipse

To create the Eclipse project files you can run

./gradlew eclipse

After changes to the dependencies you need to clean the project files and recreate them:

./gradlew cleanEclipse eclipse

 

See also  Contributing to Hibernate using Eclipse

Idea

To create the Idea project files you can run

./gradlew idea

After changes to the dependencies you need to clean the project files and recreate them:

./gradlew cleanIdea idea

此處的gradlehibernate所使用的自動構建工具,官網地址為:

http://www.gradle.org/

下載地址為:http://www.gradle.org/downloads,選擇一個版本進行下載

此處是Hibernate團隊闡述其從Maven遷移到Gradle的原因:https://community.jboss.org/wiki/GradleWhy

至於grable,有時間會深入學習一下,感興趣的同學可以看看這位博主的部落格:

http://www.blogjava.net/wldandan/archive/2012/06/26/381532.html

言歸正傳,下載grable後,解壓,我將其解壓到了C盤根目錄,解壓完畢的grable所在目錄為:C:\gradle-1.7,將其子目錄C:\gradle-1.7\bin目錄加入系統的Path變數

編譯程式碼為eclipse專案:

1)進入hibernate原始碼的根目錄:

cd C:\Users\Administrator\Documents\GitHub\hibernate-orm

2)執行gradlew.bat eclipse

接著會自動下載專案所需要的依賴項,具體輸出如下:

C:\Users\Administrator\Documents\GitHub\hibernate-orm>gradlew.bat eclipse

Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on

Deprecated dynamic property: "exportPackageVersion" on "project ':documentation'", value: "4.3.0".

Deprecated dynamic property "exportPackageVersion" created in multiple locations.

FAILURE: Build failed with an exception.

* Where:

Build file 'C:\Users\Administrator\Documents\GitHub\hibernate-orm\build.gradle' line: 85

* What went wrong:

A problem occurred evaluating root project 'hibernate-orm'.

> org/hibernate/build/gradle/publish/auth/maven/AuthenticationManager : Unsupported major.minor version 51.0

* Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

此時我去檢視我的環境變數,JAVA_HOME指向的是64位的JDK6,將其指向32JDK7後再次執行

gradlew.bat eclipse

發現此時gradlew又去下載了其他的依賴包,檢視hibernate-rom目錄下的libraries.gradle檔案,可以看到hibernate的依賴包。

重新編譯過程中還是報錯了,具體錯誤:

:hibernate-core:compileJava

警告: [options] 未與 -source 1.6 一起設定引導類路徑

C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\Loader.java:38: 錯誤: 編碼GBK的不可對映字元

 * @author L錕絪zl?Benke

                 ^

C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\ResultCheckStyle.java:29: 錯誤: 編碼GBK的不可對映字元

 * @author L錕絪zl?Benke

                 ^

C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\SQLDelete.java:37: 錯誤: 編碼GBK的不可對映字元

 * @author L錕絪zl?Benke

                 ^

C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\SQLDeleteAll.java:37: 錯誤: 編碼GBK的不可對映字元

 * @author L錕絪zl?Benke

                 ^

C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\SQLInsert.java:37: 錯誤: 編碼GBK的不可對映字元

 * @author L錕絪zl?Benke

                 ^

C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\SQLUpdate.java:37: 錯誤: 編碼GBK的不可對映字元

 * @author L錕絪zl?Benke

                 ^

: 某些輸入檔案使用或覆蓋了已過時的 API

: 有關詳細資訊, 請使用 -Xlint:deprecation 重新編譯。

: 某些輸入檔案使用了未經檢查或不安全的操作。

: 有關詳細資訊, 請使用 -Xlint:unchecked 重新編譯。

1 個警告

google了一下,貌似是區域語言設定的問題,需要修改為“英語(英國)”,但編譯並沒用中斷,接著編譯。我將控制皮膚中的區域語言設定修改後,繼續觀察,看其是否還會報同樣的錯誤。

最終還是失敗了:

:hibernate-entitymanager:compileJava

警告: [options] 未與 -source 1.6 一起設定引導類路徑

C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-entitymanager\src\main\java\org\hibernate\jpa\AvailableSettings.java:290: 錯誤: 編碼GBK的不可對映字元

         * contains 鈥淐REATE SCHEMA?commands.  If this property is not supplied (or is explicitly {@code false}), the

                                   ^

: 某些輸入檔案使用或覆蓋了已過時的 API

: 有關詳細資訊, 請使用 -Xlint:deprecation 重新編譯。

: 某些輸入檔案使用了未經檢查或不安全的操作。

: 有關詳細資訊, 請使用 -Xlint:unchecked 重新編譯。

1 個警告

:hibernate-entitymanager:processResources

:hibernate-entitymanager:classes

:hibernate-testing:generateMainLoggingClasses

Download http://repo1.maven.org/maven2/com/experlog/xapool/1.5.0/xapool-1.5.0.jar

Download http://repo1.maven.org/maven2/org/jboss/jbossts/jbossjta/4.16.4.Final/jbossjta-4.16.4.Final.jar

:hibernate-testing:compileJava

警告: [options] 未與 -source 1.6 一起設定引導類路徑

: 某些輸入檔案使用或覆蓋了已過時的 API

: 有關詳細資訊, 請使用 -Xlint:deprecation 重新編譯。

: 某些輸入檔案使用了未經檢查或不安全的操作。

: 有關詳細資訊, 請使用 -Xlint:unchecked 重新編譯。

1 個警告

:hibernate-testing:processResources UP-TO-DATE

:hibernate-testing:classes

:hibernate-testing:jar

:hibernate-entitymanager:generateTestJpaMetamodelClasses

此處略去部分download資訊

Download http://repo1.maven.org/maven2/org/jboss/shrinkwrap/shrinkwrap-build/1.0.0-alpha-11/shrinkwrap-build-1.0.0-alpha-11.pom

 

FAILURE: Build failed with an exception.

 

* What went wrong:

Could not resolve all dependencies for configuration ':hibernate-entitymanager:testCompile'.

> Could not resolve org.jboss.arquillian:arquillian-api:1.0.0.Alpha4.

  Required by:

      org.hibernate:hibernate-entitymanager:4.3.0-SNAPSHOT > org.jboss.weld.arquillian.container:arquillian-weld-ee-embedded-1.1:1.1.2.Final > org.jboss.arquillian:arquillian-spi:1.0.0.Alpha4

   > Could not GET 'http://repository.jboss.org/nexus/content/groups/public/org/jboss/arquillian/arquillian-api/1.0.0.Alpha4/arquillian-api-1.0.0.Alpha4.pom'.

      > The target server failed to respond

 

* Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 20 mins 17.755 secs

猜測還是和區域和語言有關,因為之前已經在控制皮膚修改過,重新進入hibernate-rom根目錄執行gradlew.bat eclipse再次嘗試編譯。

又是一堆download資訊後,編譯開始,盯著編譯的命令視窗的輸出,關注其是否還會因為編碼報錯。

:hibernate-osgi:eclipseJdt

:hibernate-osgi:eclipseProject

:hibernate-osgi:eclipse

:hibernate-proxool:generateMainLoggingClasses

Download http://repo1.maven.org/maven2/proxool/proxool/0.8.3/proxool-0.8.3.pom

Download http://repo1.maven.org/maven2/proxool/proxool/0.8.3/proxool-0.8.3.jar

:hibernate-proxool:generateSources

:hibernate-proxool:eclipseClasspath

:hibernate-proxool:eclipseJdt

:hibernate-proxool:eclipseProject

:hibernate-proxool:eclipse

:hibernate-testing:generateSources UP-TO-DATE

:hibernate-testing:eclipseClasspath

Download http://repo1.maven.org/maven2/org/jboss/jbossts/jbossjta/4.16.4.Final/jbossjta-4.16.4.Final-sources.jar

Download http://repo1.maven.org/maven2/com/experlog/xapool/1.5.0/xapool-1.5.0-sources.jar

:hibernate-testing:eclipseJdt

:hibernate-testing:eclipseProject

:hibernate-testing:eclipse

:release:eclipseProject

:release:eclipse

 

BUILD SUCCESSFUL

 

Total time: 9 mins 13.742 secs

最終,編譯成功了。

總結:

1)編譯需要使用JDK7,估計Hibernate中使用了JDK7的新特性,應該是關於try-rource的使用會比較多,接下來需要從原始碼中找一下證據。

2)需要將區域和語言修改為“英語(英國)”。

3)編譯時需要聯網下載依賴項,因此網速會影響最終的編譯時間。

編譯完畢後匯入專案到eclipse之前,先修改一下JAVA_HOME,需要和你的eclipse的版本一致,否則eclipse無法正常啟動。

由於專案較多,匯入的過程也會比較緩慢。需要耐心等待,如果想嘗試修改hibernate原始碼並編譯檢視效果的話,匯入時建議勾選copy projects into workspace,將原始碼拷到你指定的工作空間。

 

 

相關文章