Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Intellij to Build War and Deploy to Tomcat

My basic workflow is in terminal mvn clean install on my base directory. This creates a target directory with a WAR file. I copy that war file into the webapps directory of my Tomcat.

If I make changes I obviously have to repeat the process which is tedious. I am using Intellij Ultimate 13.1.6 and I know it is possible to have all of this done through the IDE based on this guide.

So before I begin I run mvn clean on my base which removes the target directory.

Now to start I when into the Project Structure --> Artifacts and created a new Web Application: Archive. I named it project_name and it had a different output directory by default (baseDir/out/artifacts/project_name vs baseDir/target) which I left everything default and hit ok.

Now I go to Run --> Edit Configurations and create a new Local Tomcat Server using the following settingsenter image description here

and in the Deployment Tab I add the artifact which is built in my Before launch as seen in my picture. Pretty straightforward.

However, whenever I run the server through Intellij it successfully starts but nothing is deployed. I get the error:

Artifact project_name: com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException: /Users/path/to/workspace/project_name/out/artifacts/project_name/project_name.war not found for the web module.

And I double check my directory and as seen in the error, nothing is created. Not even the out directory.

What am I doing wrong? I know one workaround is to go back to terminal, run mvn clean install and with that .war file that is created, put that into the deployment configuration. I felt like that was more of a hack since I am still doing it through command line and if I don't have it already built I can't set the target as a directory to find the war file.

Any assistance is appreciated.

like image 405
Joey Avatar asked Jul 29 '15 16:07

Joey


People also ask

Can I use Tomcat with IntelliJ?

If you're not creating a new project from scratch and instead have an existing project that runs on Tomcat, you can configure IntelliJ IDEA Ultimate to connect to your existing Tomcat installation. Let's work with this application from GitHub: Clone the Project in IntelliJ IDEA and then go to Run > Edit Configurations.

How can I deploy WAR file in Tomcat?

Perhaps the simplest way to deploy a WAR file to Tomcat is to copy the file to Tomcat's webapps directory. Copy and paste WAR files into Tomcat's webapps directory to deploy them. Tomcat monitors this webapps directory for changes, and if it finds a new file there, it will attempt to deploy it.


2 Answers

Checklist of things you can do to verify where things go wrong:

  1. Manually build the artifacts using Build > Build artifacts.
  2. Verify if the artifact is in the expected output directory (out/artifacts/xxx).
  3. If not, go to File > Project structure > Artifacts
  4. Select the artifact file in question (project_name in this case).
  5. Verify if the output directory is correct. Optionally try to create this directory yourselves in case IntelliJ doesn't do it (although this would surprise me)
  6. Verify if the contents are correct.
  7. Optionally check "Build on make" to always have the latest version of your code deployed whenever you run the server.

Note that for deploying WAR files to a Tomcat, you should prefer to use the exploded WAR. This makes deploying/testing/debugging significantly faster.

like image 52
xaviert Avatar answered Sep 19 '22 17:09

xaviert


I got this error when I tried to launch an app by using Run. The problem was that for some reason the WAR file wasn't deployed even after selecting Build->Build Artifacts. Solution: I closed Idea then reimported the project and choose as artifact war-exploded. This time the deploy worked.

like image 21
vovahost Avatar answered Sep 21 '22 17:09

vovahost