I am developing a web-application using React and I want to make a WAR file of the project which should be deployable. I need help if anyone can provide a good resource or maybe help me out?
-
Did you use create-react-app ? – raj Jun 29 '18 at 19:57
-
Yes I am using Create-React-App. – Asad Awan Jun 30 '18 at 04:26
-
I think this should help you out https://www.megadix.it/blog/create-react-app-servlet/ – raj Jun 30 '18 at 04:55
3 Answers
First, add a pom.xml and make it a maven project and then build it. It will create a War file for you in the target folder after that you can deploy it wherever you want.
pom.xml
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 it.megadix my-app 0.0.1-SNAPSHOT war
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<npm.output.directory>build</npm.output.directory>
</properties>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Standard plugin to generate WAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>${npm.output.directory}</directory>
</resource>
</webResources>
<webXml>${basedir}/web.xml</webXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<!-- Required: The following will ensure `npm install` is called
before anything else during the 'Default Lifecycle' -->
<execution>
<id>npm install (initialize)</id>
<goals>
<goal>exec</goal>
</goals>
<phase>initialize</phase>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>
<!-- Required: The following will ensure `npm install` is called
before anything else during the 'Clean Lifecycle' -->
<execution>
<id>npm install (clean)</id>
<goals>
<goal>exec</goal>
</goals>
<phase>pre-clean</phase>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
</execution>
<!-- Required: This following calls `npm run build` where 'build' is
the script name I used in my project, change this if yours is
different -->
<execution>
<id>npm run build (compile)</id>
<goals>
<goal>exec</goal>
</goals>
<phase>compile</phase>
<configuration>
<executable>npm</executable>
<arguments>
<argument>run</argument>
<argument>build</argument>
</arguments>
</configuration>
</execution>
</executions>
<configuration>
<environmentVariables>
<CI>true</CI>
<!-- The following parameters create an NPM sandbox for CI -->
<NPM_CONFIG_PREFIX>${basedir}/npm</NPM_CONFIG_PREFIX>
<NPM_CONFIG_CACHE>${NPM_CONFIG_PREFIX}/cache</NPM_CONFIG_CACHE>
<NPM_CONFIG_TMP>${project.build.directory}/npmtmp</NPM_CONFIG_TMP>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<environmentVariables>
<PUBLIC_URL>http://localhost:7001/${project.artifactId}</PUBLIC_URL>
<REACT_APP_ROUTER_BASE>/${project.artifactId}</REACT_APP_ROUTER_BASE>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<environmentVariables>
<PUBLIC_URL>http://my-awesome-production-host/${project.artifactId}</PUBLIC_URL>
<REACT_APP_ROUTER_BASE>/${project.artifactId}</REACT_APP_ROUTER_BASE>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Note:- If you find a blank page after running your project then clear your cache or restart your IDE.

- 51
- 2
-
Its not working its giving this error [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 03:19 min [INFO] Finished at: 2021-08-09T15:19:29+05:30 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:exec (npm run build (compile)) on project create-react-app-servlet: Command execution failed.: Process exited with an error: 1 (Exit value – John Aug 09 '21 at 11:32
If you used create-react-app to create your application just add this to your "scripts" section in the package.json:
"compile": "npm run build && cd build && jar -cvf web.war ."
Then to execute do:
npm run compile
It will:
- Run a Production Build
- Go to your /build folder
- Create a war file with the name web.war inside the build folder (Put the name you want)
To configure the Proxy to call the server check out this answer.
Also add this line to your package.json: "homepage": "."
This is how your package.json should look: ...
"homepage": ".",
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"compile": "npm run build && cd build && jar -cvf terminal.war ."
},
...

- 3,179
- 32
- 31
-
`'jar' is not recognized as an internal or external command, operable program or batch file.` – Andremoniy Nov 06 '22 at 10:36
You can use webpack-war-plugin
to build war file:
Create .bablerc
and webpack.config.js
files and their contents in project folder :
.bablerc
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
webpack.config.js
const path = require('path');
const { WebpackWarPlugin } = require('webpack-war-plugin');
const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
mode: 'development',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(html|css)$/,
use: [
{
loader: "html-loader"
}
]
}
]
},
plugins: [
new WebpackWarPlugin(),
new HtmlWebPackPlugin({
template: "./public/index.html",
filename: "./index.html"
})
]
};
Add build war
script and dev dependencies to package.json
file.
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"build war": "webpack"
}
"devDependencies": {
"webpack-cli": "^3.3.11",
"babel-loader": "^8.1.0",
"html-loader": "^1.1.0",
"webpack-war-plugin": "^1.0.0-beta.3",
"@babel/core": "^7.10.2",
"@babel/preset-env": "^7.10.2",
"@babel/preset-react": "^7.10.1"
}
Run npm update
to provide dependencies
Then use build war
script to create war file
.

- 498
- 7
- 17