Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell Gradle to include a local library jar that in turn has dependencies?

I'm surely missing something as I read the Gradle documentation, since this feels like a common use case. Basically, I have two projects - MyApp and MyLib. MyApp will make use of MyLib.jar as a dependency. MyLib, in turn, has its own dependency jars - when built, MyLib's build.gradle specifies these. In the end, MyLib.jar will contain only the non-dependency .class files, so MyApp will somehow need to download MyLib.jar's dependencies at compile-time.

How do I tell MyApp's build.gradle to include MyLib.jar and then download MyLib's dependency jars without specifying those jars in MyApp's build.gradle? I feel like MyApp's build.gradle shouldn't know about transitive dependencies. I understand that normally, build.gradle will look in a remote Maven/Ivy repo and read the corresponding xml to download the appropriate transitive dependencies, but I don't want to upload MyLib to a Maven/Ivy repository. Ideally, I'd like to distribute MyLib.jar alongside some descriptor file - e.g. pom.xml - that lists the transitive dependencies, but not necessarily packaged as a "repository". See the diagram below to see what I'm referring to:

Library jar dependencies

Note that I plan on distributing MyLib.jar, not as a set of sources, but as a closed jar, so I don't want to set up a multi-project Gradle build. Furthermore, MyLib's dependencies may either be found in MavenCentral/etc repositories or as jars alongside MyLib.jar in the filesystem.

like image 687
Phillip Avatar asked Oct 31 '22 08:10

Phillip


1 Answers

You can publish your library to a local isolated maven repository, and then distribute that repository (for example, as an archive), which can then be used by gradle or maven.

Note: I'm using gradle 2.11.

To create a repository for your library we'll use gradle's maven-publish plugin. build.gradle for your library will look like that:

apply plugin: 'java'
apply plugin: 'maven-publish'

group = 'com.stackoverflow.phillip' // change it
version = '1.0' // whatever

dependencies {
    // ...
}

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
            // uncomment if you want to change artifact id (the default equals to project directory name)
            //artifactId 'my-lib'
        }
    }

    repositories {
        maven {
            url 'file:local-repo' // path is relative to project root
        }
    }
}

After running gradle publish a directory local-repo will be created. That directory will be an actual maven repository containing only one artifact - your library. Naturally, there will be a pom describing your library's dependencies.

Now you can use that repository inside another project's build.gradle, for example:

repositories {
    mavenCentral()
    maven { url 'file:path/to/local-repo' } // path is relative to project root
}

dependencies {
    compile 'com.stackoverflow.phillip:my-lib:1.0'
}
like image 147
Roman Avatar answered Nov 15 '22 03:11

Roman