Cannot find the xml
configuration file when maven is running
The XML
file was created under the java package, but the configuration file under the `resource` file was found when maven
was running, and the java package could not be imported.
Understand the target directory
In a maven project, a module mainly has two directories `src` and `target`, and a `pom.xml` configuration file
src
: This directory mainly stores the source code files of the projecttarget
: This directory is a directory generated after the project is compiled, and it mainly stores the compiled .class
files1. When building a Maven project, if there is no special configuration, Maven will find and process various types of files according to the standard directory structure
src/main/java和src/test/java
All *.java
files in these two directories will be compiled in the stages of comile
and test-comiple
respectively, and the compilation results are placed in the directories of target/classes
and targe/test-classes
respectively. But other files in these two directories will be ignored.
src/main/resouces和src/test/resources
The files in these two directories will also be copied to the target/classes
and target/test-classes
directories.
target/classes
By default, the packaging plugin will put all the contents in this directory into the jar package or war package.
Under normal circumstances, the resource files we use (various xml
, properites
, xsd
files, etc.) are placed under src/main/resources
. When using maven to package, maven can store these resource files Packaged into the corresponding jar or war.
Sometimes, such as the mapper.xml
file of mybatis, we are used to putting it together with `Mapper.java`, both under src/main/java
, so when using maven to package, you need to modify pom.xml
File, to pack the mapper.xml
file together into the jar or war, otherwise, these files will not be packaged. (maven thinks that src/main/java
is just the source code path of java).
In this project, I put the `UserMapper.xml` file in the `java/` directory. Therefore, this configuration file, after compilation, will not be placed under the target directory, which is an error. The reason to the xml file.
Put the UserMapper.xml
file in the test-classes
of the target/
directory
Here, the path registered by the mapper should be changed to a directory at the same level as the mybatis-config.xml
directory
Put the UserMapper.xml
file in the resource
directory, after compiling, the UseMapper
file will be automatically added under the target
directory
While keeping the original xml configuration file directory, add the UserMapper.xml
configuration file to the target
directory by changing the pom.xml
configuration file
Configure the resource of pom.xml
to package the xml into the mapper directory
<build>
<!-- Resource Directory -->
<resources>
<resource>
<!-- Set the main resource directory -->
<directory>src/main/java</directory>
<!-- maven default life cycle, the resources goal of the maven-resources-plugin plug-in is executed in the process-resources phase when processing resource files under the main resource category, only the resource types contained in the following configuration are processed -->
<includes>
<include>**/*.xml</include>
</includes>
<!-- maven default life cycle, the resources goal of the maven-resources-plugin plug-in is executed in the process-resources phase. When processing the resource files under the main resource category, the resource types included in the following configuration are not processed (reject the resources included in the following configuration) Type) -->
<excludes>
<exclude>**/*.yaml</exclude>
</excludes>
<!-- maven default life cycle, in the process-resources phase, execute the resources goal of the maven-resources-plugin plug-in when processing resource files under the main resource directory, specify the processed resource file output directory, the default is ${build.outputDirectory} specified Directory -->
<!--<targetPath>${build.outputDirectory}</targetPath> -->
<!-- maven default life cycle, process-resources phase executes the resources goal of the maven-resources-plugin plug-in when processing resource files under the main resource directory, whether to enable resource filtering on the main resource directory -->
<filtering>true</filtering>
</resource>
</resources>
</build>