Source code on github: https://github.com/kendarorg/Gradle101
Just take the demo003 and copy as demo004
Create then a new project demo004lib at the same level of the demo004 with a java-library type
Remove eveything but the plugins and add the maven plugin and the package/version
plugins {
id 'java'
id 'java-library'
id 'maven-publish'
}
group 'org.kendar'
version '1.0.0-SNAPSHOT'
Add the repository definition into build.gradle
ext.repos = [
'LocalMaven' : "file://${projectDir}/../maven"
]
And then the deployment part. Note that not using the "subprojects" we will add this part directly
publishing {
publications {
mavenJava(MavenPublication) {
from project.components.java
}
}
repositories {
maven {
name = 'LocalMaven'
url = uri(repos.LocalMaven)
}
}
}
Remove the test class :)
Now we can "gradle build publish" the jar :) Remember that we added only the local maven dir repo!!!
Inside the service library we will add the dependency (do you remember about the various ways to define dependencies)
dependencies {
implementation group: 'org.kendar', name: 'demo004lib', version: '1.0.0-SNAPSHOT'
}
Now with the gradle build everything will work :)
We can do a "gradle build publish" and we will see the dependency added on the disk repository pom:
...
<modelVersion>4.0.0</modelVersion>
<groupId>org.kendar</groupId>
<artifactId>services</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.kendar</groupId>
<artifactId>demo004lib</artifactId>
<version>1.0.0-SNAPSHOT</version>
...
Everything is working fine!
We then would like to define the version of the dependency in the main project. In the main demo004 build.gradle we will add a new variable. The dependencies, if you remember, are in plain text
ext.dependenciesVersion = [
'demo004lib' : "1.0.0-SNAPSHOT"
]
And inside the "services" build gradle we will now write the following (note the version!)
dependencies {
implementation group: 'org.kendar', name: 'demo004lib',
version: dependenciesVersion.demo004lib
}
We can now build with "gradle build publishMavenJavaPublicationToLocalMavenRepository"
And now we can see from the maven repo that everything have been published!
You have notice how long are the task names generated by gradle. There is a way to reduce all this.
Let's start with the demo004lib, here there is only one publish task In the build.gradle add the following. We are telling gradle that a new task exists depending on the original task "publish"
task publishDir(dependsOn:'publish'){
}
When running "gradle tasks --all" into the demo004lib just modified appears a new task!
And running it will publish the artifact to the local repository
It is now possible to do the same. Running "gradle tasks" the relevant lines are
Inside the demo004 build.gradle we can then add in afterEvaluate the needed tasks. Of course in the section of the subprojects containing the MavenPublishPlugin (maven)
Here we define a new item, the task. It can contain real java code :)
plugins.withType(MavenPublishPlugin) {
...
task publishDir(dependsOn:'publishMavenJavaPublicationToLocalMavenRepository'){}
task publishRemote(dependsOn:'publishMavenJavaPublicationToRemoteMavenRepository'){}
task publishAll(){
dependsOn 'publishDir'
dependsOn 'publishRemote'
}
...
Running "gradle publishAll" will spread our jars!!