【问题标题】:Using other projects in build path with Gradle in Eclipse在 Eclipse 中使用 Gradle 在构建路径中使用其他项目
【发布时间】:2020-12-02 12:01:27
【问题描述】:

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

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

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

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

在我看来这是一个非常基本的要求,但除了一个 7 年前没有答案的问题,我找不到任何相关的东西。

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

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

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

/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”而不是“implementation”是否会产生效果。

有没有办法将此包含编写为更复杂的函数,该函数将根据 settings.gradle 中的变量使用项目或 jar,而无需注释/取消注释项目中的每个 includeBuild 以使其工作取决于情况?

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

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

【问题讨论】:

标签: java eclipse gradle


【解决方案1】:

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

我在一个项目中的 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 和所需的库。 您也可以遵循此解决方案,在本地构建您的库,然后将其复制到众所周知的库路径。

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 2011-11-04
    • 2011-08-06
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多