Gradle 具有依赖关系的多项目设置 -> 无法解析所有依赖关系

Gradle Multi-Project Setup with Dependencies -> Could not resolve all dependencies

我有一个带有 gradle 的小型多项目设置(也是新的),但它无法编译。 我可以在 IDE 中 运行 它,但是当我想编译它时,它失败了。 然后,当我尝试在 Game 项目中使用来自 Engine 项目的依赖项之一时,它甚至在 IDE 内部失败。 更多详情请往下看。

:Engine:compileJava UP-TO-DATE
:Engine:processResources UP-TO-DATE
:Engine:classes UP-TO-DATE
:Engine:jar
:compileJava

FAILURE: Build failed with an exception.

* What went wrong:
Could not resolve all dependencies for configuration ':compileClasspath'.
> Could not find org.lwjgl:lwjgl:3.0.1-SNAPSHOT.
  Searched in the following locations:
      https://jcenter.bintray.com/org/lwjgl/lwjgl/3.0.1-SNAPSHOT/maven-metadata.xml
      https://jcenter.bintray.com/org/lwjgl/lwjgl/3.0.1-SNAPSHOT/lwjgl-3.0.1-SNAPSHOT.pom
      https://jcenter.bintray.com/org/lwjgl/lwjgl/3.0.1-SNAPSHOT/lwjgl-3.0.1-SNAPSHOT.jar
  Required by:
      project : > project :Engine
> Could not find org.lwjgl:lwjgl-egl:3.0.1-SNAPSHOT.
  Searched in the following locations:
      https://jcenter.bintray.com/org/lwjgl/lwjgl-egl/3.0.1-SNAPSHOT/maven-metadata.xml
      https://jcenter.bintray.com/org/lwjgl/lwjgl-egl/3.0.1-SNAPSHOT/lwjgl-egl-3.0.1-SNAPSHOT.pom
      https://jcenter.bintray.com/org/lwjgl/lwjgl-egl/3.0.1-SNAPSHOT/lwjgl-egl-3.0.1-SNAPSHOT.jar
  Required by:
      project : > project :Engine
> Could not find org.lwjgl:lwjgl-glfw:3.0.1-SNAPSHOT.
  Searched in the following locations:
      https://jcenter.bintray.com/org/lwjgl/lwjgl-glfw/3.0.1-SNAPSHOT/maven-metadata.xml
      https://jcenter.bintray.com/org/lwjgl/lwjgl-glfw/3.0.1-SNAPSHOT/lwjgl-glfw-3.0.1-SNAPSHOT.pom
      https://jcenter.bintray.com/org/lwjgl/lwjgl-glfw/3.0.1-SNAPSHOT/lwjgl-glfw-3.0.1-SNAPSHOT.jar
  Required by:
      project : > project :Engine
> Could not find org.lwjgl:lwjgl-jemalloc:3.0.1-SNAPSHOT.
  Searched in the following locations:
      https://jcenter.bintray.com/org/lwjgl/lwjgl-jemalloc/3.0.1-SNAPSHOT/maven-metadata.xml
      https://jcenter.bintray.com/org/lwjgl/lwjgl-jemalloc/3.0.1-SNAPSHOT/lwjgl-jemalloc-3.0.1-SNAPSHOT.pom
      https://jcenter.bintray.com/org/lwjgl/lwjgl-jemalloc/3.0.1-SNAPSHOT/lwjgl-jemalloc-3.0.1-SNAPSHOT.jar
  Required by:
      project : > project :Engine
> Could not find org.lwjgl:lwjgl-openal:3.0.1-SNAPSHOT.
  Searched in the following locations:
      https://jcenter.bintray.com/org/lwjgl/lwjgl-openal/3.0.1-SNAPSHOT/maven-metadata.xml
      https://jcenter.bintray.com/org/lwjgl/lwjgl-openal/3.0.1-SNAPSHOT/lwjgl-openal-3.0.1-SNAPSHOT.pom
      https://jcenter.bintray.com/org/lwjgl/lwjgl-openal/3.0.1-SNAPSHOT/lwjgl-openal-3.0.1-SNAPSHOT.jar
  Required by:
      project : > project :Engine
> Could not find org.lwjgl:lwjgl-opencl:3.0.1-SNAPSHOT.
  Searched in the following locations:
      https://jcenter.bintray.com/org/lwjgl/lwjgl-opencl/3.0.1-SNAPSHOT/maven-metadata.xml
      https://jcenter.bintray.com/org/lwjgl/lwjgl-opencl/3.0.1-SNAPSHOT/lwjgl-opencl-3.0.1-SNAPSHOT.pom
      https://jcenter.bintray.com/org/lwjgl/lwjgl-opencl/3.0.1-SNAPSHOT/lwjgl-opencl-3.0.1-SNAPSHOT.jar
  Required by:
      project : > project :Engine
> Could not find org.lwjgl:lwjgl-opengl:3.0.1-SNAPSHOT.
  Searched in the following locations:
      https://jcenter.bintray.com/org/lwjgl/lwjgl-opengl/3.0.1-SNAPSHOT/maven-metadata.xml
      https://jcenter.bintray.com/org/lwjgl/lwjgl-opengl/3.0.1-SNAPSHOT/lwjgl-opengl-3.0.1-SNAPSHOT.pom
      https://jcenter.bintray.com/org/lwjgl/lwjgl-opengl/3.0.1-SNAPSHOT/lwjgl-opengl-3.0.1-SNAPSHOT.jar
  Required by:
      project : > project :Engine
> Could not find org.lwjgl:lwjgl-opengles:3.0.1-SNAPSHOT.
  Searched in the following locations:
      https://jcenter.bintray.com/org/lwjgl/lwjgl-opengles/3.0.1-SNAPSHOT/maven-metadata.xml
      https://jcenter.bintray.com/org/lwjgl/lwjgl-opengles/3.0.1-SNAPSHOT/lwjgl-opengles-3.0.1-SNAPSHOT.pom
      https://jcenter.bintray.com/org/lwjgl/lwjgl-opengles/3.0.1-SNAPSHOT/lwjgl-opengles-3.0.1-SNAPSHOT.jar
  Required by:
      project : > project :Engine
> Could not find org.lwjgl:lwjgl-stb:3.0.1-SNAPSHOT.
  Searched in the following locations:
      https://jcenter.bintray.com/org/lwjgl/lwjgl-stb/3.0.1-SNAPSHOT/maven-metadata.xml
      https://jcenter.bintray.com/org/lwjgl/lwjgl-stb/3.0.1-SNAPSHOT/lwjgl-stb-3.0.1-SNAPSHOT.pom
      https://jcenter.bintray.com/org/lwjgl/lwjgl-stb/3.0.1-SNAPSHOT/lwjgl-stb-3.0.1-SNAPSHOT.jar
  Required by:
      project : > project :Engine
> Could not find org.lwjgl:lwjgl-vulkan:3.0.1-SNAPSHOT.
  Searched in the following locations:
      https://jcenter.bintray.com/org/lwjgl/lwjgl-vulkan/3.0.1-SNAPSHOT/maven-metadata.xml
      https://jcenter.bintray.com/org/lwjgl/lwjgl-vulkan/3.0.1-SNAPSHOT/lwjgl-vulkan-3.0.1-SNAPSHOT.pom
      https://jcenter.bintray.com/org/lwjgl/lwjgl-vulkan/3.0.1-SNAPSHOT/lwjgl-vulkan-3.0.1-SNAPSHOT.jar
  Required by:
      project : > project :Engine

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 9.375 secs

我的文件结构很简单:

Project\Engine

Project\Game

我的游戏项目应该使用 Engine 项目,这样我就可以轻松地更改 Engine 项目中的内容,而无需为其构建新的 .jar 文件,并且 Game 项目只与它一起工作(例如,如果您将其添加为 eclipse 中的依赖项项目)并且在编译 Game 项目时,它应该编译 Engine 项目以获得 Game 项目所需的 jar。

目前它的工作方式是 Game 项目依赖于 Engine 项目,这在我的 IDE 中工作得很好(好吧,至少当 Engine 项目是 importet 时),但是一旦我运行 构建失败,如上所示。 当我想访问 class 时,它在 IDE 中也失败了,它是引擎项目中的依赖项,但不是在完整引擎项目是依赖项的游戏项目中。

游戏项目的build.gradle文件。

// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    //compile 'org.slf4j:slf4j-api:1.7.21'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    //testCompile 'junit:junit:4.12'
    
    compile project(':Engine')
    runtime project(':Engine')
}

Game项目的settings.gradle文件:

include 'Engine'
project (':Engine').projectDir = new File('../Engine') 

rootProject.name = 'Game'

然后我得到了引擎项目的 build.gradle 文件:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'

sourceCompatibility = 1.8
targetCompatibility = 1.8

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}

task wrapper(type: Wrapper) {
    gradleVersion = '3.1'
}

sourceSets {
    main.java.srcDir "src/main/java"
}

project.ext.lwjglVersion = "3.0.1-SNAPSHOT"

import org.gradle.internal.os.OperatingSystem

switch ( OperatingSystem.current() ) {
    case OperatingSystem.WINDOWS:
      project.ext.lwjglNatives = "natives-windows"
      break
    case OperatingSystem.LINUX:
      project.ext.lwjglNatives = "natives-linux"
      break
    case OperatingSystem.MAC_OS:
      project.ext.lwjglNatives = "natives-macos"
      break
}

dependencies {
    // LWJGL dependencies START
    compile "org.lwjgl:lwjgl:${lwjglVersion}"
    compile "org.lwjgl:lwjgl-egl:${lwjglVersion}"
    compile "org.lwjgl:lwjgl-glfw:${lwjglVersion}"
    compile "org.lwjgl:lwjgl-jemalloc:${lwjglVersion}"
    compile "org.lwjgl:lwjgl-openal:${lwjglVersion}"
    compile "org.lwjgl:lwjgl-opencl:${lwjglVersion}"
    compile "org.lwjgl:lwjgl-opengl:${lwjglVersion}"
    compile "org.lwjgl:lwjgl-opengles:${lwjglVersion}"
    compile "org.lwjgl:lwjgl-stb:${lwjglVersion}"
    compile "org.lwjgl:lwjgl-vulkan:${lwjglVersion}"
    // LWJGL natives
    runtime "org.lwjgl:lwjgl:${lwjglVersion}:${lwjglNatives}"
    runtime "org.lwjgl:lwjgl-glfw:${lwjglVersion}:${lwjglNatives}"
    runtime "org.lwjgl:lwjgl-jemalloc:${lwjglVersion}:${lwjglNatives}"
    runtime "org.lwjgl:lwjgl-openal:${lwjglVersion}:${lwjglNatives}"
    runtime "org.lwjgl:lwjgl-stb:${lwjglVersion}:${lwjglNatives}"
    // LWJGL dependencies END
}

那么为什么我会收到此错误以及如何以我可以 运行 的方式解决它并在 Linux、Windows 和 [=47= 上构建项目] 使用 IDEs Eclipse 和 Intellij? 只有在运行ning

时才会出现该错误

gradlew build

在游戏项目中。构建引擎项目工作得很好。当我 运行 例如 eclipse 中的游戏项目时,它也能正常工作,但是当我想构建它或想使用引擎项目在游戏项目中可以访问的 class 时,它失败。

编辑(更好的答案)

我会以不同的方式组织项目。

尝试使用此层次结构:

Project/ 
  |
  +- Game/
  +- Engine/
  +- build.gradle
  +- settings.gradle

在你的settings.gradle

include ':Game'
include ':Engine'

在您的 build.gradle 项目根目录中

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}

repositories 您的子项目的节点不再需要了。

您的游戏项目

settings.gradle只是

rootProject.name = 'Game'