Eclipse--maven项目打包成War包

禄奇希
2023-12-01

打包war包步骤

1. 修改pom.xml文件,添加几句话:

<packaging>war</packaging>

<!-- Tomcat外置。-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
	<scope>provided</scope>
</dependency>

<plugin>		
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>2.18.1</version>
	<configuration>
	 <skipTests>true</skipTests>
	 </configuration>
</plugin>

2. 右键debug as -->maven install
打包服务项目war包日志如下:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building springdemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springdemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ springdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory I:\baiduDownload\springdemo\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ springdemo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ springdemo ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:2.6:war (default-war) @ springdemo ---
[INFO] Packaging webapp
[INFO] Assembling webapp [springdemo] in [I:\baiduDownload\springdemo\target\springdemo-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [I:\baiduDownload\springdemo\src\main\webapp]
[INFO] Webapp assembled in [640 msecs]
[INFO] Building war: I:\baiduDownload\springdemo\target\springdemo-0.0.1-SNAPSHOT.war
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ springdemo ---
[INFO] Downloading: http://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.pom
[INFO] Downloaded: http://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.pom (11 KB at 7.0 KB/sec)
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.pom
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.pom (4 KB at 5.8 KB/sec)
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.pom
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.pom (4 KB at 5.0 KB/sec)
[INFO] Downloading: http://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.jar
[INFO] Downloaded: http://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.jar (228 KB at 163.5 KB/sec)
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.jar
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.jar (152 KB at 189.3 KB/sec)
[INFO] Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.jar
[INFO] Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.jar (234 KB at 341.7 KB/sec)
[INFO] Installing I:\baiduDownload\springdemo\target\springdemo-0.0.1-SNAPSHOT.war to C:\Users\zheng\.m2\repository\com\scdn\springdemo\0.0.1-SNAPSHOT\springdemo-0.0.1-SNAPSHOT.war
[INFO] Installing I:\baiduDownload\springdemo\pom.xml to C:\Users\zheng\.m2\repository\com\scdn\springdemo\0.0.1-SNAPSHOT\springdemo-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.406 s
[INFO] Finished at: 2019-01-20T17:12:11+08:00
[INFO] Final Memory: 23M/273M
[INFO] ------------------------------------------------------------------------

3. 打包好的war包放在target下。

4. 遇到的问题

1) 在MAVEN install 项目时 出现如下异常:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project spring-cloud-eureka-server-cluster: 
Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: The forked VM terminated without properly saying 
goodbye. VM crash or System.exit called?

可以看出是maven-surefire-plugin:2.18.1 插件问题,在网上寻找解决方案如下,再 install 一次 。

<plugin>
 
  <groupId>org.apache.maven.plugins</groupId>
 
  <artifactId>maven-surefire-plugin</artifactId>

   <version>2.18.1</version>
 
  <configuration>

      <skipTests>true</skipTests>
 
  </configuration>

</plugin>

2)在碰到maven install 发现报错

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project autotest_fchtgl: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

JDK路径问题,解决方法:

在eclipse中,选择window→preference→java→Installed JREs 点add添加你的jdk路径,选jdk而不是jre。然后右键maven项目,在maven中点击update project。

 类似资料: