Source code on github:

004-Using "external" projects

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

Initialize the repos on the library

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) {
    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!!!

Use the library inside demo004

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:


Everything is working fine!

Add the dependency on the main project

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!

Simplify the publishing task names

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!

gradle init

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!!

Last modified on: February 18, 2020