In the previous article, I show you how to add spring boot projects to a Nx workspace using Gradle. Today I will show the same thing using maven.
I will use my plugin
@jnxplus/nx-boot-maven to make this integration possible.
@jnxplus/nx-boot-maven requires a Java 8 or higher Runtime Environment and the current Long Term Support (LTS) version of node.js.
We start by creating a workspace :
Open a terminal and run this command to create a new workspace :
When asked provide my-product as name and choose an empty workspace :
devs> npx create-nx-workspace@latest npx: installed 48 in 3.278s √ Workspace name (e.g., org name) · my-product √ What to create in the new workspace · empty √ Use Nx Cloud? (It's free and doesn't require registration.) · No > NX Nx is creating your workspace.
In the same terminal go inside my-product folder :
@jnxplus/nx-boot-maven with your package manager using the dev flag.
This is an exampel with npm:
npm install --save-dev @jnxplus/nx-boot-maven
The following command adds Spring boot and Maven support (Maven wrapper and config files) to the workspace. This only needs to be performed once per workspace.
nx generate @jnxplus/nx-boot-maven:init
Choose the version of Java supported by your operating system and values for the information asked for the Maven parent project :
my-product> nx generate @jnxplus/nx-boot-maven:init √ Which version of Java would you like to use? · 11 √ What groupId would you like to use? · com.example √ What parentProjectName would you like to use? · boot-multi-module √ What project version would you like to use? · 0.0.1-SNAPSHOT CREATE mvnw CREATE mvnw.cmd CREATE pom.xml CREATE .mvn/wrapper/maven-wrapper.jar CREATE .mvn/wrapper/maven-wrapper.properties CREATE .mvn/wrapper/MavenWrapperDownloader.java CREATE tools/linters/checkstyle.xml CREATE tools/linters/pmd.xml UPDATE nx.json UPDATE .gitignore UPDATE .prettierignore
As you see, the command added the following files :
- Maven wrapper and Maven executables for windows and Linux.
- Pom.xml for maven parent project. Here we will add our apps and libs later so Maven could perform its tasks.
pmd.xmlfor java linting.
We also updated
nx.json file to add the plugin for dep-graph feature and
.prettierignore so we can ignore Maven build and cache folders.
Let's generate some code now :
nx generate @jnxplus/nx-boot-maven:application my-app
When asked, provide answers or choose default :
my-product> nx generate @jnxplus/nx-boot-maven:application my-app √ Which language would you like to use? · java √ What groupId would you like to use? · com.example √ What project version would you like to use? · 0.0.1-SNAPSHOT √ Which packaging would you like to use? · jar √ Which configuration format would you like to use? · .properties UPDATE workspace.json UPDATE nx.json CREATE apps/my-app/pom.xml CREATE apps/my-app/src/main/java/com/example/myapp/HelloController.java CREATE apps/my-app/src/main/java/com/example/myapp/MyAppApplication.java CREATE apps/my-app/src/main/resources/application.properties CREATE apps/my-app/src/test/java/com/example/myapp/HelloControllerTests.java CREATE apps/my-app/src/test/resources/application.properties UPDATE pom.xml
Run this command to generate a library:
nx generate @jnxplus/nx-boot-maven:library my-lib --projects my-app
The projects option, allow to specify a host project for the library.
my-product> nx generate @jnxplus/nx-boot-maven:library my-lib --projects my-app √ Which language would you like to use? · java √ What groupId would you like to use? · com.example √ What project version would you like to use? · 0.0.1-SNAPSHOT UPDATE workspace.json UPDATE nx.json CREATE libs/my-lib/pom.xml CREATE libs/my-lib/src/main/java/com/example/mylib/HelloService.java CREATE libs/my-lib/src/test/java/com/example/mylib/HelloServiceTests.java CREATE libs/my-lib/src/test/java/com/example/mylib/TestConfiguration.java UPDATE pom.xml UPDATE apps/my-app/pom.xml
@jnxplus/nx-boot-maven allow us to build, test, lint, format and serve Spring Boot projects with the same commands as Nx.
@jnxplus/nx-boot-maven construct project dependencies automatically by analyzing Spring Boot projects and add them to the Nx project graph.
To visualize the dependency graph, run the following command :