![](/img/trans.png)
[英]How do you include a local jar when using mvn assembly:single to build a jar-with-dependencies?
[英]How do you build a .jar that contains external .jar dependencies?
我正在使用Microsoft JDBC Driver 4.1连接到SQL Server。 在Eclipse中,我将.jar文件添加到项目的类路径中(C:\\ Program Files \\ Microsoft JDBC Driver 4.1 for SQL Server \\ sqljdbc_4.1 \\ enu \\ sqljdbc41.jar)。
我已经将这个.jar添加到了我的项目中名为lib的文件夹中,并尝试将此.jar作为我的.jar的一部分添加:
repositories {
mavenCentral()
}
dependencies {
compile files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
但是,sqljdbc41.jar不包含在我的.jar中。 我错过了什么吗?
您必须创建(应用程序)分发以将库和应用程序jar打包到一个文件夹中: http : //gradle.org/docs/current/userguide/application_plugin.html
或者,您可以使用像https://github.com/johnrengelman/shadow这样的插件来创建一个独立的uberjar。
谢谢chromanoid。 我能够通过两种方式解决问题。 首先,我将sqljdbc41.jar放在项目中名为lib的新文件夹中。 解决方案1是创建应用程序分发:
apply plugin:'application'
apply plugin: 'java'
mainClassName = "main.java.application.Main"
repositories {
mavenCentral()
}
dependencies {
runtime files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
然后,您可以使用distZip任务创建分发。 解决方案2是创造一个胖罐。
apply plugin: 'java'
repositories {
mavenCentral()
}
configurations {
provided
compile.extendsFrom provided
}
dependencies {
runtime files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
jar {
dependsOn configurations.runtime
from {
(configurations.runtime - configurations.provided).collect {
it.isDirectory() ? it : zipTree(it)
}
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
}
并使用构建任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.