Home  >  Article  >  Java  >  Problems encountered when maven integrates the framework with mybaits

Problems encountered when maven integrates the framework with mybaits

巴扎黑
巴扎黑Original
2017-06-26 10:39:291803browse

First, let’s take a look at the standard directory structure of the MAVENx project:

Generally, the resource files we use (various xml , properties, xsd files, etc.) are placed under src/main/resources. When using maven for packaging, maven can package these resource files into the corresponding jar or war.

Sometimes, for example, the mapper.xml file of mybatis, we are used to putting it together with Mapper.java, both under src/main/java. In this way, when using maven to package, You need to modify the pom.xml file to package the mapper.xml file into a jar or war. Otherwise, these files will not be packaged. (Maven thinks that src/main/java is just the source code path of java). There are many methods on the Internet. I tried it out, and several methods are available. You can choose any one.

Method 1, where **/* is written in order to ensure that resource files in subdirectories at all levels are packaged.

xml code

Problems encountered when maven integrates the framework with mybaits
<build>
    <finalname>test</finalname>
    <!--
    这样也可以把所有的xml文件,打包到相应位置。
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.tld</include>
            </includes>
            <filtering>false</filtering><--这里是false,用true会报 数据库连接 错误-->
        
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.tld</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    
</build>
Problems encountered when maven integrates the framework with mybaits

Method 2, using the build-helper-maven-plugin plugin

Problems encountered when maven integrates the framework with mybaits##
<build>
    ...
    
        ...
        <!--
        此plugin可以用
        利用此plugin,把源代码中的xml文件,
        打包到相应位置,这里主要是为了打包Mybatis的mapper.xml文件 
        -->
        <plugin>
            <groupid>org.codehaus.mojo</groupid>
            <artifactid>build-helper-maven-plugin</artifactid>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/java</directory>
                                <includes>
                                    <include>**/*.xml</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>   
        ...
         
    ...
</build>
Problems encountered when maven integrates the framework with mybaits
Method 3, using the maven-resources-plugin plug-in

Problems encountered when maven integrates the framework with mybaits##
<build>
    ...
    
        ...
        <!--
        此plugin可以用
        利用此plugin,把源代码中的xml文件,打包到相应位置,
        这里主要是为了打包Mybatis的mapper.xml文件 
        -->
        <plugin>
            <artifactid>maven-resources-plugin</artifactid>
            <version>2.5</version>
            <executions>
                <execution>
                    <id>copy-xmls</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputdirectory>${basedir}/target/classes</outputdirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/java</directory>
                                <includes>
                                    <include>**/*.xml</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>   
        ...
         
    ...
</build>
Problems encountered when maven integrates the framework with mybaits

The above is the detailed content of Problems encountered when maven integrates the framework with mybaits. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn