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.
#2 Go to
#3 Run
#5 Run
Here we specified the name of the project and subprojects.
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
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 deletesettings.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 aJAR
after assembling
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
:Build and Run
Now we can build our project. Let's go to root folder and execute:
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
Make sure, that the order of build is
You should see:
And now we are able to run our app:
./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
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).
.gitignore
:
backend/build/
- same for frontendbuild
- 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
Comments
Post a Comment