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

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

1.1 现象

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

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 文件。

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 文件:

    <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 包部署到服务器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值