Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 26, 2021 11:26 am GMT

Announcing @jnxplus/nx-boot-maven

In the previous article, I show you how to add a 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

When asked provide my-product as name and choose an empty workspace :

devs> npx create-nx-workspace@latestnpx: 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.

2. Install @jnxplus/nx-boot-maven

In the same terminal go inside my-product folder :

cd my-product 

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

3. Init worspace with Add Spring boot and Maven support

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-SNAPSHOTCREATE mvnwCREATE mvnw.cmdCREATE pom.xmlCREATE .mvn/wrapper/maven-wrapper.jarCREATE .mvn/wrapper/maven-wrapper.propertiesCREATE .mvn/wrapper/MavenWrapperDownloader.javaCREATE tools/linters/checkstyle.xmlCREATE tools/linters/pmd.xmlUPDATE nx.jsonUPDATE .gitignoreUPDATE .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.
  • 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

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?  .propertiesUPDATE workspace.jsonUPDATE nx.jsonCREATE apps/my-app/pom.xmlCREATE apps/my-app/src/main/java/com/example/myapp/HelloController.javaCREATE apps/my-app/src/main/java/com/example/myapp/MyAppApplication.javaCREATE apps/my-app/src/main/resources/application.propertiesCREATE apps/my-app/src/test/java/com/example/myapp/HelloControllerTests.javaCREATE apps/my-app/src/test/resources/application.propertiesUPDATE pom.xml

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 

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-SNAPSHOTUPDATE workspace.jsonUPDATE nx.jsonCREATE libs/my-lib/pom.xmlCREATE libs/my-lib/src/main/java/com/example/mylib/HelloService.javaCREATE libs/my-lib/src/test/java/com/example/mylib/HelloServiceTests.javaCREATE libs/my-lib/src/test/java/com/example/mylib/TestConfiguration.javaUPDATE pom.xmlUPDATE apps/my-app/pom.xml

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

Alt Text

4.5 To know more

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


Original Link: https://dev.to/gridou/announcing-jnxplus-nx-boot-maven-4g28

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To