Skip to main content

Kotlin + Spring + Vue: Migration from Maven to Gradle

Let's look how to migrate our current Maven Project to Gradle.

Actually, there is an instruction Moving from Maven to Gradle in under 5 minutes, but hardly the result will be what you really need. I recommend perform this process manually - it takes not much more time.

First of all, you need to install Gradle.

Basic algorithm for subprojects - backend and frontend

#1 Remove all maven files - pom.xml, .mvn, etc.
#2 Go to backend subproject using command line.
#3 Run gradle init and answer the following questions:
  • Select type of project to generate: basic
  • Select implementation language: Kotlin
  • Select build script DSL: let it be also Kotlin
#4 Remove settings.gradle.kts - this file is needed only for root project.
#5 Run gradle wrapper

Basic algorithm for root project

The algorithm for root project is the same except one thing: don't delete settings.gradle.kts.

Build Configuration

Backend

Let's fill build.gradle.kts in the backend project with the following code:
  • Specify all required Kotlin and Spring plugins
  • Please, don't forget about org.jetbrains.kotlin.plugin.jpa plugin to use database connection successfully
  • Specify all required dependencies
  • Add runtimeOnly(project(":frontend")) dependency - we need to build frontend first

Frontend

Let's fill build.gradle.kts in the frontend project with the following code:
  • Let's use org.siouan.frontend plugin for frontend building
  • Specify Node.js version, clean script, build script and assemble script - in our case these are commands specified in package.json
  • Now we pack our frontеnd subproject to JAR file and use it as a dependency, so we have to specify task which copies files from build directory to /public and creates a JAR after assembling 
Modify vue.config.js: specify the new build directory outputDir: 'build/dist'.

Modify package.json: specify the build script: "build": "vue-cli-service build" or make sure that it's already specified.

Root Project

We don't need to add anything to build.gradle.kts in our root project, but we need to add the following code to settings.gradle.kts:
Here we specified the name of the project and subprojects.

Build and Run

Now we can build our project. Let's go to root folder and execute:

./gradlew build


NOTE: if use have placeholders in your application.properties such like ${SPRING_DATASOURCE_URL} but don't have the relevant environment variables the building will be failed. Use ./gradlew build -x test to avoid this.


Make sure, that the order of build is frontend than backend. Also you can see at your project structure by

gradle -q projects


You should see:

Root project 'demo'
+--- Project ':backend'
\--- Project ':frontend'



And now we are able to run our app:

./gradlew bootRun


.gitignore

Let's look at the files and folders we should add to .gitignore:
  • backend/build/ - same for frontend
  • build - same for root projects
  • .gradle

NOTE: don't add gradle folder and gradlew files to .gitignore - they contains nothing dangerous and required for successful build at the remote server (Heroku, for example).


Ways to improve

  • Configure a CI/CD pipeline for automated build and deploy

Links

Comments

Popular posts from this blog

Make Authentication More Secure With Cookies Email Registration Confirmation That's all for this year. See you in 2020!

Kotlin + Spring Boot + Vue.js: + Gradle

Hello, dear visitors! I added two new articles to the Fullstack section: Kotlin + Spring + Vue: Migration from Maven to Gradle Kotlin + Spring + Vue: Deploy to Heroku with Gradle I'll be glad if you will find then useful for yourself.

Kotlin + Spring Boot + Vue.js

Hello there! I've published my first set of articles about developing web applications using Kotlin , Spring Boot and Vue.js : Koltin + Spring Boot + Vue.js Here are the contents of this set: Introduction Start CI/CD REST API Database Connection Authentication Spam Protection (reCAPTCHA) Email