有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在带有Gradle的JAR中包含单个依赖项?

我从Gradle开始,我想知道如何将一个依赖项(在我的例子中是TeamSpeak API)包含到我的JAR中,以便它在运行时可用

这是我身体的一部分。格拉德尔:

apply plugin: 'java'

compileJava {
    sourceCompatibility = '1.8'
    options.encoding = 'UTF-8'
}

jar {
    manifest {
        attributes 'Class-Path': '.......'
    }

    from {
        * What should I put here ? *
    }
}

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '4.3.7.Final'
    compile group: 'org.spigotmc', name: 'spigot', version: '1.8-R0.1-RELEASE'
    // Many other dependencies, all available at runtime...

    // This one isn't. So I need to include it into my JAR :
    compile group: 'com.github.theholywaffle', name: 'teamspeak3-api', version: '+'

}

谢谢你的帮助:)


共 (1) 个答案

  1. # 1 楼答案

    最简单的方法是从要包含的依赖项的单独配置开始。我知道您只询问了一个jar,但是如果您在新配置中添加更多依赖项,这个解决方案就会起作用。Maven对这类东西有一个众所周知的名字叫做provided,所以我们将使用它

       configurations {
          provided
          // Make compile extend from our provided configuration so that things added to bundled end up on the compile classpath
          compile.extendsFrom(provided)
       }
    
       dependencies {
          provided group: 'org.spigotmc', name: 'spigot', version: '1.8-R0.1-RELEASE'
       }
    
       jar {
           // Include all of the jars from the bundled configuration in our jar
           from configurations.provided.asFileTree.files.collect { zipTree(it) }
       }
    

    使用provided作为配置的名称也很重要,因为当发布jar时,provided配置中的任何依赖项都将在POM中显示为provided。与JAR一起发布的xml。Maven依赖项解析程序不会下拉provided依赖项,jar的用户不会在类路径上得到类的重复副本。见Maven Dependency Scopes