Prerequisites
- You should have install java 1.8 or above.
- You should have Eclipse installed in your PC.
- Your PC should setup Maven installed and configured.
First of all lets create maven project by click on File-> New -> Maven Project then provide project details (I select create simple project). My project details as follows.
Lets add required dependencies in to the pom file first. I will use the similar pom structure with my inital spring boot project setup.
<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>
<groupId>com.nirmal.blog</groupId>
<artifactId>ReacjsApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Provided -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Runtime -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
But still project will not be able to perform maven install since we have not added springboot initialization class with main method.
Lets add that class with main method in to our project I'll call it as MyApp class. It will contain following details.
package com.nirmal.blog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class MyApp extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApp.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
After our springboot initialization class lets add the spring boot configuration file "application.properties". it should be added it to resource folder.
spring.mvc.view.prefix: /.
spring.mvc.view.suffix: .html
Then lets start develop the Reacjs part of the project. First of all you need to install node in your local machine. You can download nodejs from following location. In my application I'm using node version node-v8.11.3-x64. Install it in your local machine.
Then I created new folder call "reactjs" and then use following command to create new reactjs project inside our maven project structure.
npx create-react-app my-app
after successful installation you should be able to see similar output to following
Then you can go inside the my-app folder and enter following command
npm run-script build
This will build the web application for us on the build folder in our reactjs project as show in bellow.
Then lets start reactjs app by entering following command
nmp start
Then your newly created project should started and you should able to see similar output on your browser.
So far our project structure should be similar to bellow image.
First of all lets set the our application context path. Here we have to set context path in two places. One is for ReactJs and other one is for spring boot.
To set context path on ReactJs we have to modify homepage variable in package.json file.
In my example I set as bellow. you can access whole package json on github location.
"homepage": "http://localhost:8080/ReacjsApp/"
Then lets set the context path on Spring boot application we just need to add that configuration in to application.properties file. For my application configuration would be similar to bellow.
server.servlet.context-path=/ReacjsApp
Then we have to add following configuration to specify the location for static resources which required for the reactjs app to start.
spring.resources.static-locations=classpath:/
Whole pplication.properties file can be access on github location.
Then lets modify this default application application to perform the react installation and build when project perform the maven install. In order to do that we have to add following command in the pom file.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<workingDirectory>src/main/reactjs/my-app</workingDirectory>
<installDirectory>target</installDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v10.11.0</nodeVersion>
<npmVersion>6.4.1</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>npm run</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run-script build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/webapp</outputDirectory>
<resources>
<resource>
<directory>src/main/reactjs/my-app/build</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Now we are ready to install our application using maven. When performing the maven install you can see its executes the npm install and npm run-script build as show in bellow.
After successful maven installation you can see the war file generated on the target folder and you can run the we application using following command without any servlet container.
java -jar ReacjsApp-0.0.1-SNAPSHOT.war
Then you will be able to see spring application starting run and after its completed you will be able to access first reactjs app on SpringBoot on following http://localhost:8080/ReacjsApp/.
Project source can be access on following github location.
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging
ReplyDeleteReact Js Training in Electronic City
This comment has been removed by a blog administrator.
ReplyDeleteValuable post useful for everyone.Keep sharing.
ReplyDeleteReact JS Online training
Hey! This is my first visit to your blog! We are
ReplyDeletea collection of volunteers and starting
a new project in a community in the same niche. Your blog provided us beneficial information to work on. You have done a marvellous job!
React Native Development Company
Reactjs Development Company Texas
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteAfter I to begin with commented I seem to have clicked at the -Notify me whilst new feedback are added- checkbox and now on every occasion a comment is introduced I get preserve of 4 emails with the appropriate identical comment. There has to be an smooth method you're net capable of eliminate me from that provider? Kudos!
ReplyDeleteThis is an wonderful blog with an unique and awesome information.
ReplyDeleteAngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery
Such an great blog with new updates. I like to learn more about this topic.
ReplyDeleteFull Stack Training in Chennai | Certification | Online Training Course| Full Stack Training in Bangalore | Certification | Online Training Course | Full Stack Training in Hyderabad | Certification | Online Training Course | Full Stack Developer Training in Chennai | Mean Stack Developer Training in Chennai | Full Stack Training | Certification | Full Stack Online Training Course
Here at Clavax, we open new doors to controlling commercial and residential property. Our custom Real Estate Software Solution offers management software, broker solutions, accounting, and mobile apps - all designed for more efficient management, selling or buying assets.
ReplyDeleteWhat a fantastic post! This is so chock full of useful information I can't wait to dig deep and start utilizing the resources you have given me. Your exuberance is refreshing. Lots of good advice here, but I want to plead with anybody planning to build auction software real estate to track business activity. Thanks again for this valuable post.
ReplyDeleteAmazing writing! Again, you provide several realistic ways. I want to thank you for your outstanding performance. In most cases, a mobile app development framework can help you choose the best framework to create a new mobile application. Thanks for sharing, as otherwise i would not have thought about trying this solution.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteNice post. Hi, I am Ashika working as an SEO at CronJ. If you are searching for a React App development Company USA then you can contact us and we will provide the services.
ReplyDeleteOnce again you provide several doses of reality which explore the complete explanation of packing and moving companies in Bangalore . This article don't have to be that long. I simply couldn't leave your web site before suggesting that I actually loved the usual info on packing and movers services in Bangalore. I just want to know what is the best way to get real service.
ReplyDeleteThank you so much for sharing this valuable information and here I just want to introduce about App Cost Calculator which can help you determine how much it will cost to build an app and how long it will take to launch. The cost varies depending on the device, functionality, UI/UX, and many other factors that will be discussed in detail.
ReplyDeleteThank you for sharing informative.
ReplyDeleteOur Intelligent Video Analytics Solutions use artificial intelligence and machine learning to identify and remove particles in the video, identify each object using a professional Deep Convolutional Neural network, and classify each object for innovative surveillance analysis, such as search and filtration notifying, and aggregation of data and visualization.
What a great article! This is so jam-packed with helpful information that I can't wait to go in and apply the resources you provided. custom erp
ReplyDeleteI'm glad my information about software development services was helpful.
ReplyDelete