通常在项目中都会使用maven进行多模块管理,默认被依赖的模块都会以jar包形式被引用。
然而在J2EE项目中,当使用了Spring的自动扫描配置时,jar包形式的依赖class将不能被自动装配:<context:component-scan base-package="com.xxx.xxx" /> 。

例如,存在如下结构的maven多模块项目:
--test-root
   --test-account(账户模块)
   --test-report(报表模块)
   --test-web(页面模块)

test-web模块同时依赖了test-account,test-report,所有模块的包名前缀都相同,便于在test-web配置根据包名自动扫描装配bean。
默认情况下,test-account和test-report两个模块都会以jar包的形式添加到test-web/WEB-INF/lib目录下。
但是,此时一旦在test-web模块中通过自动注入bean的方式引用test-account和test-report中的组件,将会报java.lang.NullPointerException异常。
也就是说,test-account和test-report中的组件并没有被自动注入,这是因为test-account和test-report中的组件并没有被spring自动扫描到并进行装配。

而要解决这个问题,必须将被依赖模块中的组件class文件打包到test-web/WEB-INF/classes目录中,即:打包时需要将被依赖模块的class文件copy到指定位置。
通过插件maven-dependency-plugin可以解决此问题,详见:https://maven.apache.org/plugins/maven-dependency-plugin/

test-web模块pom.xml配置案例如下:

 <build>
<plugins>
<!-- 将依赖模块的jar包文件提取出来放到指定位置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.test</groupId>
<artifactId>test-account</artifactId>
<version>1.0.0</version>
<type>jar</type>
<includes>**/*.class</includes>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>com.test</groupId>
<artifactId>test-report</artifactId>
<version>1.0.0</version>
<type>jar</type>
<includes>**/*.class</includes>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugins>
</build>

maven打包子模块中的class文件的更多相关文章

  1. 【Maven】maven打包生成可执行jar文件

    http://blog.csdn.net/u013177446/article/details/53944424 ******************************************* ...

  2. Maven打包排除不需要的文件。

    pom.xml <!-- package打包排除掉一些配置文件 --> <plugin> <groupId>org.apache.maven.plugins< ...

  3. 用maven打包java项目的pom文件配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  4. 清理maven本地库中的lastUpdated文件

    通过CMD命令窗口进入响应的文件夹下 输入指令 for /r %i in (*.lastUpdated) do del %i

  5. spring-boot子模块打包的jar中去掉BOOT-INF文件夹

    1.spring-boot maven打包,一般pom.xml文件里会加 <plugin> <groupId>org.springframework.boot</grou ...

  6. maven打包可执行jar文件运行报错

    起因 项目中同时依赖了Spring和MyBatis,并使用mybatis-spring集成MyBatis和Spring. 使用maven打包为可执行jar文件运行,打包插件为:maven-shade- ...

  7. Maven使用yuicompressor-maven-plugin打包压缩css、js文件

    最近项目想使用在maven打包的时间压缩js,css文件,采用yuicompressor-maven-plugin插件进行压缩,但只是压缩减小大小,提高请求速度,并没有对js进行混淆.下面就写一下这个 ...

  8. 【Maven学习】Maven打包生成普通jar包、可运行jar包、包含所有依赖的jar包

    http://blog.csdn.net/u013177446/article/details/54134394 ******************************************* ...

  9. maven打包springboot项目的插件配置概览

    jar包的术语背景: normal jar: 普通的jar,用于项目依赖引入,不能通过java -jar xx.jar执行,一般不包含其它依赖的jar包. fat jar: 也叫做uber jar,是 ...

随机推荐

  1. python模块_pcharm导入包的问题

    1.添加pip包 2.导入项目需要由内置包(library root)

  2. 二叉排序树类的: C++ 实现

    #include<iostream> using namespace std; template<class T> struct TreeNode { T element; T ...

  3. 【Mysql】—— MySQL存储引擎中的MyISAM和InnoDB区别详解

    在使用MySQL的过程中对MyISAM和InnoDB这两个概念存在了些疑问,到底两者引擎有何分别一直是存在我心中的疑问.为了解开这个谜题,搜寻了网络,找到了如下信息: MyISAM是MySQL的默认数 ...

  4. Layui_Tree模块遍历HDFS

    注:转载请署名 一.实体 package com.ebd.application.common.Base; import java.util.List; public class HDFSDir { ...

  5. 如何修改可运行Jar包,如何反编译Jar包

    将可运行Jar包,反编译成项目,修改代码,再次编译,打包. 需要工具:jd-gui.myeclipse 具体步骤: 1.使用jd-gui打开原始的Jar包,选择File-->Save All  ...

  6. 【 Gym - 101138D 】Strange Queries (莫队算法)

    BUPT2017 wintertraining(15) #4B Gym - 101138D 题意 a数组大小为n.(1 ≤ n ≤ 50 000) (1 ≤ q ≤ 50 000)(1 ≤ ai ≤  ...

  7. 【Luogu4630】【APIO2018】 Duathlon 铁人两项 (圆方树)

    Description ​ 给你一张\(~n~\)个点\(~m~\)条边的无向图,求有多少个三元组\(~(x, ~y, ~z)~\)满足存在一条从\(~x~\)到\(~z~\)并且经过\(~y~\)的 ...

  8. Leetcode 217.存在重复元素 By Python

    给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 示例 1: 输入: [1,2,3,1] 输出: true ...

  9. 自学Linux Shell16.1-函数概念

    点击返回 自学Linux命令行与Shell脚本之路 16.1-函数概念 编写比较复杂的shell脚本时,完成具体任务的代码可能需要重复使用.bash shell提供满足这种要求的特性.函数是被赋予名称 ...

  10. 牛客练习赛 小D的Lemon 解题报告

    小D的Lemon 题意 已知 \[ g(x)=\left\{\begin{matrix} 1&,x=1\\ \sum_{i=1}^qk_i&,otherwise \end{matrix ...