How to add local .jar file dependency to build.gradle file?

前端 未结 17 2066
攒了一身酷
攒了一身酷 2020-11-21 23:23

So I have tried to add my local .jar file dependency to my build.gradle file:

apply plugin: \'java\'

sourceSets {
    main {
        java {
            srcD         


        
相关标签:
17条回答
  • 2020-11-21 23:29

    The accepted answer is good, however, I would have needed various library configurations within my multi-project Gradle build to use the same 3rd-party Java library.

    Adding '$rootProject.projectDir' to the 'dir' path element within my 'allprojects' closure meant each sub-project referenced the same 'libs' directory, and not a version local to that sub-project:

    //gradle.build snippet
    allprojects {
        ...
    
        repositories {
            //All sub-projects will now refer to the same 'libs' directory
            flatDir {
                dirs "$rootProject.projectDir/libs"
            }
            mavenCentral()
        }
    
        ...
    }
    

    EDIT by Quizzie: changed "${rootProject.projectDir}" to "$rootProject.projectDir" (works in the newest Gradle version).

    0 讨论(0)
  • 2020-11-21 23:30

    Be careful if you are using continuous integration, you must add your libraries in the same path on your build server.

    For this reason, I'd rather add jar to the local repository and, of course, do the same on the build server.

    0 讨论(0)
  • 2020-11-21 23:31

    An other way:

    Add library in the tree view. Right click on this one. Select menu "Add As Library". A dialog appear, let you select module. OK and it's done.

    0 讨论(0)
  • 2020-11-21 23:33

    You can try reusing your local Maven repository for Gradle:

    • 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
    0 讨论(0)
  • 2020-11-21 23:35

    The following works for me:

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

    Refer to the Gradle Documentation.

    0 讨论(0)
  • 2020-11-21 23:35

    The solution which worked for me is the usage of fileTree in build.gradle file. Keep the .jar which need to add as dependency in libs folder. The give the below code in dependenices block in build.gradle:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
    }
    
    0 讨论(0)
提交回复
热议问题