2013-07-10 54 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