Gradle 的依赖项没有出现在 jar 中?

Dependencies not showing up in jar with Gradle?

我正在使用这个 build.grade。当我 运行 gradlew build 时,它只生成一个带有源的 jar 文件,而不是 libs 文件夹中的 stone.jar。我应该怎么做?

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

// Source sets in the project, specify source directories
sourceSets {
    main {
        java.srcDir("${projectDir}/src/")
        resources.srcDir("${projectDir}/src/")
    }
}

// Dependencies for the project are stored in the libs directory
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

// Control what goes into the JAR
jar {

    manifest {
        attributes 'Main-Class': 'com.elsea.sublimelauncher.Driver'
    }

    // Include all the classes except the tests
    include("com/elsea/sublimelauncher/**")
}

默认情况下,gradle 中的 jar 任务从您的项目源文件构建一个可执行的 jar 文件。它不会包含您的程序所需的任何传递库。这对 web 服务器有好处,因为它们通常将所有 jar 保存在一个特殊的 lib 文件夹中,但对许多独立程序不利。

您要做的是创建一个 fat jar,它将在一个 jar 文件中包含所有 类 和资源。 如果您使用 Maven,您会看到像 'my-program-v1.0-jar-with-dependcies.jar'

这样的文件

有一个 shadow plugin for gradle 可以做同样的事情。 github 上的 Wiki 包含有关如何在您的项目中使用它的所有信息。