2015-08-21 188 views
2

我开始使用Gradle,我想知道如何在我的JAR中包含单个依赖项(TeamSpeak API),以便它可以在运行时使用。如何在包含Gradle的JAR中包含单个依赖项?

这里是我的build.gradle的一部分:

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: '+' 

} 

感谢您的帮助:)

+0

依存关系不存储在jar文件中。它们不在jar文件中,Class-Path清单条目包含该jar的相对路径。 –

回答

3

最简单的方法是开始与您要包括依赖单独的配置。我知道你只问过一个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作为配置的名称也很重要,因为当瓶子被发布后,您在provided配置有任何依赖关系将显示为在获取与JAR公布的pom.xml provided。 Maven依赖关系解析器不会拉低provided依赖关系,并且您的jar的用户不会以类路径上类的重复副本结束。请参见Maven Dependency Scopes

相关问题