Index

Source code on github: https://github.com/kendarorg/Gradle101

002-Adding internal dependencies

We add now a dependency. We will create a new application called demo002 identical to the demo001.

It is possible to directly copy the content of demo001 into the new demo002 directory and rename the project inside the demo002\settings.gradle to demo002

Then we create a new "services" subproject excatly as we previously did with the startup project calling (with the demo002 project inside the "C:\Documents" directory) and adding the services project to the main settins.gradle

    mksrc C:\Documents\demo001\services java org.kendar 1.0.0-SNAPSHOT

And adding the plugin 'java-library' to the services build.gradle to define that as...a library, resulting in the following:

plugins {
   id 'java'
   id 'java-library'
}
group 'org.kendar'
version '1.0.0-SNAPSHOT'

Now running the "gradle projects" a new project will appear

A new service

We then create a new service to extract the "Hello World" printing responsability adding demo002\services\src\main\java\org\kendar\HelloWorldService.java

Containing

package org.kendar;

public class HelloWorldService {
    public void printHello() {
        System.out.println("Hello World Service\n");
    }
}

Adding the dependency

We can now add the dependency from "services" into the "startup\build.gradle" adding the following at the end of the file. We use the keyword "implementation". This means that the dependency is considered always.

We have these different ways (with their Maven equivalent)

If a new dependency is needed, part of the main project. This can be added with the following syntax:

    dependencies {
        implementation project(":services")
    }

Eventually a couple of other syntaxes are allowed here if the dependency is on an artifact

All those ARE string, and can be replaced by variables (remember this when the refactor will start)

And change the HelloWorld class implementation to

package org.kendar;

public class HelloWorld {
    public static void main(String[] args) {
        HelloWorldService service = new HelloWorldService();
        service.printHello();
    }
}

Then running "gradle build run" we should see the "Hello World Service" message

gradle run build


Last modified on: February 18, 2020