Gradle build for Spring Boot/Ionic application.

In the past, I have not been faced with gradle so far. I mainly used maven as this is still much more widely used in projects. Still, I wanted to get to know Gradle better and therefore I set-up a multi-module project based on Spring Boot and Ionic. In the following I will briefly explain the steps.

The project consists of the parent-project that inlcudes one module for the backend and the other for the frontend. Bascially, it looks as follows:
parent_project
build.gradle
settings.gradle
backend
--> backend.gradle
frontend
--> frontend.gradle

The settings.gradle file in the parent-project basically defines the two modules frontend and backend. Since the gradle files in the two modules do not use the default name (build.gradle), but are named after the module name, the build file names need to be configured.


include 'backend'
include 'frontend'
rootProject.children.each {
it.buildFileName = it.name + '.gradle'
}

view raw

settings.gradle

hosted with ❤ by GitHub

The build.gradle defines configrations which are valid for all projects or subprojects. It looks as follows:


allprojects {
buildscript {
repositories {
mavenCentral()
}
}
repositories {
mavenCentral()
}
}

view raw

build.gradle

hosted with ❤ by GitHub

The frontend.gradle file defines a plugin that allows to execute npm builds. This plugin is needed as the Ionic build is based on npm.


plugins {
id "com.moowork.node" version "1.2.0"
}
//node {
// version = '6.9.4'
// download = true
//}
task clean(dependsOn: 'npm_run_clean') {
}
task build(dependsOn: 'npm_run_build') {
}

view raw

frontend.gradle

hosted with ❤ by GitHub

The backend.gradle defines the jar task that copies the distribution artifacts in the www folder of the frontend module to the public folder in the build resources of the backend module.


plugins {
id 'org.springframework.boot' version '1.5.8.RELEASE'
}
bootRepackage {
mainClass = 'ch.renewinkler.mobile_track.MobileTrackApplication'
}
apply plugin: 'java'
group = 'ch.renewinkler'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
jar {
dependsOn(':frontend:build')
into('public') {
from "${project(':frontend').projectDir}/www"
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

view raw

backend.gradle

hosted with ❤ by GitHub