整理下maven搭建项目遇到的问题

一、src下的.xml没有拷贝到classpath下

问题:IDEA运行maven项目,启动后调用功能,mybatis模块在查询数据时发生了异常,org.apache.ibatis.binding.BindingException,查询到是所有的mapper.xml没有编译并拷贝到calsses目录,未进行打包编译。
当前的配置:mapper.xml和mapper接口同名,配置在同一包下,在applicationContext.xml中配置了mapper接口扫描器MapperScannerConfigurer
原因:在IDEA中运行maven项目,对于src.java包下的xml文件默认不会编译到classes中,只会编译拷贝resources目录
解决:
方式一:把dao包的mapper.xml文件全部配置到resources目录
比如,在使用GenerateConfig配置map的xml生成器时,指定到resources中

<sqlMapGenerator targetPackage="cn.xy.crm.mapper" targetProject="src/main/resources">  ....</sqlMapGenerator>

注意设置好resources的content root,maven工具默认在编译的时候,会将resources文件夹中的资源文件一块打包进classes目录中。如果已经设置了content root还是没有编译打包,在Module的Dependences中将resources目录以claess的类别添加到依赖中。
方式二:配置pom.xml
Maven提供了一致的项目目录配置(源文件夹、资源文件夹等),在自动构建项目时, Maven会按照这个配置来执行操作(编译源文件、拷贝资源文件),Maven 默认的源文件夹及资源文件夹的配置代码如下:

<build>  
   <sourceDirectory>src/main/java</sourceDirectory >  
   <testSourceDirectory>src/test/java</testSourceDirectory >  
   <resources>  
       <resource>  
          <directory>src/main/resources</directory>  
       </resource>  
   </resources>  
   <testResources>  
       <testResource>  
          <directory>src/test/resources</directory>  
       </testResource>  
   </testResources>  
</build>

想解决这个问题,按照下面这样配置:

<!--这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里。-->
<resources>
	 <resource>
		<!-- 资源文件的路径 -->
		<directory>src/main/resources</directory>
		<!-- 需要编译打包的文件类型 -->
		<includes>
		   <include>**/*.properties</include>
		   <include>**/*.xml</include>
		   <include>**/*.tld</include>
		</includes>
		<filtering>false</filtering>
	 </resource>
	 <resource>
		<directory>src/main/java</directory>
		<excludes>
			<!-- 表示除了*.java,全部编译打包 -->
		   <exclude>**/*.java</exclude>
		</excludes>
	 </resource>
  </resources>

另外,被编译拷贝到classes的mapper.xml和mapper接口不在一个包下或者名字不同,就需要在spring配置文件中的sqlSessionFactory中配置,mapper接口扫描器只能扫描到同包下的同名xml

<property name="mapperLocations" value="classpath:cn/xy/crm/mapper/*Mapper.xml"/>

针对以上这种异常,通常的检查方法是:
出现这个错误时,按以下步骤检查一般就会解决问题:
1:检查xml文件所在package名称是否和Mapper interface所在的包名一一对应;
2:检查xml的namespace是否和xml文件的package名称一一对应;
3:检查方法名称是否对应;
4:去除xml文件中的中文注释;
5:随意在xml文件中加一个空格或者空行然后保存。

二、

问题:使用Maven插件启动Tomcat,在pom.xml配置了虚拟目录用来存放图片,报错:Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run (default-cli) on project engineer_center_web: Execution default-cli of goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run failed: addChild: Child name '/file' is not unique
配置:

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
  <configuration>
      <server>tomcat7</server>
      <!-- tomcat启动的端口 -->
      <port>80</port>
      <!-- 应用的上下文路径 -->
      <path>/</path>
      <staticContextPath>/pic</staticContextPath>
      <staticContextDocbase>D:/Resource/Picture</staticContextDocbase>
      <uriEncoding>utf-8</uriEncoding>
  </configuration>
</plugin>

原因:tomcat插件内部的问题,在pom.xml中配置tomcat插件,使用/[file] 这个配置后,启动时tomcat会自动创建两个“/[file]”,所以就报错child名字不唯一
解决:替换掉Maven仓库中的tomcat的插件jar,下载地址为:https://github.com/fzz1wzy/fzz-tools 或者 http://islet.ys168.com/ AboutCode区

三 Maven编译自动执行单元测试

https://www.cnblogs.com/kingstarer/p/13977933.html
https://www.cnblogs.com/qyf404/p/5013694.html

posted @ 2022-02-26 10:23  1131012  阅读(117)  评论(0)    收藏  举报