I have a java web application project (maven) that I want to serve on a tomcat server. I want to create a .war file from it so then I can place it in the webapps directory.I know that from eclipse I can to do it by the export but I need to do it through terminal. also I have tried the jar cvf example.war *
command but when I put the war file inside the webapps directory tomcat cant read my java application.
Can someone please guide me on how to create a war file from terminal that tomcat can read it.

- 447
- 1
- 7
- 25
-
Possible duplicate: http://stackoverflow.com/questions/29245567/how-to-create-a-war-file-using-the-commandline – Sandeep Chatterjee Sep 01 '15 at 20:20
-
1Hey Sandeep no its not because I have tried the jar command – click Sep 01 '15 at 20:22
-
`cd /to/your/folder/location`; `jar -cvf my_web_app.war *` didn't worked for you? – Sandeep Chatterjee Sep 01 '15 at 20:25
-
This is creating a war file but Tomcat cant recognize it when I place it in the webapps – click Sep 01 '15 at 20:31
-
Use `mvn package` on the directory that has `pom.xml` – Raphael Amoedo Sep 01 '15 at 20:40
-
hey Raphael this created a jar file for me. I have added the `
war ` in my pom.xml but then it complain that the webxml attribute is required – click Sep 01 '15 at 20:55
2 Answers
In pom.xml, add the following line
<packaging>war</packaging> // Replace <packaging>jar</packaging> if jar packaging already present
And then go to the parent directory of the project and enter mvn package
. war file will be generated in
/parent_directory/target/myproject.war
If you want to access your project as http://localhost:8080
rather than http://localhost:8080/myproject
, rename myproject.war
to ROOT.war
and paste the war file in webapps of tomcat directory.

- 2,562
- 5
- 33
- 62
-
This was the solution that I came up with. Also if anyone else get the error about the web.xml you should add these attributes to your pom.xml. `
org.apache.maven.plugins maven-war-plugin ${basedir}/WebContent/WEB-INF/web.xml
Steps:
Navigate to an appropriate directory(let's say desktop) and use
maven-archetype-webapp
to start a simple webapp maven project. For example:mvn archetype:generate -DgroupId=com.mywebapp.www -DartifactId=MyWebApp -DarchetypeArtifactId=maven-archetype-webapp
See: Introduction to Maven Archetypes
Once your project is created, navigate to the directory which has the
pom.xml
and use:mvn package
Once you are done with step 2, navigate to the
target
directory where you would find aMyWebApp.war
generated.Copy-paste
MyWebApp.war
to tomcatwebapps
directory and start the tomcat server(./startup.sh
).As you can see, we can now access the URL
http://localhost:8080/MyWebApp/
.
For your reference -
index.jsp:
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mywebapp.www</groupId>
<artifactId>MyWebApp</artifactId>
<packaging>war</packaging>
<version>1</version>
<name>MyWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>MyWebApp</finalName>
</build>
</project>
Hope that helps.

- 3,220
- 9
- 31
- 47