Gradle在ClassPath中生成不可移植的绝对路径

时间:2016-06-13 10:40:39

标签: java eclipse gradle

我不是Java专家,但我正在尝试使用CMU Sphinx 4库构建一个转录播客的组件。我正在使用Eclipse Mars和Gradle将项目构建为可运行的胖罐。我的build.gradle看起来像这样。

/*
 * This build file was auto generated by running the Gradle 'init' task
 * by 'peter' at '03/06/16 18:34' with Gradle 2.6
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/2.6/userguide/tutorial_java_projects.html
 */

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



// 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()
    mavenLocal()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

// 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.12'
    compile group: 'edu.cmu.sphinx', name: 'sphinx4-core', version:'5prealpha-SNAPSHOT'
    compile group: 'edu.cmu.sphinx', name: 'sphinx4-data', version:'5prealpha-SNAPSHOT'

    // 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'
}

jar {

   manifest {
        attributes (
        'Class-Path': configurations.compile.collect { it.isDirectory() ? it : zipTree(it) },
        'Main-Class': 'org.conlang.sources.transcriber.Transcriber')
    }
    from configurations.compile.collect { entry -> zipTree(entry) }
}

这是它产生的清单。

Manifest-Version: 1.0
Class-Path: [ZIP '/home/peter/.gradle/caches/modules-2/files-2.1/org.s
 lf4j/slf4j-api/1.7.12/8e20852d05222dc286bf1c71d78d0531e177c317/slf4j-
 api-1.7.12.jar', ZIP '/home/peter/.gradle/caches/modules-2/files-2.1/
 edu.cmu.sphinx/sphinx4-core/5prealpha-SNAPSHOT/523f86d4932fc124a6d497
 6adbcbc4976f1ece28/sphinx4-core-5prealpha-SNAPSHOT.jar', ZIP '/home/p
 eter/.gradle/caches/modules-2/files-2.1/edu.cmu.sphinx/sphinx4-data/5
 prealpha-SNAPSHOT/545a2d84cfe804d18cd9926a35331546fe09f2c2/sphinx4-da
 ta-5prealpha-SNAPSHOT.jar', ZIP '/home/peter/.gradle/caches/modules-2
 /files-2.1/org.apache.commons/commons-math3/3.2/ec2544ab27e110d2d431b
 dad7d538ed509b21e62/commons-math3-3.2.jar']
Main-Class: org.conlang.sources.transcriber.Transcriber

当我复制并粘贴到我自己的机器上的另一个目录时,jar将无法运行,因为Java无法在清单中找到路径,并且它肯定不会在另一台机器上运行。我需要在build.gradle中更改什么才能获得可用的ClassPath?

2 个答案:

答案 0 :(得分:0)

您如何分发依赖项以保持您的包可移植?

it.isDirectory() ? it : zipTree(it)将生成依赖项jar的绝对路径。如果您正在尝试创建包含依赖项的可分发包,那么jar插件可能不是最好的方法,请尝试使用application plugin

答案 1 :(得分:0)

您只需使用

即可
jar {
   manifest {
        attributes (
            'Main-Class': 'org.conlang.sources.transcriber.Transcriber')
    }
    from configurations.compile.collect { entry -> zipTree(entry) }
}

如果使用configurations.compile.collect解包所有依赖项,则无需指定类路径。这是Using Gradle to build a jar with dependencies

的副本
相关问题