我有一个java web应用程序项目(maven),我想在tomcat服务器上提供服务。我想从它创建一个.war文件,然后我可以将它放在webapps目录中。我知道从eclipse我可以通过导出来完成它但我需要通过终端来完成它。我也尝试了jar cvf example.war *
命令,但当我把war文件放在webapps目录下时,tomcat无法读取我的java应用程序。
有人可以指导我如何从终端创建一个warcat文件,tomcat可以读取它。
答案 0 :(得分:1)
在pom.xml中,添加以下行
<packaging>war</packaging> // Replace <packaging>jar</packaging> if jar packaging already present
然后转到项目的父目录并输入mvn package
。
/parent_directory/target/myproject.war
如果您想以http://localhost:8080
而不是http://localhost:8080/myproject
访问项目,请将myproject.war
重命名为ROOT.war
并将war文件粘贴到tomcat目录的webapps中。
答案 1 :(得分:0)
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 -
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
<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.