依赖 jar 时未导入 Java-Library 依赖项

标签 java gradle

我正在尝试将一些常用模块移动到他们自己的项目中,以便可以将它们上传到私有(private)存储库并在应用程序中使用。目前,它们都在一个多项目配置中,一切都按预期工作。我把它们搬走了,一切似乎都很好。当我在我的应用程序中将它们用作依赖项时,我收到错误消息 java.lang.module.FindException: Module X not found, required by COMMON-MODULE-Y .

我的印象是 java-library插件允许我的应用程序导入声明的依赖项所需的那些辅助依赖项,但这不是正在发生的事情。

我如何打包我常用的模块以允许这样做,或者我如何配置我的项目以允许这样做?我为此使用 Java 11。

EDIT



基本上我希望我的库是自包含的,并且使用它们的应用程序不包含任何其他库。

这是我的build.gradle对于我遇到上述错误的模块。
plugins {
    id 'java-library'
    id 'idea'
}

if(project.hasProperty('javaVer') && javaVer == '8') {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}

idea {
    module {
        inheritOutputDirs = true
    }
}

def currentOS = org.gradle.internal.os.OperatingSystem.current()
def depPlatform

if (!project.hasProperty(('platform'))) {
    if (currentOS.isWindows()) {
        depPlatform = 'win'
    } else if (currentOS.isLinux()) {
        depPlatform = 'linux'
    } else if (currentOS.isMacOsX()) {
        depPlatform = 'mac'cd
    }
}else {
    depPlatform = project.getProperties().get("platform");
}

sourceSets.main {
    java {
        srcDir 'src/main/java' //assume that your source codes are inside this path
    }
    resources {
        srcDirs = ['src/main/java', 'src/main/resources']
        exclude "**/*.java"
    }
}

ext.moduleName = 'license'

dependencies {
    implementation project(':common')
    implementation "org.openjfx:javafx-base:11.0.2:${depPlatform}"
    implementation 'commons-io:commons-io:2.6'
    implementation 'org.apache.commons:commons-lang3:3.7'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.github.purejavacomm:purejavacomm:1.0.1.RELEASE'
}

这里是 build.gradle对于多项目,上述模块位于。
subprojects {
    afterEvaluate {
        repositories {
            mavenCentral()
            jcenter()
        }

        compileJava {
            if (project.hasProperty(('javaVer')) && javaVer == '8') {
                excludes = ['**/module-info.java']
            } else {
                doFirst {
                    options.compilerArgs = [
                        '--module-path', classpath.asPath,
                    ]
                }
            }
        }

        jar {
            archiveFileName = project.name + '-' + (project.hasProperty(('releaseSpec')) ? project.releaseSpec : 'SNAPSHOT') + '.jar'
        }
    }
}

最佳答案

简短回答:你想expose runtime dependencies .例如。它们是应用程序执行所必需的,但它们不应该可用于编译。

请使用以下可行的代码:

settings.gradle.kts:

include(":application")
include(":libraryA")
include(":libraryB")

应用程序/gradle.build.kts:
plugins {
    application
    kotlin("jvm") version "1.3.61"
}

dependencies {
    api(project(":libraryA")) // we reference only library, we know nothing about the dependencies.
}

库A/gradle.build.kts:
plugins {
    kotlin("jvm") version "1.3.61"
}

dependencies {
    api(project(":libraryB")) // again, we know nothing about the dependencies
}

库B/gradle.build.kts:
plugins {
    kotlin("jvm") version "1.3.61"
}

dependencies {
    api("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.61")
   // api("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.3.3") - uncomment this line to expose api. You will see kotlinx-coroutines-reactor members in intellisence
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.3.3") // mark, that this dependency is needed for compilation. However it will not be exposed to other project.
    runtimeOnly("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.3.3") // mark, that this dependency is required for runtime. It means, that it will be exposed as runtime dependency only
}

库B\src\Something.kt
import kotlinx.coroutines.runBlocking

fun doSomething() {
    runBlocking { // use kotlinx-coroutines here.
        println("Yess !!!")
    }
}

应用程序\src\Application.kt
package gradle.multi.application

import doSomething

fun main() {
    "12314".toInt() // check, that api layer is exposed here

    /* runBlocking { // this couldn't be compiled, because kotlinx-coroutines aren't exposed here
    }*/

    doSomething()
}

所以,我们做了什么:
  • 我们的依赖:application --> libraryA --> libraryB
  • application是可运行的项目,所有其他的只是库。
  • libraryA没有任何话可以重新暴露依赖。它只是引用 libraryB
  • application只是引用 libraryA .开发人员不知道任何隐式依赖项(编译或运行时)。
  • libraryB使用 kotlinx-coroutines-reactor并将其公开为运行时依赖项。该信息通过libraryA发布到可运行项目。
  • 关于依赖 jar 时未导入 Java-Library 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59685313/

    相关文章:

    java - 您可以从 Solr 索引中的文档中删除一个字段吗?

    android - Gradle + Android,想要用buildFlavor中的自定义文件夹覆盖/assets

    maven - 与原始 Artifact 相同的POM父 Artifact ID是

    Android无法导入两个库

    java - 简单的 Android 应用程序没有响应

    java - LinearLayout 中的 RecyclerView 布局未刷新

    java - Java 中 finally block 后的返回语句如何工作?

    c# - Unity gradle 构建失败

    java - Gradlew 不会在 IDEA 16.2 中添加对我的外部库(类路径)的依赖项

    java - 从 String 反序列化 EnumS 的优雅方式