maven 打包時mapper.xml打不進去問題

來源:互聯網
上載者:User

標籤:

首先,來看下MAVENx項目標準的目錄結構:

一般情況下,我們用到的資源檔(各種xml,properites,xsd檔案等)都放在src/main/resources下面,利用maven打包時,maven能把這些資源檔打包到相應的jar或者war裡。

 

有時候,比如mybatis的mapper.xml檔案,我們習慣把它和Mapper.java放一起,都在src/main/java下面,這樣利用maven打包時,就需要修改pom.xml檔案,來把mapper.xml檔案一起打包進jar或者war裡了,否則,這些檔案不會被打包的。(maven認為src/main/java只是java的原始碼路徑)。網路上有很多方法,我大概試了下,幾種方法都可以,可以任選一種即可。

 

方法1,其中**/*這樣的寫法,是為了保證各級子目錄下的資源檔被打包。

xml代碼

<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>        <resource>            <directory>src/main/java</directory>            <includes>                <include>**/*.properties</include>                <include>**/*.xml</include>                <include>**/*.tld</include>            </includes>            <filtering>false</filtering>        </resource>    </resources></build>

方法2,利用build-helper-maven-plugin外掛程式

<build>    ...    </plugins>        ...        <!--        此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>           ...    </plugins>         ...</build>

方法3,利用maven-resources-plugin外掛程式

<build>    ...    </plugins>        ...        <!--        此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>           ...    </plugins>         ...</build>

轉載於:http://bglmmz.iteye.com/blog/2063856

maven 打包時mapper.xml打不進去問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.