Source code on github:

001-Create an application with root project

The aim of this part is to create an application inside a project. Like with the maven "pom" project types


*Create directory "demo001" and enter it * Run 'gradle init --type basic' and select the following to setup a simple stub * Script DSL "1: Groovy" * Project name, leave it blank (default)
* Now a new project without sources has been created * Add then inside the build.gradle the following lines

    plugins {
        id 'base'

Remember the Gradle builds use (for our tutorial) groovy as a language. With the previous lines we are loading the plugins that are part of the current project build

Creating a 'main' supbroject

We will add to the main "settings.gradle" the following line. It's like adding child projects to the pom type pom.xml

    include 'startup'

To be fair, only one level of nesting is encouraged, because of some glitchy behaviour of gradle with nested projects (something that in maven you can't see...but think about the problem this carried)

The name of the project IS the name of the directory in which you will put the new build.gradle script

With a simple helper

I created a small script to initialize the directory "mksrc.bat". Supposing our demo001 project is in C:\Documents we will call the following to create a java project with Maven group org.kendar and version 1.0.0-SNAPSHOT

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

The source is

@echo off
set DEST_DIR=%1
set PRJ_TYPE=%2
set PRJ_GROUP=%3

mkdir %DEST_DIR%

echo plugins {>%DEST_DIR%\build.gradle
echo    id '%PRJ_TYPE%'>>%DEST_DIR%\build.gradle
echo }>>%DEST_DIR%\build.gradle
echo group '%PRJ_GROUP%'>>%DEST_DIR%\build.gradle
echo version '%PRJ_VERSION%'>>%DEST_DIR%\build.gradle

mkdir %DEST_DIR%\src\main\java
echo empty>%DEST_DIR%\src\main\java\.gitkeep
mkdir %DEST_DIR%\src\main\resources
echo empty>%DEST_DIR%\src\main\resources\.gitkeep

mkdir %DEST_DIR%\src\test\java
echo empty>%DEST_DIR%\src\test\java\.gitkeep
mkdir %DEST_DIR%\src\test\resources
echo empty>%DEST_DIR%\src\test\resources\.gitkeep

By hand

Create a directory under demo001, let's call it "startup" Add a new fle under it called "build.gradle". This is the equivalent of the maven pom We should then add

And add all directories (src test etc)

The group and version are now accessible through the "group" and "version" variables!


Now running the "gradle projects" command from demo001 directory you will find the demo001 project plus all its referenced projects!

gradle projects

Then you can add a class in demo001\startup\src\main\java\org\kendar\


package org.kendar;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World\n");

And finally run "gradle jar" and you will find the jar inside the directory "demo001\startup\build\libs"

Running the jar

You now want to run your project. Simply add in startaup\build.gradle the plugin 'application' and the class to start. This is how the file should look like.

Tells gradle to use Java, in form of application, and as an application contains a main class, defined in the relative section

    plugins {
       id 'java'
       id 'application'

    group 'org.kendar'
    version '1.0.0-SNAPSHOT'

    application {
        mainClassName = 'org.kendar.HelloWorld'

Now you can start the application with

    gradle run

gradle run

From now on you can find inside demo001\startup\build the directory "scripts" and "lib" that contains a batch to run the jar and the jar itself with its needed libraries

You can look here for further references on the application plugin

Last modified on: February 18, 2020