構建Maven項目的時候,若是沒有進行特殊的配置,Maven會按照標準的目錄結構查找和處理各類類型文件。html
src/main/java和src/test/java java
這兩個目錄中的全部*.java文件會分別在comile和test-comiple階段被編譯,編譯結果分別放到了target/classes和targe/test-classes目錄中,可是這兩個目錄中的其餘文件都會被忽略掉。web
src/main/resouces和src/test/resourcesapache
這兩個目錄中的文件也會分別被複制到target/classes和target/test-classes目錄中。mybatis
target/classesapp
打包插件默認會把這個目錄中的全部內容打入到jar包或者war包中。webapp
資源文件是Java代碼中要使用的文件。代碼在執行的時候會到指定位置去查找這些文件。前面已經說了Maven默認的處理方式,可是有時候咱們須要進行自定義的配置。maven
有時候有些配置文件一般與.java文件一塊兒放在src/main/java目錄(如mybatis或hibernate的表映射文件)。有的時候還但願把其餘目錄中的資源也複製到classes目錄中。這些狀況下就須要在Pom.xml文件中修改配置了。post
能夠有兩種方法:測試
<build> ....... <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> </excludes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> ...... </build>
<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>
另外一個插件也能完成相同的功能
<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>
打包時target/classes目錄中的資源文件會和class字節碼一塊兒被打進jar包或war包中。有時候默認的狀況不能徹底知足需求,如target/classes目錄中的一些文件不但願打入jar包中,就須要額外配置maven-jar-plugin插件。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <excludes> <exclude>*.properties</exclude> </excludes> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>xxxxxx.ConsoleLauncher</mainClass> </manifest> </archive> </configuration> </plugin>
轉:http://www.cnblogs.com/pixy/p/4798089.html
參考資料
http://bglmmz.iteye.com/blog/2063856