DEV Community

khalil la
khalil la

Posted on • Updated on

Announcing @jnxplus/nx-boot-maven

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.

0. Prerequisites

@jnxplus/nx-boot-maven requires a Java 8 or higher Runtime Environment and the current Long Term Support (LTS) version of node.js.

1. Create a Nx workspace

We start by creating a workspace :
Open a terminal and run this command to create a new workspace :

npx create-nx-workspace@latest
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

2. Install @jnxplus/nx-boot-maven

In the same terminal go inside my-product folder :

cd my-product 
Enter fullscreen mode Exit fullscreen mode

Now install @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
Enter fullscreen mode Exit fullscreen mode

3. Init workspace with Spring boot and 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
  • checkstyle.xml and pmd.xml for java linting.

We also updated nx.json file to add the plugin for dep-graph feature and .gitignore and .prettierignore so we can ignore Maven build and cache folders.

4. Usage

Let's generate some code now :

4.1 Generate an application

nx generate @jnxplus/nx-boot-maven:application my-app
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

4.2 Generate a library and add it to my-app project

Run this command to generate a library:

nx generate @jnxplus/nx-boot-maven:library my-lib --projects my-app 
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

4.3 Project targets

@jnxplus/nx-boot-maven allow us to build, test, lint, format and serve Spring Boot projects with the same commands as Nx.

4.4 Project graph

@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 :

nx dep-graph
Enter fullscreen mode Exit fullscreen mode

Alt Text

4.5 To know more

To know more about the plugin, visit the GitHub repo and the documentation
Give it a try :)

Discussion (0)