简体   繁体   English

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

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

I have several projects (let's say 15).我有几个项目(比如说 15 个)。 To make it easy let's consider each is like a library that generate one jar.为了简单起见,让我们认为每个都像一个生成一个 jar 的库。 For this reason there is no "root" project.因此,没有“根”项目。 Any project can have dependencies on any other (without making circular dependencies of course).任何项目都可以依赖于任何其他项目(当然不会产生循环依赖)。

A developer could be working on only one project, and only have its dependencies as jars that can be in a repository.开发人员可能只在一个项目上工作,并且其依赖项只有 jars 可以在存储库中。

But they could also need to modify something in one of the dependency project and import it into their Eclipse.但他们也可能需要修改其中一个依赖项目中的某些内容并将其导入到他们的 Eclipse 中。

At this point is there any way to define this dependency so that it will end up as a Project in the Eclipse build path?此时是否有任何方法可以定义此依赖项,使其最终成为 Eclipse 构建路径中的项目? I want Project_2 to use the live code from Project_1, not a generated jar.我希望 Project_2 使用来自 Project_1 的实时代码,而不是生成的 jar。

It seems to me like a very basic requirement, yet I could not find anything related to that except a 7 year old question without answer.在我看来,这是一个非常基本的要求,但除了一个没有答案的 7 岁问题外,我找不到与此相关的任何内容。

I've seen solutions that more or less work with gradle, but they still don't generate the build path correctly and all implies a root project.我已经看到或多或少与 gradle 一起使用的解决方案,但它们仍然无法正确生成构建路径,并且都意味着一个根项目。 There is no root project, someone working on Project_1 and Project_2 should not have to import Project_3 to 15 in their IDE just so that Gradle works correctly.没有根项目,在 Project_1 和 Project_2 上工作的人不必在 IDE 中将 Project_3 导入到 15,这样 Gradle 才能正常工作。

An easy way to switch between a jar or a project in settings.gradle would be ideal.在 jar 或 settings.gradle 中的项目之间切换的简单方法是理想的。

Update: Composite builds did not work last time I tried, but it looks better now.更新:我上次尝试的复合构建不起作用,但现在看起来好多了。 Still only works at one level.仍然只在一个级别上工作。

/Project_A/build.gradle /Project_A/build.gradle

plugins {
    id 'java-library'
}

dependencies {
}

/Project_B/build.gradle /Project_B/build.gradle

plugins {
    id 'java-library'
}

dependencies {
    implementation 'undefined:project-A'
}

/Project_B/settings.gradle /Project_B/settings.gradle

rootProject.name = 'Project_B'

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

This seems to work.这似乎有效。 I can see the project in the dependencies (not the same as adding an actual project, but it looks like it does the job).我可以在依赖项中看到该项目(与添加实际项目不同,但看起来它可以完成工作)。

/Project_C/build.gradle /Project_C/build.gradle

plugins {
    id 'java-library'
}

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

/Project_C/settings.gradle /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(':')
    }
}

This does not work because Multiple projects in the build are located in the same directory: C:\Workspace\Project_A这不起作用,因为构建中的多个项目位于同一目录中:C:\Workspace\Project_A

It's ok in case I need to work on Project A, B and C.如果我需要在项目 A、B 和 C 上工作,没关系。 But if I wanted to only use Project C and A, and use B from a jar?但是,如果我只想使用项目 C 和 A,并使用 jar 中的 B?

And it's the simplest version, I have a lot more projects with much more complex dependencies.它是最简单的版本,我有更多的项目具有更复杂的依赖关系。

This is the best result I got so far though.这是迄今为止我得到的最好的结果。

I also tried using "project()" instead of "implementation" in the dependencies, or to include project in the settings.gradle.我还尝试在依赖项中使用“project()”而不是“实现”,或者在 settings.gradle 中包含项目。

I'm not sure if using "api" instead of "implementation" would have an effect.我不确定使用“api”而不是“实现”是否会产生影响。

Is there a way to write this include as a more complex function that would use the project or the jar according to a variable in settings.gradle, without having to comment/uncomment every includeBuild in the projects to make it work depending on the situation?有没有办法根据 settings.gradle 中的变量将其写成更复杂的 function 使用项目或 jar。

Is there a way to refer to the projects by just their (unique) name or coordinates and not their path?有没有办法仅通过项目的(唯一)名称或坐标而不是路径来引用项目?

Maybe I need to write a dependency resolver?也许我需要编写一个依赖解析器?

I am having the same troubles, for me I've got a working solution by including project as dependency not as included build.我遇到了同样的麻烦,对我来说,通过将项目包含为依赖项而不是包含的构建,我得到了一个可行的解决方案。

my settings.gradle in one project is looking like this:我在一个项目中的 settings.gradle 看起来像这样:

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

Then I manipulated my dependencies with:然后我操纵了我的依赖关系:

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

The idea is like, "am I building on my local computer", then use the project instead of artifact.这个想法就像,“我是在我的本地计算机上构建”,然后使用项目而不是工件。

Ontop I've created a task, for jar file used as loadable plugin.在顶部,我为用作可加载插件的 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"
    }
}

This task is copying the built jar and needed libraries on the main project.此任务是在主项目上复制构建的 jar 和所需的库。 You could follow this solution as well, to build your libs locally and then let it copy to a well know library path.您也可以遵循此解决方案,在本地构建您的库,然后将其复制到众所周知的库路径。

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

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