2013-02-07 62 views
1

我的項目基於Spring + JPA + Hibernate,並在之前使用Ant來管理構建過程,現在我打算使用Gradle。當運行'gradle clean test'時,我得到一個異常,如下所示:在Gradle中進行集成測試時沒有發現持久化類

[2013-02-07 11:01:36,703] [Test worker] [WARN] [QuerySplitter]找不到查詢的持久化類class:from com.mpos.lottery.te.workingkey.domain.WorkingKey w其中w.createDateStr =:createDateStr和w.gpeId =:gpeId

[2013-02-07 11:01:36,718] [Test worker ] [錯誤] [TEPortServlet] org.hibernate.QueryParameterException:無法找到命名參數[gpeId];嵌套的例外是java.lang.IllegalArgumentException異常:org.hibernate.QueryParameterException:無法找到名爲參數[gpeId]

我已經跑了測試用例(不需要碼頭)在Eclipse和螞蟻,它通過,所以我懷疑gradle中的一些不正確的配置會導致這個異常。

下面是我的腳本的gradle:

apply plugin: 'war' 
// 'war' plugin will apply 'java' plugin automatically 
apply plugin: 'java' 
apply plugin: 'eclipse' 
// run web application, refer to http://gradle.org/docs/current/userguide/jetty_plugin.html 
apply plugin: 'jetty' 

compileJava.options.encoding = _sourcecode_encoding 
compileTestJava.options.encoding = _sourcecode_encoding 

// Properties added by the java plugin 
sourceCompatibility="${_source_compatibility}" 
targetCompatibility="${_target_compatibility}" 
//Properties added by the 'war' plugin 
webAppDirName="src/main/WWW" 

configurations { 
    provided { 
     description = 'Non-exported comiple-time dependencies, it will be provided by container.' 
    } 
} 

dependencies { 
    provided files('lib/DEV/j2ee/servlet-api.jar') 
    compile fileTree(dir: 'lib', include: '**/*.jar', exclude: 'DEV/**/*.jar') 
    //compile sourceSets.main.output 
    testCompile fileTree(dir:"lib", include:"DEV/**/*.jar") 
} 

sourceSets { 
    main { 
     compileClasspath = compileClasspath + configurations.provided 
     //compileClasspath.collect().each({println it}) 
     resources { 
      srcDir 'src/main/resource' 
     } 
    } 
    test { 
     resources { 
      srcDir 'src/test/resource' 
     } 
    } 
} 

test { 
    scanForTestClasses = false 
    classpath = configurations.provided + configurations.compile + configurations.testCompile + sourceSets.main.output + sourceSets.test.output 
    classpath.each({println it}) 
    // customize test process 
    include 'com/mpos/lottery/te/gameimpl/smsraffle/**/*Test.class' 
} 

我搖籃和Groovy,請幫助我的新學員。提前致謝。

回答

2

默認情況下,Gradle對類和資源使用單獨的輸出目錄,這會導致Hibernate/JPA出現問題。嘗試:

sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir 
sourceSets.test.output.resourcesDir = sourceSets.test.output.classesDir 

您可能不需要後者。

+0

非常感謝你,彼得。它解決了我的問題,但我不明白爲什麼會出現這種情況,因爲我已經在「測試」任務中打印出了類路徑,並且類路徑確實包含了所有這些目錄(構建\ classes \ main,構建\ resources \ main,構建\類\測試,建立\資源\測試)。 – Ramon

+0

簡而言之,JPA需要類和資源駐留在同一個目錄中。 –