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:
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.
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'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With