Java 将库添加到 gradle 构建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21037879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add library to gradle build
提问by itchmyback
I'm trying to add org.apache.commons.lang3
to my build. I've downloaded the library which is directory containing jar files.
我正在尝试添加org.apache.commons.lang3
到我的构建中。我已经下载了包含 jar 文件的目录库。
My group is using gradle to build the project, and I know just enough to maybe ask the right question. So what I think the build is doing is
我的小组正在使用 gradle 来构建项目,我知道的足以提出正确的问题。所以我认为构建正在做的是
- copying a bunch of .bnds to the build directory
- compiles the java we have in src/main/java (via source sourceSets.main.java.srcDirs?)
- 将一堆 .bnds 复制到构建目录
- 编译我们在 src/main/java 中的 java(通过源 sourceSets.main.java.srcDirs?)
I would like to add the lang3 library, but I'm not sure how to go about doing that. Can I just dump it into src/main/java? Or do I have to tell gradle about it?
我想添加 lang3 库,但我不确定如何去做。我可以将它转储到 src/main/java 中吗?或者我必须告诉 gradle 吗?
This is what I think is relevant from the current build.gradle
这是我认为与当前 build.gradle 相关的内容
ext.releaseDir = "${buildDir}/release/${tpVersion.getProgramName()}"
ext.bundlesDir = "${releaseDir}/nucleus/bin/nucleus_java/bundles/"
dependencies {
compile fileTree(dir: bundlesDir, include: '*.jar')
bnd {
source sourceSets.main.java.srcDirs
include '**/*.bnd'
采纳答案by Spindizzy
You could declare it as a dependency, if it exists in any remote repository. That's the way I would do it.
如果它存在于任何远程存储库中,您可以将其声明为依赖项。我就是这样做的。
But if you want to use the local file, do not put it in src/main. Use an extra folder called lib or similar on the same directory level as src or you build script.
但是如果你想使用本地文件,不要把它放在 src/main 中。在与 src 或您构建脚本的同一目录级别上使用名为 lib 或类似的额外文件夹。
Then you can add the local dependency to the build.gradle as in this sample:
然后,您可以将本地依赖项添加到 build.gradle 中,如下例所示:
repositories {
//central maven repo
mavenCentral()
}
dependencies {
//local file
compile files('libs/toxiclibscore.jar')
//dependencies from a remote repository
compile 'java3d:vecmath:1.3.1', 'commons-lang:commons-lang:2.6'
}
回答by MariuszS
The simplest way is to use maven repository for accessing dependencies.
最简单的方法是使用 maven 存储库来访问依赖项。
You can also access this jar directly from filesystem with file dependencies.
您还可以直接从具有文件依赖项的文件系统访问此 jar 。
dependencies {
compile files('libs/a.jar', 'libs/b.jar')
compile fileTree(dir: 'libs', include: '*.jar')
}