2015-08-21 190 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

相關問題