我对 JVM 世界还很陌生,我发现 Maven 和 Gradle 是处理构建和依赖关系的非常出色的工具。
我需要在我的解决方案中合并两个 jar 文件。它们不托管在任何 Maven 存储库中。我必须使用 libs 文件夹并在开发人员之间或存储库中共享文件吗?
jar 文件不受我控制,我不想费力地在 Maven Central 或类似的东西上发布一些东西。我相信 jar 文件的 url 是相当持久的。
第一个解决方案
不要离开 Gradle。相反,尝试使用文件集合。它应该有效!但不适合我,请选择第二个解决方案
dependencies {
def webHostedJarFiles = ["http://url.to.jar", "http://url.to.second.jar"]
.collect{fileName->new File(fileName)}
compile([
files{webHostedJarFiles},
'commons-validator:commons-validator:1.4.1'
/* and all the other Maven dependencies...*/])
}
将 URL 直接放入 files 方法中会出现 Cannot conversion URL "http://url.to.jar" to a file 异常
由于某种原因,这对我不起作用。依赖项已下载并显示在 IntelliJ 的 gradle 插件中,但编译时编译器似乎无法找到该依赖项。
第二种解决方案
不要离开 Gradle。而是将文件下载到 libs 文件夹中。
复制任务:
task downloadJarsToLibs(){
def f = new File('libs/myFile.jar')
if (!f.exists()) {
new URL('http://path.to/myFile.jar').withInputStream{ i -> f.withOutputStream{ it << i }}
}
}
依赖关系:
dependencies {
compile([
fileTree(dir: 'libs', include: ['*.jar']),
'commons-validator:commons-validator:1.4.1'
/* and all the other Maven dependencies...*/])
}
第三种解决方案(@RaGe的Cortesey)
示例文件:
http://exampe.com/uda/virtuoso/7.2/rdfproviders/jena/210/virt_jena2.jar
http://exampe.com/uda/virtuoso/7.2/jdbc/virtjdbc4.jar
构建.gradle:
repositories {
ivy {
url 'http://example.com/'
layout 'pattern', {
artifact '/uda/[organisation]/7.2/[module]/[revision].[ext]'
}
// This is required in Gradle 6.0+ as metadata file (ivy.xml)
// is mandatory. Docs linked below this code section
metadataSources { artifact() }
}
mavenCentral()
}
dependencies {
compile 'virtuoso:rdfproviders/jena210:virt_jena2:jar', 'virtuoso:jdbc:virtjdbc4:jar'
}
所需元数据的参考此处
不幸的是,这似乎不适用于我的设置,但 Gradle 很高兴,并且在需要时下载文件(因为它们被缓存)