29.md 6.2 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12
# 如何使用 Maven 创建 Java WAR 文件

> 原文: [https://javatutorial.net/how-to-create-java-war-file-with-maven](https://javatutorial.net/how-to-create-java-war-file-with-maven)

在继续进行下一步之前,请确保已在系统上安装了 JDK 和 Maven。

![java-featured-image](img/e0db051dedc1179e7424b6d998a6a772.jpg)

如果您尚未安装 JDK,请点击此处的[](https://javatutorial.net/install-java-8-jdk-on-ubuntu)

如果您尚未安装 Maven,请点击此处的[](https://javatutorial.net/how-to-install-maven-on-windows-linux-and-mac)

W
wizardforcel 已提交
13
## 使用 Eclipse 生成 WAR 文件
W
init  
wizardforcel 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

步骤 1 –打开 Eclipse 并创建一个新的 Maven 项目(文件->新建->其他-> Maven 项目)![How to generate WAR file using Maven java example](img/e015b5975e479d17af96f6c852f62685.jpg)

步骤 2 –创建 Maven 项目后,在新窗口中单击“下一步”,如下所示:![How to generate WAR file using Maven java example](img/d45a478cce51758e82f390a444eaa532.jpg)

步骤 3 –选择 maven-archetype-webapp 并单击 Next,如下所示:![How to generate WAR file using Maven java example](img/1d50c47b3c02a31d78c682bc4c33eab6.jpg)

步骤 4 –输入详细信息,例如我的,然后单击完成

![How to generate WAR file using Maven java example](img/d3405c528e542ef87734e7f6ead65988.jpg)

您的 Maven 项目目录应类似于以下内容:

![How to generate WAR file using Maven java example](img/e2dd3cee1e9ed427a02a960868755faf.jpg)

并且 pom.xml 应该看起来像这样:

```java
<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>createwar</groupId>
  <artifactId>createwar</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>createwar 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>createwar</finalName>
  </build>
</project>

```

步骤 6 –将 pom.xml 替换为以下代码:

```java
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>CrunchifyTutorial</groupId>
	<artifactId>CrunchifyTutorial</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.4</version>
				<configuration>
					<warSourceDirectory>WebContent</warSourceDirectory>
					<failOnMissingWebXml>false</failOnMissingWebXml>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
		</dependency>
	</dependencies>
</project>
```

最重要的几行是:

```java
<packaging>war</packaging>
```

和:

```java
<artifactId>maven-compiler-plugin</artifactId>
```

这就是我们有效地将其转换为 WAR 的地方。

步骤 7 –右键点击 Project- &gt; Run As- &gt; Maven build…:![java maven install](img/286a29eb65c5c30381f15a2053adb3da.jpg)

步骤 8 –在“目标”部分中键入**全新安装**,然后单击运行,如下所示:![maven clean install java](img/861088aaf532e2ae1cb56f2957e4892c.jpg)

步骤 9 –您应该看到 BUILD SUCCESS,像这样:![Java maven clean install build success](img/619532c4a014cc880c1d07b561715edc.jpg)
恭喜! 您有您的.war 文件。
![java war file maven install](img/a78656f303a23f8d7f3f305bcbb6d817.jpg)

## 使用 CMD 生成 WAR 文件

步骤 1 –通过在我们安装了 Java 的地方添加编译器来修改 pom.xml 文件。

**pom.xml**

```java
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>craetewar</groupId>
  <artifactId>craetewar</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>craetewar</name>

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
   <dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
    </dependencies>

  <build>  
  <finalName>createwarexample</finalName>
   <plugins>  
       <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
			<configuration>
			<fork>true</fork>
			<executable>C:\Program Files\Java\jdk1.8.0_211\bin\javac.exe</executable>
			</configuration>
        </plugin>
    </plugins>  
   </build> 

</project>
```

**注意:**&lt;可执行文件&gt;是重要的部分。 您的 Java 版本可能有所不同,请确保牢记这一点并放置正确的文件夹路径。

第 2 步–在终端中导航到项目的文件夹路径,例如:
![maven war file install cmd](img/8f85c2389fcd7a6003c8149afd59721f.jpg)

步骤 3-运行 **mvn clean install** ,它将负责创建 war 文件:

![java maven clean install command cmd eclipse](img/b1806dbee2e98bfcad3f1c06de909dd8.jpg)

有我们的 WAR 文件:

![Java maven install war file cmd eclipse clean install build](img/ccbb472243ff045ace7419d27d5ee877.jpg)

而已! 我们已经成功使用 Eclipse 和 Terminal 生成了战争文件。

如果您有兴趣生成 JAR 文件,我已经有关于该主题的文章。 您可以通过单击此处的[来遵循它。](https://javatutorial.net/how-to-create-java-jar-file-with-maven)