我尝试将本地.jar文件依赖项添加到构建中。gradle文件:

apply plugin: 'java'

sourceSets {
    main {
        java {
            srcDir 'src/model'
        }
    }
}

dependencies {
    runtime files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
    runtime fileTree(dir: 'libs', include: '*.jar')
} 

您可以看到,我将.jar文件添加到这里的referencedLibraries文件夹:https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1/referencedLibraries

但问题是,当我在命令行上运行命令:gradle build时,我得到以下错误:

error: package com.google.gson does not exist
import com.google.gson.Gson;

这是我的全部回购:https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1


当前回答

你可以添加jar做:

对于gradle,只需在build.gradle中放入以下代码:

dependencies {
...
compile fileTree(dir: 'lib', includes: ['suitetalk-*0.jar'])
...
}

对于maven,只需遵循以下步骤:

Intellij: 文件->项目结构->模块->依赖选项卡->点击+号-> jar和依赖->选择jar你想导入->ok ->应用(如果可见)->ok

记住,如果你在运行时得到任何java.lang.NoClassDefFoundError: Could not initialize class exception,这意味着jar中的依赖项没有安装,你必须在父项目中添加所有依赖项。

其他回答

最好的方法是在构建中添加这个功能。Gradle文件,然后点击同步选项

dependency{
    compile files('path.jar')
}

如果您正在使用持续集成,请小心,您必须将库添加到构建服务器上的相同路径中。

出于这个原因,我宁愿将jar添加到本地存储库,当然,在构建服务器上也做同样的事情。

以下是我的工作:

compile fileTree(dir: 'libs', include: '*.jar')

参考Gradle文档。

我无法在https://stackoverflow.com/a/20956456/1019307上得到上面的建议。不过这对我来说很管用。对于我存储在项目根目录libs/目录中的secondstring-20030401.jar文件:

repositories {
    mavenCentral()
    // Not everything is available in a Maven/Gradle repository.  Use a local 'libs/' directory for these.
    flatDir {
       dirs 'libs'
   }
}

...

compile name: 'secondstring-20030401'

您可以尝试为Gradle重用您的本地Maven存储库:

Install the jar into your local Maven repository: mvn install:install-file -Dfile=utility.jar -DgroupId=com.company -DartifactId=utility -Dversion=0.0.1 -Dpackaging=jar Check that you have the jar installed into your ~/.m2/ local Maven repository Enable your local Maven repository in your build.gradle file: repositories { mavenCentral() mavenLocal() } dependencies { implementation ("com.company:utility:0.0.1") } Now you should have the jar enabled for implementation in your project