This content originally appeared on DEV Community and was authored by khalil la
I like to use Nx workspace to manage my projects. I don't put all my projects in one workspace but I use one workspace per multi-module project.
It works nicely with node frameworks like angular, react, nest... But when its come to other languages and frameworks, we need to use custom plugins.
In this article I will use my plugin @jnxplus/nx-boot-gradle to add support of spring boot and Gradle multi-project builds to Nx workspace.
@jnxplus/nx-boot-gradle add to the Spring Boot world, an opinionated way to integrate Spring Boot apps/libs inside Nx workspace using Gradle multi-project builds.
Let's show you how to use it.
We start by creating a workspace :
Open a terminal and run this command to create a new workspace :
npx create-nx-workspace@latest
Wen asked provide my-org as name and choose a empty workspace :
Now and in the same terminal go inside my-org folder :
cd my-org
1. Install the plugin
In the workspace root run this command to install the plugin :
npm install --save-dev @jnxplus/nx-boot-gradle
2. Add Spring boot and Gradle wrapper support
The following command adds Spring boot and Gradle support (Gradle wrapper and config files) to the workspace. This only needs to be performed once per workspace.
nx generate @jnxplus/nx-boot-gradle:init
As you see, the command add the following files :
- Gradle wrapper and Gradle executables for windows and Linux : Using Gradle Wrapper we can distribute/share a project to everybody to use the same version and Gradle's functionality(compile, build, install...) even if it has not been installed
-
gradle.properties: This file contain Spring Boot and dependency management versions that we will use for all apps and libs inside Nx worspace. -
settings.gradle: Here we will add our apps and libs later so Gradle will be able to perform its tasks.
3. Usage
Generate an application
nx generate @jnxplus/nx-boot-gradle:application my-app
When asked, provide answers or choose default :
Check the settings.gradle, here we added a entry for my-app :
rootProject.name = 'boot-multiproject'
include('apps:my-app')
Build the app
nx build my-app
Serve the app
nx serve my-app
open http://localhost:8080 to see app working:
Test the app
To test the app run this command :
nx test my-app
What I like to do to see if test command is really working is breaking the tests, and run it again :
So change the test and add a comma between Hello and World :
@Test
public void shouldReturnHelloWorld() throws Exception {
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello, World")));
}
Perfect, now test target is failing :
Revert change and let's generate this time a library :
To generate a library use this command :
nx generate @jnxplus/nx-boot-gradle:library my-lib
When asked, provide answers or choose default :
Check the settings.gradle, here we added a entry for my-lib :
rootProject.name = 'boot-multiproject'
include('libs:my-lib')
include('apps:my-app')
Like an app, we can build it and test it :
Build :
nx build my-lib
Test :
nx test my-lib
But we can't run it with serve target :
PS C:\Users\khali\devs\my-org> nx serve my-lib
> NX ERROR Cannot find target 'serve' for project 'my-lib'
PS C:\Users\khali\devs\my-org>
Hope you like this article, and give this plugin a try :)
You can find the Github code here :
https://github.com/khalilou88/my-org
This content originally appeared on DEV Community and was authored by khalil la
khalil la | Sciencx (2021-09-12T12:01:09+00:00) How to add Spring Boot and Gradle multi-project builds capabilities to your Nx workspace?. Retrieved from https://www.scien.cx/2021/09/12/how-to-add-spring-boot-and-gradle-multi-project-builds-capabilities-to-your-nx-workspace/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.