0
votes

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.

2
Hey Sandeep no its not because I have tried the jar commandclick
cd /to/your/folder/location; jar -cvf my_web_app.war * didn't worked for you?Sandeep Chatterjee
This is creating a war file but Tomcat cant recognize it when I place it in the webappsclick
Use mvn package on the directory that has pom.xmlRaphael Amoedo

2 Answers

1
votes

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.

0
votes

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

enter image description here

See: Introduction to Maven Archetypes

  • Once your project is created, navigate to the directory which has the pom.xml and use:

    mvn package

enter image description here

  • Once you are done with step 2, navigate to the target directory where you would find a MyWebApp.war generated.

  • Copy-paste MyWebApp.war to tomcat webapps directory and start the tomcat server(./startup.sh).

  • As you can see, we can now access the URL http://localhost:8080/MyWebApp/.

enter image description here

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.