首页 > 解决方案 > 导出的可运行 jar 不包含来自 eclipse 2018-12 和 java 11 的依赖 jar。我们如何解决这个问题?

问题描述

我的 byild.gradle 文件

能够使用 JAVA 8 导出 jar。当我配置为 JAVA 11 时,导出的 jar 不包含外部 jar

// 应用 java-library 插件以添加对 Java 库的支持 apply plugin: 'java-library'

// In this section you declare where to find the dependencies of your project
repositories {
    jcenter()
}

configurations 
    { 
        all*.exclude group: 'org.slf4j', module: 'slf4j-log4j12' //by both name and group
    }

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:21.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    compile group: 'com.microsoft', name: 'sqljdbc4', version: '3.0'
    compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
    compile group: 'com.ibatis', name: 'ibatis2-common', version: '2.1.7.597'
    compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
    compile group: 'org.yaml', name: 'snakeyaml', version: '1.21'
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
    compile group: 'org.json', name: 'json', version: '20171018'

    compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.13'
    compile group: 'com.itextpdf.tool', name: 'xmlworker', version: '5.5.13'  
    testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.2'
}

标签: javaeclipsebuild.gradle

解决方案


将此以下行添加到您的 build.gradle

jar { manifest {attributes "Main-Class": "your main class" } from {configurations.compile.collect { it.isDirectory()? it : zipTree(it) } }

然后进行清理/构建,在 \build\libs 文件夹中创建的 jar 是您的可运行 jar


推荐阅读