<?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: JakMar17</title>
    <description>The latest articles on DEV Community by JakMar17 (@jakmar17).</description>
    <link>https://dev.to/jakmar17</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%2F464670%2Fd01a5e2c-15c0-4529-8b29-6fa7e73010ff.png</url>
      <title>DEV Community: JakMar17</title>
      <link>https://dev.to/jakmar17</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jakmar17"/>
    <language>en</language>
    <item>
      <title>Deploy Spring Boot on WildFly application server</title>
      <dc:creator>JakMar17</dc:creator>
      <pubDate>Wed, 17 Mar 2021 15:46:30 +0000</pubDate>
      <link>https://dev.to/jakmar17/deploy-spring-boot-on-wildfly-application-server-2029</link>
      <guid>https://dev.to/jakmar17/deploy-spring-boot-on-wildfly-application-server-2029</guid>
      <description>&lt;p&gt;&lt;strong&gt;By default Spring Boot comes with embedded Tomcat server, which, when build as &lt;em&gt;fat jar&lt;/em&gt; enables deploying application as simple as running &lt;code&gt;java -jar&lt;/code&gt; command. But one could also use some other JavaEE or application server for deployment. Let's look how to deploy Spring Boot project to WildFly.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Disclaimer
&lt;/h3&gt;

&lt;p&gt;For most of the time deploying Spring Boot on application server like WildFly does not actually makes sense since embedded servers (Tomcat by default, Jetty,...) are part of Spring Boot itself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Deploying Spring Boot project to WildFly will be divided into three parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Setting up WildFly (standalone edition)&lt;/li&gt;
&lt;li&gt;Setting up and configuring new Spring Boot project&lt;/li&gt;
&lt;li&gt;Building Spring Boot project and deploying&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Setting up WildFly
&lt;/h2&gt;

&lt;p&gt;Firstly &lt;a href="https://www.wildfly.org/" rel="noopener noreferrer"&gt;download WildFly application server&lt;/a&gt;, after download is complete unzip archive, save it and open terminal in WildFly directory.&lt;/p&gt;

&lt;p&gt;Next step is setting up WildFly user, with terminal move into &lt;code&gt;bin&lt;/code&gt; sub directory and run &lt;code&gt;./add-user.sh&lt;/code&gt; on Linux or &lt;code&gt;add-user.bat&lt;/code&gt; on Windows. Then simply follow those steps:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; What &lt;span class="nb"&gt;type &lt;/span&gt;of user &lt;span class="k"&gt;do &lt;/span&gt;you wish to add?
a &lt;span class="o"&gt;(&lt;/span&gt;Management User&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Enter the details of the new user to add.
Username: wildfly
Password: w1ldFly!
Re-enter Password : w1ldFly!

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; What &lt;span class="nb"&gt;groups &lt;/span&gt;&lt;span class="k"&gt;do &lt;/span&gt;you want this user to belong to? &lt;span class="o"&gt;(&lt;/span&gt;Please enter a comma separated list, or leave blank &lt;span class="k"&gt;for &lt;/span&gt;none&lt;span class="o"&gt;)[&lt;/span&gt;  &lt;span class="o"&gt;]&lt;/span&gt;: &amp;lt;LEAVE BLANK&amp;gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Is this correct &lt;span class="nb"&gt;yes&lt;/span&gt;/no?
&lt;span class="nb"&gt;yes&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;yes&lt;/span&gt;/no?
&lt;span class="nb"&gt;yes&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; Press any key to &lt;span class="k"&gt;continue&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt; &lt;span class="nb"&gt;.&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With tha done, we can run WildFly server using &lt;code&gt;./standalone.sh&lt;/code&gt; (Linux) or &lt;code&gt;.\standalone.bat&lt;/code&gt; (Windows). When initialization is completed you should see log:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

15:28:47,740 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now enter &lt;a href="http://127.0.0.1:9990" rel="noopener noreferrer"&gt;&lt;code&gt;http://127.0.0.1:9990&lt;/code&gt;&lt;/a&gt; into the browser, after entering admin credentials (defined in previous step) you should see WildFly admin console:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzbwrdtyft89uyenriov1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzbwrdtyft89uyenriov1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this we are done setting WildFly server and we are moving on to setting up Spring Boot project.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Setting up and configuring Spring Boot project
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzq8mlpgcvckmfswk79vq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzq8mlpgcvckmfswk79vq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Firstly let's create project on &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;Spring Boot website&lt;/a&gt;, here the only important thing is choosing &lt;code&gt;War&lt;/code&gt; as packaging. &lt;em&gt;And also Maven if you want to follow next steps.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Then click &lt;code&gt;Generate&lt;/code&gt; button, download, unzip archive and import project into your favourite IDE &lt;em&gt;(I will be using IntelliJ, but you could also use Eclipse, NetBeans, VSCode or anything else)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For testing purposes, we will be creating a simple HelloWorld API endpoint. Let's create Java file under &lt;code&gt;/src/main/java//HelloApi.java&lt;/code&gt; in it write following code &lt;em&gt;(package and imports are not shown for readbility, but at the end of this article I posted Git repository with source code)&lt;/em&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;

&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloApi&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/hello"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Hello from Spring Boot"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You could try building (&lt;code&gt;mvn clean package&lt;/code&gt;) and running (&lt;code&gt;java -jar /target/&amp;lt;APP-NAME&amp;gt;.jar&lt;/code&gt;). While accessing &lt;code&gt;http://localhost:8080/hello&lt;/code&gt; you should see &lt;code&gt;Hello from Spring Boot&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqkkb3g4jeqgcfs8w279o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqkkb3g4jeqgcfs8w279o.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's configure Spring Boot project to deploy it on WebFly. Firstly we will remove Tomcat embedded server from out project. In the &lt;code&gt;pom.xml&lt;/code&gt; find &lt;code&gt;spring-boot-starter-tomcat&lt;/code&gt; dependency and make sure that &lt;code&gt;&amp;lt;scope&amp;gt;&lt;/code&gt; is set to &lt;code&gt;provided&lt;/code&gt; if not set it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;         
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.springframework.boot&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;spring-boot-starter-tomcat&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;scope&amp;gt;&lt;/span&gt;provided&lt;span class="nt"&gt;&amp;lt;/scope&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And lets exclude some logging libraries that may break deployment:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.springframework.boot&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;spring-boot-starter-web&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;exclusions&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;exclusion&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;ch.qos.logback&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;logback-classic&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/exclusion&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/exclusions&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next thing we will add some WildFly properties:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;properties&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;java.version&amp;gt;&lt;/span&gt;15&lt;span class="nt"&gt;&amp;lt;/java.version&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;deploy.wildfly.host&amp;gt;&lt;/span&gt;127.0.0.1&lt;span class="nt"&gt;&amp;lt;/deploy.wildfly.host&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;deploy.wildfly.port&amp;gt;&lt;/span&gt;9990&lt;span class="nt"&gt;&amp;lt;/deploy.wildfly.port&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;deploy.wildfly.username&amp;gt;&lt;/span&gt;wildfly&lt;span class="nt"&gt;&amp;lt;/deploy.wildfly.username&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;deploy.wildfly.password&amp;gt;&lt;/span&gt;w1ldFly!&lt;span class="nt"&gt;&amp;lt;/deploy.wildfly.password&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/properties&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's break it down: &lt;code&gt;java.version&lt;/code&gt; defines JDK version that project is build upon (it is already set), &lt;code&gt;deploy.wildfly.XX&lt;/code&gt; are properties that we need for deploying app on WildFly, &lt;code&gt;host&lt;/code&gt; and &lt;code&gt;port&lt;/code&gt; are where WildFly is "living", &lt;code&gt;username&lt;/code&gt; and &lt;code&gt;password&lt;/code&gt; are same as defined in previous step.&lt;/p&gt;

&lt;p&gt;Lets use them in build plugin:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.wildfly.plugins&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;wildfly-maven-plugin&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;2.0.2.Final&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;phase&amp;gt;&lt;/span&gt;install&lt;span class="nt"&gt;&amp;lt;/phase&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;deploy&lt;span class="nt"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;                
        &lt;span class="nt"&gt;&amp;lt;filename&amp;gt;&lt;/span&gt;${project.build.finalName}.war&lt;span class="nt"&gt;&amp;lt;/filename&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;hostname&amp;gt;&lt;/span&gt;${deploy.wildfly.host}&lt;span class="nt"&gt;&amp;lt;/hostname&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;port&amp;gt;&lt;/span&gt;${deploy.wildfly.port}&lt;span class="nt"&gt;&amp;lt;/port&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;username&amp;gt;&lt;/span&gt;${deploy.wildfly.username}&lt;span class="nt"&gt;&amp;lt;/username&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;password&amp;gt;&lt;/span&gt;${deploy.wildfly.password}&lt;span class="nt"&gt;&amp;lt;/password&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;With that done make sure everything is still building (run &lt;code&gt;mvn clean package&lt;/code&gt;) and lets move on to the next step.&lt;/p&gt;

&lt;p&gt;Last thing we will do in our Spring Boot project is setting up context root path (we will set it up to &lt;code&gt;/&lt;/code&gt; so our app will be available on &lt;code&gt;domain.com/hello&lt;/code&gt;). Firstly create new file under &lt;code&gt;/src/main/webapp/WEB-INF/jboss-web.xml&lt;/code&gt; secondly add to the file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;

&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;jboss-web&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"http://www.jboss.com/xml/ns/javaee"&lt;/span&gt;
           &lt;span class="na"&gt;xmlns:xsi=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/2001/XMLSchema-instance"&lt;/span&gt;
           &lt;span class="na"&gt;xsi:schemaLocation=&lt;/span&gt;&lt;span class="s"&gt;"
      http://www.jboss.com/xml/ns/javaee
      http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;context-root&amp;gt;&lt;/span&gt;/&lt;span class="nt"&gt;&amp;lt;/context-root&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/jboss-web&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here the only important part is &lt;code&gt;&amp;lt;context-root&amp;gt;/&amp;lt;/context-root&amp;gt;&lt;/code&gt; where we define context-root.&lt;/p&gt;

&lt;p&gt;With this out of the way we build out project (&lt;code&gt;mvn clean package&lt;/code&gt;) and deploy our Spring Boot project to WildFly server.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Deploying Spring Boot to WildFly
&lt;/h2&gt;

&lt;p&gt;After succesful build you should find &lt;code&gt;.war&lt;/code&gt; file in &lt;code&gt;/target&lt;/code&gt; directory, this is our project's final form that we will deploy to WildFly.&lt;/p&gt;

&lt;p&gt;In WildFly's admin console chose &lt;code&gt;Deployments&lt;/code&gt; in navbar and then &lt;code&gt;Upload Deployment&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgf3vo2o6n577q25epadi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgf3vo2o6n577q25epadi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now all we have to do is uplaod &lt;code&gt;war&lt;/code&gt; file and deploy it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8esua98t30pdk1cmiotk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8esua98t30pdk1cmiotk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After successful deployment you could go to &lt;a href="http://127.0.0.1:8080/hello" rel="noopener noreferrer"&gt;http://127.0.0.1:8080/hello&lt;/a&gt; and see our final result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F02lc1c19d5qi1n4w37ti.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F02lc1c19d5qi1n4w37ti.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We could (and should) skip last step altogether and simply use Maven for deploying to WildFly by using&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;mvn clean install&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Source code is available on [GitHub](https://github.com/JakMar17/SpringBoot-WildFly)

*Cover photo by Min An from Pexels*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>java</category>
      <category>maven</category>
      <category>wildfly</category>
      <category>springboot</category>
    </item>
    <item>
      <title>Deploying Angular application inside Spring Boot</title>
      <dc:creator>JakMar17</dc:creator>
      <pubDate>Tue, 16 Mar 2021 19:15:58 +0000</pubDate>
      <link>https://dev.to/jakmar17/deploying-angular-application-inside-spring-boot-57g7</link>
      <guid>https://dev.to/jakmar17/deploying-angular-application-inside-spring-boot-57g7</guid>
      <description>&lt;p&gt;&lt;strong&gt;Imagine scenario: you just developed frontend application written in Angular and powered by Spring Boot Rest API as backend. When deploying most of the time you'll deploy each application as its own container (frontend on Firebase, backend on Heroku, etc.). But did you know you can deploy both apps as a single JAR or WAR file?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When and why?
&lt;/h2&gt;

&lt;p&gt;Deploying Angular/Spring Boot (or actually any Java project in general) in one file is a great way of deployment when your backend and frontend are developed together (and will be in foreseeable future) or maybe when you, your team or your company are not big fans of microservice approach (or just wanting to produce monolith project).&lt;/p&gt;

&lt;p&gt;That kind of monolith approach simplifies (manual) deployment of the app, but it does have one big cons: whenever you update either backend or frontend you'll have to create new JAR/WAR file, so this approach may not be best for CI/CD powered projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  How?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; some parts of this tutorial may not be best practices used in production or best practice for building apps used by thousands or millions of users. But it is how our team handles application deployment for production applications used every day by hundreds of users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  0. &lt;code&gt;useHash&lt;/code&gt; strategy for Angular routing
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;This is simplest way of &lt;em&gt;"fixing"&lt;/em&gt; refreshing and direct access to Angular app via URL. You could also rewrite all routes to &lt;code&gt;index.html&lt;/code&gt; file (by changing your server's configuration).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Find your routing file (&lt;code&gt;app-routing.module.ts&lt;/code&gt;) and add/replace those lines of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
&lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;RouterModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;useHash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})],&lt;/span&gt;
&lt;span class="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;RouterModule&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppRoutingModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From now on all your Angular URLs has &lt;code&gt;/#/&lt;/code&gt; between domain (with or without port) and subpaths.&lt;br&gt;
&lt;em&gt;For example: &lt;code&gt;localhost:4200/login/&lt;/code&gt; is now &lt;code&gt;localhost:4200/#/login/&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Build Angular frontend
&lt;/h3&gt;

&lt;p&gt;Firstly check your &lt;code&gt;index.html&lt;/code&gt; file in Angular project. In it find this line (if you cannot find it, add it between &lt;code&gt;and&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
&lt;span class="nt"&gt;&amp;lt;base&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Secondly, build your angular project using AngularCLI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
ng build &lt;span class="nt"&gt;--prod&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;If your app is going to be run on subpath (for example &lt;code&gt;domain.com/my-app/&lt;/code&gt;) then add &lt;code&gt;--base-href=/my-app/&lt;/code&gt; to build command.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2. Copy Angular dist folder to backend
&lt;/h3&gt;

&lt;p&gt;After successfully building Angular check &lt;code&gt;/dist&lt;/code&gt; folder, inside you should find subfolder named like your app, inside it you'll find some html, js and css files.&lt;/p&gt;

&lt;p&gt;Copy those files into your backend &lt;code&gt;/src/main/resources/public/&lt;/code&gt; (if some of the folders are missing add them).&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Build and run backend project
&lt;/h3&gt;

&lt;p&gt;Clean build your backend using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mvn clean package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After building run it using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;java -jar ./target/.jar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this will not work if your project's configuration is set to building &lt;code&gt;WAR&lt;/code&gt; if this is the case, you need to deploy application to one of Java application servers (like Tomcat, Wildfly or Glasfish). This is out of scope for this article.&lt;/p&gt;

&lt;p&gt;After running Spring Boot app you should see logs from &lt;code&gt;WelcomePageHandlerMapping&lt;/code&gt;, they sould be somthing like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="no"&gt;INFO&lt;/span&gt; &lt;span class="mi"&gt;10732&lt;/span&gt; &lt;span class="o"&gt;---&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;WelcomePageHandlerMapping&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Adding&lt;/span&gt; &lt;span class="n"&gt;welcome&lt;/span&gt; &lt;span class="nl"&gt;page:&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;path&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kd"&gt;public&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;html&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are done :)&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  4. Accessing frontend
&lt;/h3&gt;

&lt;p&gt;When your project is up and runnning you can access your frontend on &lt;code&gt;domain.com/#/&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>angular</category>
      <category>java</category>
      <category>deployment</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
