简体   繁体   English

如何在不使用Eclipse的情况下从终端为我的Java Web应用程序项目创建War文件

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

I have a java web application project (maven) that I want to serve on a tomcat server. 我有一个要在雄猫服务器上使用的Java Web应用程序项目(Maven)。 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. 我想从中创建一个.war文件,然后将其放置在webapps目录中。我知道从eclipse中可以通过导出来完成它,但是我需要通过终端来完成。 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. 我也尝试过jar cvf example.war *命令,但是当我将war文件放在webapps目录中时,tomcat无法读取我的Java应用程序。 Can someone please guide me on how to create a war file from terminal that tomcat can read it. 有人可以指导我如何从tomcat可以读取的终端创建war文件。

In pom.xml, add the following line 在pom.xml中,添加以下行

<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 . 然后转到项目的父目录并输入mvn package war file will be generated in war文件将在

/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. 如果您要访问您的项目http://localhost:8080 ,而不是http://localhost:8080/myproject ,重命名myproject.warROOT.war并粘贴到tomcat目录中的webapps战争文件。

Steps: 脚步:

  • Navigate to an appropriate directory(let's say desktop) and use maven-archetype-webapp to start a simple webapp maven project. 导航到适当的目录(假设为桌面),然后使用maven-archetype-webapp启动一个简单的webapp maven项目。 For example: 例如:

    mvn archetype:generate -DgroupId=com.mywebapp.www -DartifactId=MyWebApp -DarchetypeArtifactId=maven-archetype-webapp mvn原型:generate -DgroupId = com.mywebapp.www -DartifactId = MyWebApp -DarchetypeArtifactId = maven-archetype-webapp

在此处输入图片说明

See: Introduction to Maven Archetypes 请参阅: Maven原型简介

  • Once your project is created, navigate to the directory which has the pom.xml and use: 创建项目后,导航至包含pom.xml的目录并使用:

    mvn package mvn包

在此处输入图片说明

  • Once you are done with step 2, navigate to the target directory where you would find a MyWebApp.war generated. 完成第2步后,导航到target目录,您将在其中找到生成的MyWebApp.war

  • Copy-paste MyWebApp.war to tomcat webapps directory and start the tomcat server( ./startup.sh ). MyWebApp.war复制并粘贴到tomcat webapps目录,然后启动tomcat服务器( ./startup.sh )。

  • As you can see, we can now access the URL http://localhost:8080/MyWebApp/ . 如您所见,我们现在可以访问URL http://localhost:8080/MyWebApp/

在此处输入图片说明

For your reference - 供你参考 -

index.jsp: index.jsp:

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

pom.xml: 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. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM