[英]Adding Google Play Services to non-gradle Android Studio project?
[英]How to convert a non-gradle JUnit project (that uses Hibernate and EntityManager) to a gradle Junit project and keep it working
我有一個JPA / Hibernate項目,該項目實現了許多存儲庫接口(我使用DDD),還有另一個項目通過使用JUnit 4測試第一個接口。我的IDE是Eclipse Luna。 后來我了解到,我可以使用運行時代碼和測試代碼來創建一個項目,但是此刻,我將它們分開在這兩個項目中。
到目前為止,一切工作正常。
現在,我決定將測試項目轉換為Gradle項目。 轉換后,我創建了這個build.gradle
文件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
repositories {
mavenLocal()
mavenCentral()
}
jar {
baseName = 'dominio.hibernate.tests'
version = '0.1.0'
}
dependencies {
compile 'junit:junit:4.11'
compile 'org.hamcrest:hamcrest-all:1.3'
runtime fileTree(dir: '../dominio/build/libs', include: '*.jar')
compile fileTree(dir: '../dominio/build/libs', include: '*.jar')
runtime fileTree(dir: '../dominio.hibernate/build/libs', include: '*.jar')
compile fileTree(dir: '../dominio.hibernate/build/libs', include: '*.jar')
compile 'postgresql:postgresql:9.0-801.jdbc4'
compile 'org.hibernate:hibernate-core:4.3.6.Final'
compile 'org.hibernate:hibernate-c3p0:4.1.0.Final'
compile "javax.enterprise:cdi-api:1.0-SP1"
compile "javax.servlet:servlet-api:2.5"
runtime "org.jboss.weld:weld-core:1.0.1-SP1"
compile project(':Dominio')
compile project(':DominioHibernate')
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
編譯工作正常,但是當我運行測試時, 無論是在Eclipse中還是在Gradle中 ,我都遇到以下異常
javax.persistence.PersistenceException: No Persistence provider for EntityManager named unidades
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at dominio.hibernate.tests.HibernateUnidadeOrganizacionalRepositoryTests.setUp(HibernateUnidadeOrganizacionalRepositoryTests.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
... the rest of the stack
我強調Java代碼中沒有任何變化,並且在Gradle出現之前,它可以完美運行。
因此,在將項目轉換為Gradle項目之后,似乎在JUnit運行期間無法再訪問persistence.xml
,並且我無法獲得執行測試所需的EntityManager
實例。
我瀏覽了很多有關JUnit,資源文件和Gradle的問題的帖子,但是沒人能給我一個解決問題的方法。
有任何想法嗎? 提前致謝!
為了成功運行使用Hibernate的測試,Hibernate資源和相關代碼必須放置在同一輸出目錄中:
sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
對於測試而言,這只是一個問題,因為它們的類路徑中有輸出目錄,而不是Jar / War。 (這避免了為運行測試而構建Jar / War的需要。)
感謝Peter Niederwiser的回答和評論, 這是StackOverflow上的帖子 ,以及有關Java插件的 Gradle文檔的一些讀物,使我能夠找出問題所在並加以解決。 以下是Detais:
彼得提出了有關更新主要和測試源集的resourceDir
建議(請參閱彼得的答案和他的評論)
在運行時環境中缺少一個Hibernate軟件包。 我更新了依賴項塊以添加它:
依賴項{...編譯'org.hibernate:hibernate-entitymanager:4.3.6.Final'運行時'org.hibernate:hibernate-entitymanager:4.3.6.Final'...}
只有這兩個步驟相結合才能解決我的問題。 META-INF文件夾已復制到正確的位置,並且存在(現在存在)Hibernate程序包用於處理persistence.xml
,以便創建我的測試代碼所需的EntityManager
實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.