<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thomas Zehe</title>
    <description>The latest articles on DEV Community by Thomas Zehe (@tzehe).</description>
    <link>https://dev.to/tzehe</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F103632%2Ff12de984-5ff6-4219-87a3-edefe3344498.jpeg</url>
      <title>DEV Community: Thomas Zehe</title>
      <link>https://dev.to/tzehe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tzehe"/>
    <language>en</language>
    <item>
      <title>Dev recipes: Integrate React frontend in a Java corporate environment</title>
      <dc:creator>Thomas Zehe</dc:creator>
      <pubDate>Tue, 25 Sep 2018 13:34:16 +0000</pubDate>
      <link>https://dev.to/tzehe/dev-recipes-integrate-react-frontend-in-a-java-corporate-environment-2e43</link>
      <guid>https://dev.to/tzehe/dev-recipes-integrate-react-frontend-in-a-java-corporate-environment-2e43</guid>
      <description>&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;You find yourself in a Java environment and want to write a frontend or testpage with React? You want to bootstrap your application with create-react-app but don't know how to integrate it in the current build steps? Then this is for you!&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Maven Frontend Plugin (&lt;a href="https://github.com/eirslett/frontend-maven-plugin"&gt;https://github.com/eirslett/frontend-maven-plugin&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Maven 3&lt;/li&gt;
&lt;li&gt;a react application&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Add frontend-maven-plugin to your pom.xml
&lt;/h2&gt;

&lt;p&gt;Example pom.xml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;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"&amp;gt;
 &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;

 &amp;lt;groupId&amp;gt;com.zehethomas&amp;lt;/groupId&amp;gt;
 &amp;lt;artifactId&amp;gt;react-spring-example&amp;lt;/artifactId&amp;gt;
 &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;

 &amp;lt;packaging&amp;gt;jar&amp;lt;/packaging&amp;gt;

 &amp;lt;parent&amp;gt;
  &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
  &amp;lt;artifactId&amp;gt;spring-boot-starter-parent&amp;lt;/artifactId&amp;gt;
  &amp;lt;version&amp;gt;2.0.4.RELEASE&amp;lt;/version&amp;gt;
  &amp;lt;relativePath/&amp;gt; &amp;lt;!-- lookup parent from repository --&amp;gt;
 &amp;lt;/parent&amp;gt;


 &amp;lt;properties&amp;gt;
  &amp;lt;java.version&amp;gt;1.8&amp;lt;/java.version&amp;gt;
 &amp;lt;/properties&amp;gt;

 &amp;lt;dependencies&amp;gt;
  &amp;lt;dependency&amp;gt;
   &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
   &amp;lt;artifactId&amp;gt;spring-boot-starter-web&amp;lt;/artifactId&amp;gt;
  &amp;lt;/dependency&amp;gt;
 &amp;lt;/dependencies&amp;gt;

 &amp;lt;build&amp;gt;
  &amp;lt;plugins&amp;gt;
   &amp;lt;plugin&amp;gt;
    &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;spring-boot-maven-plugin&amp;lt;/artifactId&amp;gt;
   &amp;lt;/plugin&amp;gt;


   &amp;lt;!--
      1. used for local installation of node and npm
      2. to install dependencies with npm install
      3. building the application
    --&amp;gt;
   &amp;lt;plugin&amp;gt;
    &amp;lt;groupId&amp;gt;com.github.eirslett&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;frontend-maven-plugin&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;1.6&amp;lt;/version&amp;gt;
    &amp;lt;configuration&amp;gt;
     &amp;lt;nodeVersion&amp;gt;v10.0.0&amp;lt;/nodeVersion&amp;gt;
     &amp;lt;npmVersion&amp;gt;6.1.0&amp;lt;/npmVersion&amp;gt;
     &amp;lt;workingDirectory&amp;gt;src/main/js/frontend&amp;lt;/workingDirectory&amp;gt;
    &amp;lt;/configuration&amp;gt;
    &amp;lt;executions&amp;gt;
     &amp;lt;execution&amp;gt;
      &amp;lt;id&amp;gt;Install node and npm locally to the project&amp;lt;/id&amp;gt;
      &amp;lt;goals&amp;gt;
       &amp;lt;goal&amp;gt;install-node-and-npm&amp;lt;/goal&amp;gt;
      &amp;lt;/goals&amp;gt;
     &amp;lt;/execution&amp;gt;
     &amp;lt;execution&amp;gt;
      &amp;lt;id&amp;gt;npm install&amp;lt;/id&amp;gt;
      &amp;lt;goals&amp;gt;
       &amp;lt;goal&amp;gt;npm&amp;lt;/goal&amp;gt;
      &amp;lt;/goals&amp;gt;
     &amp;lt;/execution&amp;gt;
     &amp;lt;execution&amp;gt;
      &amp;lt;id&amp;gt;Build frontend&amp;lt;/id&amp;gt;
      &amp;lt;goals&amp;gt;
       &amp;lt;goal&amp;gt;npm&amp;lt;/goal&amp;gt;
      &amp;lt;/goals&amp;gt;
      &amp;lt;configuration&amp;gt;
       &amp;lt;arguments&amp;gt;run build&amp;lt;/arguments&amp;gt;
      &amp;lt;/configuration&amp;gt;
     &amp;lt;/execution&amp;gt;
    &amp;lt;/executions&amp;gt;
   &amp;lt;/plugin&amp;gt;
   &amp;lt;plugin&amp;gt;
    &amp;lt;groupId&amp;gt;org.apache.maven.plugins&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;maven-resources-plugin&amp;lt;/artifactId&amp;gt;
    &amp;lt;executions&amp;gt;
     &amp;lt;execution&amp;gt;
      &amp;lt;id&amp;gt;Copy frontend build to target&amp;lt;/id&amp;gt;
      &amp;lt;phase&amp;gt;process-resources&amp;lt;/phase&amp;gt;
      &amp;lt;goals&amp;gt;
       &amp;lt;goal&amp;gt;copy-resources&amp;lt;/goal&amp;gt;
      &amp;lt;/goals&amp;gt;
      &amp;lt;configuration&amp;gt;
       &amp;lt;outputDirectory&amp;gt;${basedir}/target/classes/resources&amp;lt;/outputDirectory&amp;gt;
       &amp;lt;resources&amp;gt;
        &amp;lt;resource&amp;gt;
         &amp;lt;directory&amp;gt;${basedir}/src/main/js/frontend/build&amp;lt;/directory&amp;gt;
         &amp;lt;filtering&amp;gt;true&amp;lt;/filtering&amp;gt;
        &amp;lt;/resource&amp;gt;
       &amp;lt;/resources&amp;gt;
      &amp;lt;/configuration&amp;gt;
     &amp;lt;/execution&amp;gt;
    &amp;lt;/executions&amp;gt;
   &amp;lt;/plugin&amp;gt;
  &amp;lt;/plugins&amp;gt;
 &amp;lt;/build&amp;gt;
&amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Uses maven-frontend-plugin to install node and npm locally. Afterwards runs &lt;code&gt;npm install&lt;/code&gt; to load all dependencies and builds the application with &lt;code&gt;npm run build&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;maven-resources-plugin is used to copy the frontend build to target. &lt;br&gt;
Important is to change &lt;code&gt;&amp;lt;directory&amp;gt;${basedir}/build&amp;lt;/directory&amp;gt;&lt;/code&gt;&lt;br&gt;
to the location where your frontend code is located.&lt;/p&gt;
&lt;h2&gt;
  
  
  Connecting React app with Backend
&lt;/h2&gt;

&lt;p&gt;If you encounter errors this is probably because of the same-origin-policy &lt;a href="https://en.wikipedia.org/wiki/Same-origin_policy"&gt;Same-origin policy&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;You can fix this easily by adding &lt;code&gt;"proxy": "http://localhost:8080/"&lt;/code&gt; to your package.json file which proxies the routes to your backend.&lt;/p&gt;

&lt;p&gt;Example package.json&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "react-scripts": "1.1.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:8080/"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Create a .npmrc file
&lt;/h2&gt;

&lt;p&gt;This is necessary if your company uses a private npm registry to prevent npm 404 errors when fetching dependencies &lt;/p&gt;

&lt;h2&gt;
  
  
  Example .npmrc
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;registry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//&amp;lt;your.local.registry&amp;gt;/  &lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;babel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;registry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//registry.npmjs.org/&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;types&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;registry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//registry.npmjs.org/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Build and run application
&lt;/h2&gt;

&lt;p&gt;You can now run &lt;code&gt;maven clean package&lt;/code&gt; to build &lt;code&gt;react-spring-example-0.0.1-SNAPSHOT.jar&lt;/code&gt; which resides in your target folder.&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;java -jar react-spring-example-0.0.1-SNAPSHOT.jar&lt;/code&gt; to start your application. &lt;/p&gt;

&lt;p&gt;Go to localhost:8080 and hurrraii you are done !:&amp;gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Source Code
&lt;/h2&gt;

&lt;p&gt;You can find an example project on github &lt;a href="https://github.com/tzehe/react-spring-example.git"&gt;react-spring-example&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you!&lt;/p&gt;

&lt;p&gt;Sources:&lt;br&gt;
Inspired me to write this.&lt;br&gt;
&lt;a href="http://matejsprogblog.blogspot.com/2017/06/creating-new-web-app-using-create-react.html"&gt;Creating a new web app using create-react-app and Spring Boot&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>java</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
