2

我目前正在编辑 gradle 项目(“数据库”和“主数据”)。Masterdata 依赖于数据库项目。数据库发布到 Nexus 服务器,masterdata 从那里加载它作为依赖项。

masterdata的build.gradle

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")
    }
}

当我在数据库项目中更改某些内容时,它需要完整的构建、发布等,直到我看到 masterdata 项目中的更改。在我之前工作的公司中,我们使用 maven 进行了类似的设置。在那里,我立即看到了依赖项的变化,而没有先发布它们。这也可以用gradle吗?也许通过多项目构建?

基本上,.classpath 中缺少以下条目:

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

有没有办法自动生成它。

更新:作为一种解决方法,我将条目手动添加到 .classpath

4

1 回答 1

2

我做了一些额外的搜索,目前这仅适用于多项目构建。基本上,您需要在一个巨大的多项目构建中完成所有项目。在那里,您可以随意引用它们,并在 eclipse 中获得正确的依赖关系。

在没有多项目构建的情况下,功能请求存在jira 问题。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'

构建.gradle

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

这适用于 gradle 构建和 eclipse。唯一的缺点是每次签出依赖于它的项目时,您总是必须签出子项目。我相信有人可以构建一些花哨的常规逻辑来解决这个问题。

于 2013-07-10T12:00:07.417 回答