04. maven 三种项目打包方式 pom、jar、war 的区别(记一次 Spring 项目启动报错)

文章目录

  • [1. 记一次 Spring 项目启动报错](#1. 记一次 Spring 项目启动报错)
    • [1.1 现象](#1.1 现象)
    • [1.2 分析](#1.2 分析)
    • [1.3 过程复现](#1.3 过程复现)
  • [2. maven 项目三种打包方式的区别](#2. maven 项目三种打包方式的区别)

1. 记一次 Spring 项目启动报错

1.1 现象

我在项目下创建了一个子模块,然后又将该子模块移除,之后启动报错,如下:

java 复制代码
com.zk.App
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:257)
	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
	at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:638)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
	at com.zk.App.main(App.java:17)
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:333)
	... 13 more

Process finished with exit code 1

1.2 分析

主要报错信息如下,是说从类路径加载 applicationContext.xml 文件失败,因为类路径的 resources 目录下没有 applicationContext.xml 文件。

java 复制代码
IOException parsing XML document from class path resource [applicationContext.xml]; 

class path resource [applicationContext.xml] cannot be opened because it does not exist

出现原因:配置文件没有被编译到 target 文件夹下的 resources 目录下,因此找不到该名字的配置文件有可能是因为 maven 项目打包方式的问题

检查 pom 文件:

java 复制代码
    <groupId>org.example</groupId>
    <artifactId>Spring</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

将 pom 文件的 packaging 标签去掉或将标签内的 pom 改为 war 或者 jar 即可(需要重新编译 maven 项目)。

1.3 过程复现

新建 maven 项目,pom 文件中不会生成 <packaging> 标签。

新建子模块后,父模块的 pom 文件如下

新增了 <packaging> 标签和 <modules> 标签,<modules> 标签指向子模块。随后,移除子模块,启动项目时报前述错误。

检查发现,项目子模块依然存在且 pom 文件未发生变化。


右键子模块发现还需 delete 才可删除子模块。

delete 之后,发现 pom 文件报错。

子模块被删除,modules 标签也需要对应删掉。同时还需要修改打包方式或将 package 标签移除,重新编译后,才可正常启动项目。

2. maven 项目三种打包方式的区别

  • pom<packaging>pom</packaging> 一般用于父子工程或者聚合项目中,这个父 pom.xml 文件就是用来做 jar 包的版本控制。如果是父子工程或者聚合项目,那么必须指明这个工程的打包方式为 pom。
  • jar:将会打包成 jar 包使用,通常是开发时要引用通用类,打成 jar 包便于存放管理。
  • war:是做好一个 web 网站后,打成 war 包部署到服务器。
相关推荐
阿里巴巴P8资深技术专家1 小时前
基于 Spring AI 和 Redis 向量库的智能对话系统实践
人工智能·redis·spring
计算机学姐2 小时前
基于SpringBoot的校园资源共享系统【个性化推荐算法+数据可视化统计】
java·vue.js·spring boot·后端·mysql·spring·信息可视化
廋到被风吹走3 小时前
【Spring】Spring Boot 配置管理深度指南:Profile、类型安全与加密
spring boot·安全·spring
stillaliveQEJ3 小时前
【JavaEE】Spring IoC(二)
java·开发语言·spring
stillaliveQEJ4 小时前
【JavaEE】Spring IoC(一)
java·spring·java-ee
TaiKuLaHa4 小时前
Spring 循环依赖
java·后端·spring
IT 行者4 小时前
Spring Security 7 OAuth2 授权码分布式存储之Redis存储方案
redis·分布式·spring
计算机毕设指导65 小时前
基于微信小程序的钓鱼论坛系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
五阿哥永琪5 小时前
Spring Data Redis 实战避坑指南:从配置到缓存预热的全链路最佳实践
redis·spring·缓存
七夜zippoe5 小时前
数据库事务隔离级别与Spring传播行为深度解析
java·数据库·spring·mvcc·acid·myslq