How to deploy Maven project as jar file to Tomcat? - java

I am using eclipse IDE mars version.In that I have a maven project.I need to deploy the maven project as jar file in my tomcat's deploy path when I publish the tomcat. But when I publish the tomcat the maven project is deployed as folder where it contains class files,META_INF and WEB_INF folders.
How to pack the maven project as jar when I publish the tomcat?
I need to move the deployed jars from deploy path to lib folder of tomcat.
Please anyone suggest me how to get it done.
Thanks.

Make sure that the packaging tag in you pom.xml is set to war
<packaging>war</packaging>

Related

Deploy a Maven project with a generated webapp folder to Apache Tomcat

I am trying to build/deploy/run this maven/apache tomcat project LODE.
It's quite the first time I see this style of java web applications. I Followed the instructions provided on the website of this project. Started with maven clean install. Then an src folder was generated with a java folder (containing java classes) and a webapp folder (containing web resources) See the photo.
I am used to having a structure where a WebContent folder is there and a WAR file can generated.
.
How to deploy this project into apache tomcat and get it to run?
Here is what I tried:
I tried generating a war file by editing the build section in pom.xml file. However, it did not work.
I tried to add the project to Apache Tomcat 9 in my eclipse IDE. However, Tomcat does not believe that this is a web project, so I cannot add it (using right click on Tomcat -> Add/Remove).
I tried to take the generated webapp folder and deploy it to Tomcat (without eclipse IDE --> tomcat manager 'http://localhost:8081/manager/html' --> Deploy Directory) after creating WEB-INF/classes folder and moving the source qualified packages from the generated src folder to this folder. This did not work because the java classes were missing (cannot find the java class/package error).
LODE's README clearly says how to lunch the application. Well you don't see a war thus can't deploy on tomcat server.
I found two ways to make it work:
mvn clean jetty:run from the terminal. This will just work fine (runs on the jetty server).
For tomcat server, add <packaging>war</packaging> in the pom.xml to generate the war. Then either rename the generated war manually (for example, LODE-1.3-SNAPSHOT.war to lode.war) or use maven plugins to generate the war name. Now deploy this war to your tomcat server as usual.

Deployment Assembly configuration not working with mvn package

Below is the maven project structure I have in eclipse:
And the deployment assembly configuration is as shown below:
If I package the war with maven command (mvn clean package) and deploy the war on tomcat, the "resource" folder under WEB-INF is not getting created as I mentioned in deployment assembly configuration.
The server side folder structure created after deployment is as in the image below:
But, if I export the war from eclipse "Right click on project -> Export -> War file" and deploy in tomcat, it is creating the "resources" folder.
Kindly help me with this.
Thanks in advance!!!
If you look at the documentation for the maven-war-plugin here:
maven-war-plugin
You'll see that by default the contents of the "resources" folder gets placed inside the "classes" folder inside the WAR, so for a maven build you should find your resources in there.
Your Eclipse configuration is different, placing the resources in a separate "resources" folder under WEB-INF. Exporting from Eclipse will use this configuration rather than the maven configuration.
I would recommend changing your Eclipse configuration to match the maven one by changing the Deploy Path to "WEB-INF/classes"

Eclipse Luna, m2eclipse: empty dependency when dependency is a workspace project

I am working with Eclipse Luna.
I have a dynamic web project into my workspace: project-web. This project depends on another project into my workspace: project-lib.
So I get this scenario: project-web[:war] ---depends on--> project-lib[:jar]
I have checked "Resolve dependences from workspace projects" option into Properties -> Maven for the web project.
When I run "mvn clean package" for project-web Maven creates war file into the target directory but I have
an issue. If I explode the war file, I find an empty folder called "project-lib" into the WEB-INF/lib directory.
Since the library is empty I get some java exceptions on Tomcat startup.
How to create the war for project-web correctly?
Thanks in advance.
Enrico

How do I run a .war file with tomcat/maven?

I'm new to tomcat and maven configuration. I was given a single .war that has all the dependencies in it. I'm trying to figure out how to run the war file on a given port configured using a pom.xml file
I tried mvn tomcat:run-war where the .war file lives but it obviously needs a pom.xml How can I configure this to be able to run this?
If you have already compiled project with all the dependencies in it then it has nothing to do with maven (because it's a tool for building projects from source code).
In order to deploy a war file you should install standalone tomcat on your system and drop the war file into webapps directory in the tomcat installation home folder, then (re)start tomcat. Give it 20-30 seconds to launch and open http://localhost:8080/war-file-name-without-dot-war in your browser, if everything went right then you will see the deployed application.

Jars not copied to Tomcat's lib folder

I am doing some basic spring stuff and stuck at some point.
I am getting ClassNotFoundException whenever I deploy my application on Tomcat.
I observed that the jars are not copied to Tomcats lib folder.
When I copied the jars manually to Tomcats lib folder it works fine.
Please let me know if I am making any blunder.
PS - I am using Spring tool suite 2.6.0,Tomcat 6 and its a Maven project.
Thanks.
Go to "Project properties -> Deployment Assembly page".
This page describes how your application will be packaged for deployment or export. And added new source "Maven dependency".
From Deployment Assembly page, Click Add... button
Then select "Java Build Path Entries"
"Maven dependency" should be in the list
It solves the issue and all jar got copied to tomcat
If you define "war" packaging for your maven project then your dependent libraries should be automatically copied to the WEB-INF/lib directory of the created .war file by the Maven WAR plugin.
Put your jars in the WEB-INF/lib directory:
In Project properties -> Deployment Assembly you should have a Source of /web with a Deployment Path of /. If this is there then any jars in the web/WEB-INF/lib directory will get deployed and picked up by the class loader. Note that sub-directories will not be picked up though, so put your jars directly in the lib directory.
You run three command in cmd or bash(where your pom.xml file placed) to get jar file and copy it into your WEB-INF/lib folder
mvn compile
mvn package
mvn install
I find only this way for resolved my problem:
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

Categories

Resources