在 Gradle 中使用 SWT 无法解析所有依赖项

Using SWT in Gradle could not resolve all dependencies

我试图在我的 Gradle 项目中使用最新的 Eclipse SWT 3.114.0,但我得到以下信息:

   > Could not find org.eclipse.platform:org.eclipse.swt.${osgi.platform}:3.114.0.
     Searched in the following locations:
       - https://jcenter.bintray.com/org/eclipse/platform/org.eclipse.swt.${osgi.platform}/3.114.0/org.eclipse.swt.${osgi.platform}-3.114.0.pom
       - https://repo.maven.apache.org/maven2/org/eclipse/platform/org.eclipse.swt.${osgi.platform}/3.114.0/org.eclipse.swt.${osgi.platform}-3.114.0.pom
       - file:/C:/Users/voark/IdeaProjects/Eden-Dev/newlauncher/libs/org.eclipse.swt.${osgi.platform}-3.114.0.jar
       - file:/C:/Users/voark/IdeaProjects/Eden-Dev/newlauncher/libs/org.eclipse.swt.${osgi.platform}.jar
       - https://jcenter.bintray.com/org/eclipse/swt/org/eclipse/platform/org.eclipse.swt.${osgi.platform}/3.114.0/org.eclipse.swt.${osgi.platform}-3.114.0.pom
     Required by:
         project : > org.eclipse.platform:org.eclipse.swt:3.114.0

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

这是我的 build.gradle:

import org.gradle.api.JavaVersion

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'

mainClassName = 'com.legacypk.newlauncher.Main'

group 'com.legacypk'
version '1.0.0'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

repositories {
    jcenter()
    mavenCentral()
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile group: 'org.eclipse.platform', name: 'org.eclipse.swt.win32.win32.x86_64', version: '3.114.0'
    compile group: 'org.eclipse.platform', name: 'org.eclipse.swt.cocoa.macosx.x86_64', version: '3.114.0'
    compile group: 'org.eclipse.platform', name: 'org.eclipse.swt.gtk.linux.x86_64', version: '3.114.0'
    compile group: 'org.eclipse.platform', name: 'org.eclipse.swt.gtk.linux.ppc64le', version: '3.114.0'

    compile name: 'DJNativeSwing'
    compile name: 'DJNativeSwing-SWT'
}

我在这里错过了什么?

首先,SWT 现在正式发布在 Maven Central 上,因此您可能应该将 mavenCentral() 添加到您的存储库块中。

其次,maven 属性 osgi.platform 似乎没有被 gradle 处理。相反,您必须执行一些特殊的依赖项解析才能正确获取特定于平台的依赖项。

以下应该可以正常工作(从 https://github.com/LWJGLX/lwjgl3-swt/blob/master/build.gradle 中提取)

configurations.all {
    resolutionStrategy {
        dependencySubstitution {
            def os = System.getProperty("os.name").toLowerCase()
            if (os.contains("windows")) {
                substitute module('org.eclipse.platform:org.eclipse.swt.${osgi.platform}') with module("org.eclipse.platform:org.eclipse.swt.win32.win32.x86_64:3.114.0")
            }
            else if (os.contains("linux")) {
                substitute module('org.eclipse.platform:org.eclipse.swt.${osgi.platform}') with module("org.eclipse.platform:org.eclipse.swt.gtk.linux.x86_64:3.114.0")
            }
            else if (os.contains("mac")) {
                substitute module('org.eclipse.platform:org.eclipse.swt.${osgi.platform}') with module("org.eclipse.platform:org.eclipse.swt.cocoa.macosx.x86_64:3.114.0")
            }
        }
    }
}