reactjsmavennpmnpm-start

How to compile and deploy ReactJS app in Maven Project


I have been trying to compile and deploy React in Maven as our Jenkins pipeline has only Maven and not npm.

My task is like a POC. I want to deploy a react application in Maven.

I used the maven plugin

 <build>
        <plugins>
            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <!-- NB! Set <version> to the latest released version of frontend-maven-plugin, like in README.md -->
                <version>1.12.1</version>
                <configuration>
                    <nodeVersion>v16.17.1</nodeVersion>
                    <npmVersion>8.15.0</npmVersion>
                    <workingDirectory>app</workingDirectory>
                </configuration>
                <executions>

                    <execution>
                        <id>install node and npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                        <phase>generate-resources</phase>
                    </execution>


                    <execution>
                        <id>npm install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <arguments>install</arguments>
                        </configuration>
                    </execution>


                    <execution>
                        <id>npm run-script build</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <phase>generate-resources</phase>
                        <configuration>
                            <arguments>install</arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

When I execute mvn clean install, the dependencies in package.json and package-lock.json are installed.

I can even run npm install. But I am not able to execute npm start

I am getting error like this:

npm ERR! 
npm ERR! Did you mean one of these?
npm ERR!     npm star # Mark your favorite packages
npm ERR!     npm stars # View packages marked as favorites
npm ERR! 
npm ERR! To see a list of scripts, run:
npm ERR!   npm run

The tutorial videos show only React and Springboot deployment in Maven and not React alone in Maven.

Since our backend is not Springboot rather AWS, we need not deploy the React with Spring boot.

Can anyone please give me leads on what is missing here?

What mistake I am doing while compiling and deploying a React app in Maven? Which command I should use.

How can I run an react application from Maven?


Solution

  • This is the pom configuration which works for me now.

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>   
       
        <packaging>pom</packaging>    
       
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <frontend.maven.plugin.version>1.12.1</frontend.maven.plugin.version>
            <node.version>v18.12.1</node.version>
            <npm.version>8.19.2</npm.version>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <version>${frontend.maven.plugin.version}</version>
                    <configuration>
                        <nodeVersion>${node.version}</nodeVersion>
                        <npmVersion>${npm.version}</npmVersion>
                        <installDirectory>target</installDirectory>
                    </configuration>
                    <executions>
    
                        <execution>
                            <id>install node and npm</id>
                            <goals>
                                <goal>install-node-and-npm</goal>
                            </goals>
                            <phase>generate-resources</phase>
                        </execution>
    
    
                        <execution>
                            <id>npm install</id>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <phase>generate-resources</phase>
                            <configuration>
                                <arguments>install</arguments>
                            </configuration>
                        </execution>
    
    
                        <execution>
                            <id>npm run-script build</id>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <phase>generate-resources</phase>
                            <configuration>
                                <arguments>install</arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <profiles>
            <profile>
                <id>build-dev</id>
                <build>
                <plugins>
                <plugin>
                    <groupId>com.github.eirslett</groupId>
                    <artifactId>frontend-maven-plugin</artifactId>
                    <version>${frontend.maven.plugin.version}</version>
                    <configuration>
                        <nodeVersion>${node.version}</nodeVersion>
                        <npmVersion>${npm.version}</npmVersion>
                        <installDirectory>target</installDirectory>
                    </configuration>
                    <executions>
    
                        <execution>
                            <id>install node and npm</id>
                            <goals>
                                <goal>install-node-and-npm</goal>
                            </goals>
                            <phase>generate-resources</phase>
                        </execution>
    
    
                        <execution>
                            <id>npm install</id>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <phase>generate-resources</phase>
                            <configuration>
                                <arguments>install</arguments>
                            </configuration>
                        </execution>
    
    
                        <execution>
                            <id>npm run-script build</id>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <phase>generate-resources</phase>
                            <configuration>
                                <arguments>install</arguments>
                            </configuration>
                        </execution>
    
    
                        <execution>
                            <id>npm build</id>
                            <goals>
                                <goal>npm</goal>
                            </goals>
                            <phase>generate-resources</phase>
                            <configuration>
                                <arguments>run build:dev</arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${basedir}/target/classes/META-INF/resources</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>dest</directory>
                                        <filtering>false</filtering>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <appendAssemblyId>false</appendAssemblyId>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly-zip</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                            <configuration>
                                <descriptors>
                                    <descriptor>create-zip.xml</descriptor>
                                </descriptors>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                </plugins>
                </build>
            </profile>
        </profiles>
    </project>