【maven】之打包war依赖子项目jar

时间:2023-03-09 00:48:16
【maven】之打包war依赖子项目jar

比如

p-common

p-core

p-dao

p-service

p-web

service项目依赖dao,dao依赖core和common,web依赖service

在使用maven tomcat7:run直接运行web的时候,我们发现maven默认只添加service,对于service依赖的dao以及传递依赖的dao,common,core都没有添加到项目中

这就会产生ClassNotFound异常!

解决这个问题有两种方法

1、在web的pom中将依赖的jar重新引入一遍,如下

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
</dependency>

但是这种明显违背maven设计初衷,并且多出大量无用配置

2、使用maven插件

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.0.0</version>
</dependency>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

关于 maven-assembly-plugin 插件的说明

http://maven.apache.org/components/plugins/maven-assembly-plugin/

1、可以自定义打包配置文件

2、可以将jar打包成独立运行jar

....