繁体   English   中英

在 Eclipse 中使用 Gradle 在构建路径中的其他项目

[英]Using other projects in build path with Gradle in Eclipse

我有几个项目(比如说 15 个)。 为了简单起见,让我们认为每个都像一个生成一个 jar 的库。 因此,没有“根”项目。 任何项目都可以依赖于任何其他项目(当然不会产生循环依赖)。

开发人员可能只在一个项目上工作,并且其依赖项只有 jars 可以在存储库中。

但他们也可能需要修改其中一个依赖项目中的某些内容并将其导入到他们的 Eclipse 中。

此时是否有任何方法可以定义此依赖项,使其最终成为 Eclipse 构建路径中的项目? 我希望 Project_2 使用来自 Project_1 的实时代码,而不是生成的 jar。

在我看来,这是一个非常基本的要求,但除了一个没有答案的 7 岁问题外,我找不到与此相关的任何内容。

我已经看到或多或少与 gradle 一起使用的解决方案,但它们仍然无法正确生成构建路径,并且都意味着一个根项目。 没有根项目,在 Project_1 和 Project_2 上工作的人不必在 IDE 中将 Project_3 导入到 15,这样 Gradle 才能正常工作。

在 jar 或 settings.gradle 中的项目之间切换的简单方法是理想的。

更新:我上次尝试的复合构建不起作用,但现在看起来好多了。 仍然只在一个级别上工作。

/Project_A/build.gradle

plugins {
    id 'java-library'
}

dependencies {
}

/Project_B/build.gradle

plugins {
    id 'java-library'
}

dependencies {
    implementation 'undefined:project-A'
}

/Project_B/settings.gradle

rootProject.name = 'Project_B'

includeBuild('../Project_A'){
    dependencySubstitution{
        substitute module('undefined:project-A') with project(':')
    }
}

这似乎有效。 我可以在依赖项中看到该项目(与添加实际项目不同,但看起来它可以完成工作)。

/Project_C/build.gradle

plugins {
    id 'java-library'
}

dependencies {
    implementation 'undefined:project-A'
    implementation 'undefined:project-B'
}

/Project_C/settings.gradle

rootProject.name = 'Project_C'

includeBuild('../Project_A'){
    dependencySubstitution{
        substitute module('undefined:project-A') with project(':')
    }
}

includeBuild('../Project_B'){
    dependencySubstitution{
        substitute module('undefined:project-B') with project(':')
    }
}

这不起作用,因为构建中的多个项目位于同一目录中:C:\Workspace\Project_A

如果我需要在项目 A、B 和 C 上工作,没关系。 但是,如果我只想使用项目 C 和 A,并使用 jar 中的 B?

它是最简单的版本,我有更多的项目具有更复杂的依赖关系。

这是迄今为止我得到的最好的结果。

我还尝试在依赖项中使用“project()”而不是“实现”,或者在 settings.gradle 中包含项目。

我不确定使用“api”而不是“实现”是否会产生影响。

有没有办法根据 settings.gradle 中的变量将其写成更复杂的 function 使用项目或 jar。

有没有办法仅通过项目的(唯一)名称或坐标而不是路径来引用项目?

也许我需要编写一个依赖解析器?

我遇到了同样的麻烦,对我来说,通过将项目包含为依赖项而不是包含的构建,我得到了一个可行的解决方案。

我在一个项目中的 settings.gradle 看起来像这样:

   if (file('../otherProject').exists()) {
        include ':otherProject'
        project(':otherProject').projectDir = new File(settingsDir,'../otherProject')
    }

然后我操纵了我的依赖关系:

dependencies {
   if (file("../otherProject").exists()){
        api project(":otherProject")
    } else {
        api "foo.bar.otherProject:1.0.0-SNAPSHOT"
    } 
}

这个想法就像,“我是在我的本地计算机上构建”,然后使用项目而不是工件。

在顶部,我为用作可加载插件的 jar 文件创建了一个任务。

task copyPluginToMainProject (type: Copy, group: "distribution" , description: "copy built plugin to Main Project", dependsOn: 'jar') {
    String destinationDir = "../mainProject/plugins/dir"
    into destinationDir
    from ("build/libs")
// these are additional libraries needed for the plugin running well 
    into ("/lib") {
      from configurations.runtimeClasspath include "mariadb*.jar"
    }
}

此任务是在主项目上复制构建的 jar 和所需的库。 您也可以遵循此解决方案,在本地构建您的库,然后将其复制到众所周知的库路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM