2015-01-31 30 views
3

我有一個搖籃的Java Web項目正在開發中Wildfly具有以下結構運行的src /主/資源persistence.xml中:告訴gradle這個去摘的src /測試/資源,而不是

- src/main/java 
- src/main/resources 
    \-- META/INF 
     \-- persistence.xml 
- src/test/java 
- stc/test/resources 
    \-- META/INF 
     \-- persistence.xml 

而以下build.gradle

apply plugin: 'war' 
apply plugin: 'eclipse-wtp' 

sourceCompatibility = 1.8 
version = '1.0' 
jar { 
    manifest { 
     attributes 'Implementation-Title': 'Arch Project', 'Implementation-Version': version 
    } 
} 

repositories { 
    mavenCentral() 
} 

configurations { 
    provided 
} 

sourceSets { 
    main.compileClasspath += configurations.provided 
    test.compileClasspath += configurations.provided 
    test.runtimeClasspath += configurations.provided 
} 

dependencies { 

    /* Java EE 7 API. */ 
    provided 'javax:javaee-api:7.0' 

    /* JUnit for unit testing. */ 
    testCompile 'junit:junit:4.1.2' 

    /* DbUnit - Persitence unit testing. */ 
    testCompile 'org.dbunit:dbunit:2.5.0' 
    testCompile 'org.slf4j:slf4j-simple:1.7.9' 

    /* Hibernate. */ 
    testCompile 'org.hibernate:hibernate-core:4.3.7.Final' 

    /* Hibernate provider for JPA. */ 
    testCompile 'org.hibernate:hibernate-entitymanager:4.3.7.Final' 

    /* Hibernate dependency. */ 
    testCompile 'com.fasterxml:classmate:1.1.0' 

    /* PostgreSQL for persistence tests context */ 
    testCompile 'org.postgresql:postgresql:9.3-1102-jdbc41' 
} 

persistence.xml文件中的源文件夾src/test/resources是一個,我想,當我跑我的測試中使用,而不是另一名來自src/main/resources
我需要這樣做,因爲src/main/resources中的persistence.xml依賴於Java EE應用程序服務器中定義的JTA數據源,所以我相信它不會用於本地執行。
我該如何配置它才能通過命令gradle test在Eclipse和Gradle內成功運行測試?

在此先感謝。

+0

它是否幫助你以某種方式:http://forums.gradle.org/gradle/topics/resources_from_src_main_resources_not_included_for_test? – Opal 2015-02-01 08:59:05

+0

Thx @Opal但它沒有幫助。如果我使用src/main/resources,那麼問題是我想從src/test/resources中使用'persistence.xml'。 – 2015-02-03 15:35:09

回答

1

來解決此問題請按照下列指示:

在Eclipse中的項目屬性,Java構建路徑 - >訂單和出口,把你上面的src文件測試文件夾。

還有傳言稱,另一個問題:Use different JPA.persistence.xml in Eclipse depending on production or test environment

+0

「以及談論另一個問題:」你能說清楚你的意思嗎?你的意思是「請也按照這裏給出的指示」?但無論如何,這裏有不止一個答案,你是指哪一個?我想[這一個](http://stackoverflow.com/a/31923699/3982001),因爲它像你的開始,但仍然不清楚。 – 2015-09-03 16:56:43

+0

如果將測試文件夾放在src文件夾的上方,您的測試將在eclipse中運行。點擊Eclipse項目中的rig按鈕,屬性,Java Build Path,然後轉到選項卡Order and Export。將會有你的資源和資源,而不是你將src/test/resource移到src/main/resource上面。因此,Eclipse將首先將persistence.xml找到測試資源,測試將與Eclipse IDE中的測試persistence.xml一起使用。 – 2015-09-04 10:49:24

相關問題