为什么在我构建项目时Gradle消除了依赖性 [英] Why gradle removes dependency when I build a project

查看:41
本文介绍了为什么在我构建项目时Gradle消除了依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么gradle对我这样做.我有一个看起来像这样的多项目:

I have no idea why gradle is doing this to me. I have a multiproject that looks like this:

|Parent
 |client
 |common
 |service

父项只是一个空项目,客户端,公共项和服务项都是gradle java项目:我有一些在客户端和服务中都使用的类,因此我想创建一个通用项目并构建一个jar,稍后将在服务和客户端中使用.我可以构建通用jar,但是每当我尝试添加对common的依赖"然后尝试刷新gradle"时,它将删除依赖并且无法构建!
这是我的工作:

Parent is just an empty project and client, common and service are gradle java projects: I have some classes that are used in both client and service, therefore I wanted to create a common project and build a jar that I would later use in both service and client. I can build the common jar, but whenever i try to do 'add dependency on common' and then try to 'refresh gradle', it removes the dependency and fails to build!
This is what I do:

然后按此,因为我要构建它:

Then I press this because I want to build it:

它只是消除了依赖性!

这是客户端项目中的build.gradle文件:

This is build.gradle file from client project:

plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}

group = 'sot.rest'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.springframework', name: 'spring-web', version: '5.2.4.RELEASE'
    implementation 'org.springframework.boot:spring-boot-starter'
    // https://mvnrepository.com/artifact/com.google.code.gson/gson
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
}

请帮助,我绝望!

推荐答案

在我以前的

Check out my answer on your previous question. It should give you an idea how to structure and declare the multi-module gradle projects.

我认为,当您使用IntelliJ添加对模块的依赖时,它只是通过IntelliJ中的项目设置将其添加到项目结构中.然后,当您单击刷新时,IntelliJ将基于Gradle文件配置项目.

I think that when you add the dependency on module with IntelliJ, it just adds it to the project structure through project settings in IntelliJ. And later, when you hit refresh, IntelliJ configures the project based on Gradle files.

要使其正常运行,父项目也应该是Gradle项目.如果不只是在父目录下添加 build.gradle settings.gradle .
然后在 settings.gradle 中添加子项目,例如:

To make it working, the Parent project should also be a Gradle project. If it isn't just add build.gradle and settings.gradle under the Parent directory.
Then in settings.gradle add the subprojects like:

rootProject.name = 'Parend'
include('common') //this adds the module
include('client')
include('service')

然后,在 client service 模块的 build.gradle 文件中,添加 common 模块作为依赖项与:

And later, in build.gradle files of client and service modules add the common module as dependency with:

dependencies {
    implementation project(':common')
    //...
}

如果您打算与Gradle进行更多合作,可以看看这篇文章关于Gradle的整体见解.

If you are going to work more with Gradle, you could take a look at this article about overall insight on Gradle.


(我了解到,当您使用实现时,它不会引发错误)

要使用Gradle处理多模块项目,根项目也必须是Gradle项目.根目录可能包含或不包含任何源代码,但它需要具有自己的Gradle文件.
因此,如果您的项目结构需要像这样:

To work with multimodule project with Gradle, the root project also needs to be a Gradle project. The root might or might not contain any source code, but it needs to have its own Gradle files.
So if your project structure needs to be like:

Root project 'parent'
+--- Project ':client'
+--- Project ':common'
\--- Project ':service'

然后,需要将 parent 和子模块项目设置为Gradle项目.要使其成为 parent 项目,至少需要具有一个 settings.gradle 文件,并声明其包含子模块,类似于:

Then the parent and submodule projects need to be set as Gradle projects. To make it the parent project needs to have at least a settings.gradle file and declared includes for submodules, similarly to:

rootProject.name = 'parent' 
include 'common'
include 'client'
include 'service'

每个模块( client common service )中的每个模块下都必须具有 build.gradle 文件目录.子模块使用 common ,因此 service client 必须在自己的 build中添加 common 作为依赖项.gradle 文件,例如:

Each of modules (client,common,service) must have a build.gradle files under its directory. Submodules using common, so the service and client must add the common as dependency in their own build.gradle files, like:

dependencies {
    implementation project(':common')
    //and rest of required dependencies
    //testCompile group: 'junit', name: 'junit', version: '4.12'
}

然后,您应该能够从这些子模块中的 common 导入公共类,并重建或重新导入项目而不会出现错误.

Then you should be able to import public classes from common in those submodules and rebuild or reimport project without error.

由于 parent 项目不包含任何源代码,因此它不需要自己的构建脚本,但是所有子模块的构建文件都需要在顶部声明java插件构建文件:

Since the parent project doesn't contain any source code, then it doesn't need its own build script, but then build file of all of the submodules needs to declare the java plugin on top of the build file:

plugins {
    id 'java'
}

由于您正在使用IntelliJ,并且您的项目以前可能具有不同的结构,所以现在可能会弄乱IntelliJ设置中的项目结构.
要解决此问题,您可以转到文件"->项目结构"->模块",然后再次删除/重新导入父模块.

Since you are working with IntelliJ and your project could have different structure previously, then the project structure in IntelliJ setting could be messed up now.
To fix it you could go to File->Project Structure->Modules and remove/reimport the parent module again.

如果您现在没有很多课程,我建议您创建一个新项目.在新项目"窗口中,选择Gradle,然后取消选中其他库和框架"中的Java.这将创建空白的Gradle项目.
生成项目后,右键单击 parent ,然后选择New-> Module.在新的模块窗口中,再次选择Gradle并选中Java(因为子模块将包含源代码).
这样,IntelliJ将自动将创建的模块包括到根/父项目的 settings.gradle 文件中,并且该模块的构建文件将包含基本配置(例如Java插件).
但是,您仍然可以在该模块的 build.gradle 文件中将一个模块的依赖性添加到另一个模块中.

If you don't have many classes now, I'd recommend you to create a new project. In the "New Project" window pick Gradle and uncheck Java in "Additional Libraries and Frameworks". This will create blank Gradle project.
After the project is generated, do a right mouse click on parent, and select New->Module. In new module window again pick Gradle and leave the Java checked (since the submodules will contain source code).
With that, the IntelliJ will automatically include created module to the settings.gradle file of root/parent project and the build file of that module will contain the basic configuration (e.g. the java plugin).
But you still add the dependency of one module in another in the build.gradle file of that module.

这篇关于为什么在我构建项目时Gradle消除了依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆