How to create a war file for my java web application project from terminal without using eclipse

对着背影说爱祢 提交于 2019-12-12 03:39:18

问题


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.


回答1:


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:


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 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/.

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.



来源:https://stackoverflow.com/questions/32340190/how-to-create-a-war-file-for-my-java-web-application-project-from-terminal-witho

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!