Maven项目java目录下的配置文件不会被编译到项目中

  1. 问题:

maven的项目,是由maven来进行编译的。maven会将项目编译为以前的javaSE和javaEE的结构来运行。但是maven在编译项目的时候,不会讲java目录下的配置文件编译到项目中,也就是maven默认认为java目录下只有java代码,没有其他文件。这样造成在SSM项目中,mapper包中除了java代码以外,还有mapper.xml文件,maven是不会将mapper.xml

文件被编译到项目中的,造成项目运行失败,怎么办呢?

  1. 解决:

我们需要告诉maven在编译项目中java代码时,在java目录下除了java代码,还有配置文件,需要将配置文件也编译到

项目中使用。我们需要在项目的pom.xml文件中配置相关标签 即可

  1. 实现:

在项目的pom.xml文件的build标签下使用resources子标签来告诉maven需要将源码中的xml文件也进行编译:

<build>
<plugins>
<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
  <configuration>
    <port>7070</port><!--配置tomcat启动的端口号-->
    <path>/ty</path><!--配置项目的访问名称-->
  </configuration>
</plugin>
</plugins>
  <!--告诉maven将项目源码中的xml文件也进行编译,并放到编译目录中-->
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
      <filtering>true</filtering>
    </resource>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

  

原文地址:https://www.cnblogs.com/vincentmax/p/14339825.html