2013-07-10 52 views
2

我目前處於編輯gradle項目(「數據庫」&「masterdata」)的情況。 Masterdata取決於數據庫項目。將數據庫發佈到Nexus服務器,並將其作爲依賴項加載到masterdata中。在eclipse中缺少項目依賴關係

masterdata

import org.gradle.plugins.ide.eclipse.model.SourceFolder 

apply plugin: "java" 
apply plugin: "eclipse" 

sourceCompatibility = 1.7 
version = '0.1-SNAPSHOT' 
group = "net.example" 

def nexusHost = "http://nexus:8081" 

repositories { 
    logger.lifecycle("Configuration: Repositories") 
    maven { 
      url nexusHost + "/nexus/content/groups/public" 
    } 
} 


dependencies { 
    logger.lifecycle("Configuration: Dependencies") 

    compile 'net.example:database:0.1-SNAPSHOT' // project where the changes happen 
    compile 'com.google.guava:guava:14.0.1' 

    testCompile 'ch.qos.logback:logback-classic:1.0.13' 
    testCompile 'org.testng:testng:6.8.5' 
    testCompile 'org.dbunit:dbunit:2.4.9' 
    testCompile 'org.mockito:mockito-all:1.9.5' 
    testCompile 'org.easytesting:fest-assert-core:2.0M10' 
    testCompile 'org.hsqldb:hsqldb:2.2.9' 

} 

eclipse.classpath.file { 
    beforeMerged { classpath -> 
     classpath.entries.clear() 
     logger.lifecycle("Classpath entries cleared") 
    } 
    whenMerged { cp -> 
     cp.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/main/") }*.output = "bin/main" 
     cp.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/test/") }*.output = "bin/test" 
     cp.entries.removeAll { it.kind == "output" } 
     logger.lifecycle("Classpath entries modified") 
    } 
} 

當我改變它需要一個完整的構建數據庫項目的東西的的build.gradle,發佈等,直到我看到masterdata項目的變化。在我以前工作過的公司裏,我們用maven做了類似的設置。在那裏,我立即看到依賴關係的變化,而不是首先發布它們。這也可以用gradle嗎?也許通過多項目構建?

基本上由下列項從丟失的.classpath:

<classpathentry combineaccessrules="false" kind="src" path="/database"/> 

是否有辦法來自動它的產生。

更新:作爲一種變通方法我手動添加條目到的.classpath

回答

2

我做了一些額外的搜索,目前這是唯一可能與多項目構建。基本上,您需要在一個龐大的多項目構建中完成所有項目。在那裏你可以按照你的喜好來引用它們,並且在eclipse中看到正確的依賴關係。

有一個jira issue功能請求,使沒有多項目構建可能。 eclipse的自定義邏輯只會幫助構建eclipse,因爲在gradle構建中,它會使用缺少更改的存儲庫中的依賴項。您需要確保在構建主項目之前構建和發佈所有更改的依賴關係。

Eclipse的解決方法:

eclipse.classpath.file { 
    whenMerged { cp -> 
     // remove library dependencies 
     def toBeRemoved = cp.entries.findAll { it instanceof Library 
       && ((Library) it).library.path.contains('someProject') } 

     //configure the project dependencies: 
     def toBeAdded = [ new ProjectDependency('/someProject', null)] 

     cp.entries -= toBeRemoved 
     cp.entries += toBeAdded 
    } 
} 

做手工打造的Gradle時,這仍然會失敗,但如果你使用CI系統具有良好的建造順序你應該罰款。

解決多項目生成:

創建多項目構建是很容易,我想,它也可以當「子項目」是在同一水平上。

settings.gradle

includeFlat 'database' 

的build.gradle

dependencies { 
    ... 
    compile project(':database') 
    ...   
} 

這行之有效的gradle與構建和使用Eclipse。唯一的缺點是,每次檢出依賴於該項目的項目時,都必須檢查子項目。我確信有人可以建立一些奇特的groovy邏輯來解決這個問題。

+0

我收到以下錯誤: 您無法從抽象接口'org.gradle.api.artifacts.ProjectDependency'中創建實例。 –

+0

@Bernard我有同樣的問題,它有助於添加'import org.gradle.plugins.ide.eclipse.model。ProjectDependency'放在我的.gradle文件的頂部,並添加'apply plugin:'eclipse''。 – zpon