我从 Gradle 开始,想知道如何将单个依赖项(在我的例子中为 TeamSpeak API)包含到我的 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: '+'
}
感谢您的帮助 :)
最简单的方法是从要包含的依赖项的单独配置开始。我知道您只询问了一个 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) }
}
Using provided
因为配置的名称也很重要,因为当发布 jar 时,您在provided
配置将显示为provided
在与 JAR 一起发布的 POM.xml 中。 Maven 依赖解析器不会下拉provided
jar 的依赖项和用户不会在类路径上出现类的重复副本。看Maven 依赖范围 https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)