乐闻世界logo
搜索文章和话题

How to deploy a node.js app with maven?

1个答案

1

Traditionally, Maven is a build tool primarily used for Java projects, managing the project's build, reporting, and documentation through a project object model file named pom.xml. However, for Node.js applications, we typically use package and task management tools like npm or yarn. Nevertheless, if your project includes both Java and Node.js modules, or if you aim to unify the build process in a Maven-centric environment, you can configure Maven to manage and deploy Node.js applications.

Step 1: Add Node.js and NPM to the Project

First, add the frontend-maven-plugin to your pom.xml file. This plugin allows Maven to download specified versions of Node.js and npm and use them to build frontend projects.

xml
<build> <plugins> <plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> <version>1.11.3</version> <configuration> <nodeVersion>v14.15.1</nodeVersion> <npmVersion>6.14.8</npmVersion> <workingDirectory>path to your Node.js application</workingDirectory> </configuration> <executions> <execution> <id>install node and npm</id> <goals> <goal>install-node-and-npm</goal> </goals> </execution> <execution> <id>npm install</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>install</arguments> </configuration> </execution> <execution> <id>build</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>run build</arguments> </configuration> </execution> </executions> </plugin> </plugins> </build>

Step 2: Configure NPM Build Scripts

In your Node.js project's package.json file, ensure that there is a "build" script that Maven will invoke to build the frontend.

json
{ "scripts": { "build": "react-scripts build" } }

Step 3: Deployment

Once the Maven project is configured, you can use Maven commands to execute the build and deployment process.

bash
mvn clean install

This command triggers the Maven lifecycle, which includes cleaning the project, installing Node.js and npm, running npm install and npm run build, etc.

Real-World Example

In a previous project, we had a Spring Boot application and a React frontend. Both the build and deployment required automation via Jenkins. Since the company's build process is based on Maven, we used the frontend-maven-plugin to integrate Node.js build into the Maven lifecycle. This way, during each CI/CD pipeline run, both frontend and backend are built and deployed simultaneously, ensuring consistency and automation in the build process.

Conclusion

Although Maven is not designed specifically for Node.js, by using the frontend-maven-plugin, we can effectively integrate Node.js build and deployment processes into Maven-driven projects, achieving automated simultaneous builds for frontend and backend. This is particularly helpful for managing multi-technology stack projects.

2024年6月29日 12:07 回复

你的答案