简体   繁体   中英

Including local jar dependency with corresponding source jar in gradle

How can I include a local jar dependency and its corresponding sources jar (for my IDE, Intellij) in Gradle?

I have tried adding a flatDir to lib (a directory in the same parent directory as all the Gradle stuff) under repositories , where lib contains mylib-1.0.jar and mylib-1.0-sources.jar . I then put implementation name: "mylib-1.0" under dependencies . The jar with compiled classes was included, but not the sources.

I also tried creating a local maven repository. In this case, lib contained

lib/xxx/yyy/mylib/1.0/mylib-1.0.jar

and

lib/xxx/yyy/mylib/1.0/mylib-1.0-sources.jar

where xxx.yyy is the group ID. I added

maven {
    url uri("lib")
}

under repositories and

implementation group: "xxx.yyy", name: "mylib", version: "1.0"

under dependencies . Still did not work--neither jars were included this time.

I also tried adding a minimal POM in the same directory as both the jars, but that did not change anything.

Any idea as to where I could be going wrong?

Note: I am no expert at using Gradle or Maven.

Edit: Rationale: in case somebody suggests it, I am aware I can just include as a dependency the jar with the compiled class and "link" the sources jar to it in Intellij, but then every time I refresh gradle I have to re-link them.

I provide below the following approaches.

Prior to gradle 5

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
}

Here libs is the directory which contains the list of jar files and it should be location inside your project base directory.

You can also use in the following manner if you have only one jar file. Here libs refer to the directory which contains only one jar file and libs is available in the project base directory.

dependencies {
    compile files('libs/your jar file name.jar')
}

If you want to specify a list of jar files, you can use in the following manner.

dependencies {
compile files(‘libs/a.jar’,
‘libs/b.jar’,
‘libs/c.jar’
)
}

In Gradle 5 You have to use in the follwong manner.

dependencies {
    externalLibs files('libs/a.jar', 'libs/b.jar')
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM