{"id":6507,"date":"2018-09-27T19:34:27","date_gmt":"2018-09-27T19:34:27","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6507"},"modified":"2018-09-27T19:34:27","modified_gmt":"2018-09-27T19:34:27","slug":"creating-a-java-web-application-with-intellij","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-a-java-web-application-with-intellij\/","title":{"rendered":"Creating a Java Web Application with IntelliJ"},"content":{"rendered":"<p><strong>Creating our project<\/strong><\/p>\n<ul>\n<li>Choose File | New | Project<\/li>\n<li>Select Java Enterprise<\/li>\n<li>Then tick the Web Application<\/li>\n<\/ul>\n<p><em>If you have an application server setup, select or add it using New, I&#8217;m going to ultimately add an embedded Tomcat container, so leaving this blank.<\/em><\/p>\n<p>Finally give the project a name, i.e. MyWebApp.<\/p>\n<p><strong>Adding a Maven pom.xml<\/strong><\/p>\n<p>I want to use Maven to import packages, so add a pom.xml to the project root add then supply the bare bones (as follows)<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;project xmlns=&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/maven-v4_0_0.xsd&quot;&gt;\r\n    &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\r\n    &lt;groupId&gt;MyWebApp&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;MyWebApp&lt;\/artifactId&gt;\r\n    &lt;packaging&gt;war&lt;\/packaging&gt;\r\n    &lt;name \/&gt;\r\n    &lt;version&gt;MyWebApp.0.0.1-SNAPSHOT&lt;\/version&gt;\r\n    &lt;description \/&gt;\r\n\r\n&lt;\/project&gt;\r\n<\/pre>\n<p>In IntelliJ, select the pom.xml, right mouse click and select Add as Maven project.<\/p>\n<p>Next, we want to tell Maven how to compile and generate our war, so add the following after the description tag in the pom.xml<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;build&gt;\r\n   &lt;sourceDirectory&gt;${basedir}\/src&lt;\/sourceDirectory&gt;\r\n   &lt;outputDirectory&gt;${basedir}\/web\/WEB-INF\/classes&lt;\/outputDirectory&gt;\r\n   &lt;resources&gt;\r\n      &lt;resource&gt;\r\n         &lt;directory&gt;${basedir}\/src&lt;\/directory&gt;\r\n         &lt;excludes&gt;\r\n            &lt;exclude&gt;**\/*.java&lt;\/exclude&gt;\r\n         &lt;\/excludes&gt;\r\n      &lt;\/resource&gt;\r\n   &lt;\/resources&gt;\r\n   &lt;plugins&gt;\r\n      &lt;plugin&gt;\r\n         &lt;artifactId&gt;maven-war-plugin&lt;\/artifactId&gt;\r\n         &lt;configuration&gt;\r\n            &lt;webappDirectory&gt;${basedir}\/web&lt;\/webappDirectory&gt;\r\n            &lt;warSourceDirectory&gt;${basedir}\/web&lt;\/warSourceDirectory&gt;\r\n         &lt;\/configuration&gt;\r\n      &lt;\/plugin&gt;\r\n      &lt;plugin&gt;\r\n         &lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\r\n         &lt;configuration&gt;\r\n            &lt;source&gt;1.8&lt;\/source&gt;\r\n            &lt;target&gt;1.8&lt;\/target&gt;\r\n         &lt;\/configuration&gt;\r\n      &lt;\/plugin&gt;\r\n      &lt;plugin&gt;\r\n         &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n         &lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\r\n         &lt;configuration&gt;\r\n            &lt;source&gt;8&lt;\/source&gt;\r\n            &lt;target&gt;8&lt;\/target&gt;\r\n         &lt;\/configuration&gt;\r\n      &lt;\/plugin&gt;\r\n   &lt;\/plugins&gt;\r\n&lt;\/build&gt;\r\n<\/pre>\n<p><strong>Creating a run configuration<\/strong><\/p>\n<p>Whilst my intention is to add a Tomcat embedded server, we can create a new run configuration at this point to test everything worked.<\/p>\n<p>Select the Run, Edit Configuration option from the toolbar or Run | Edit Configuration and click + and add a Tomcat Server | Local.<\/p>\n<p>Mine&#8217;s set with the URL http:\/\/localhost:8080\/. Give the configuration a name and don&#8217;t forget to select the Deployment tab, press the + and then click Aritifact\u2026 I selected MyWebApp:war.<\/p>\n<p>Press OK and OK again to finish the configuration and now you can run Tomcat locally and deploy the war.<\/p>\n<p>Don&#8217;t forget to execute <em>mvn install<\/em> to build you war file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating our project Choose File | New | Project Select Java Enterprise Then tick the Web Application If you have an application server setup, select or add it using New, I&#8217;m going to ultimately add an embedded Tomcat container, so leaving this blank. Finally give the project a name, i.e. MyWebApp. Adding a Maven pom.xml [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[228,161,227],"tags":[],"class_list":["post-6507","post","type-post","status-publish","format-standard","hentry","category-j2ee","category-java","category-jsp"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6507"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6507\/revisions"}],"predecessor-version":[{"id":6513,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6507\/revisions\/6513"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}