http://gaolixu.iteye.com/blog/571216

1. 配置项目的pom文件(Maven配置文件)

1.1 Maven 默认的源文件夹及资源文件夹配置

 

Maven 默认的源文件夹及资源文件夹的配置代码如下:

Xml代码 build-helper-maven-plugin 配置多 source resource 文件-编程之家 build-helper-maven-plugin 配置多 source resource 文件-编程之家build-helper-maven-plugin 配置多 source resource 文件-编程之家

  1. <build>  
  2.    …   
  3.    < sourceDirectory > src/main/java </ sourceDirectory >  
  4.    < testSourceDirectory > src/test/java </ testSourceDirectory >  
  5.    < resources >  
  6.        < resource >  
  7.           < directory > src/main/resources </ directory >  
  8.        </ resource >  
  9.    </ resources >  
  10.    < testResources >  
  11.        < testResource >  
  12.           < directory > src/test/resources </ directory >  
  13.        </ testResource >  
  14.    </ testResources >  
  15.    …   
  16. </build>  
    <build>       ...       < sourceDirectory > src/main/java </ sourceDirectory >       < testSourceDirectory > src/test/java </ testSourceDirectory >       < resources >           < resource >              < directory > src/main/resources </ directory >           </ resource >       </ resources >       < testResources >           < testResource >              < directory > src/test/resources </ directory >           </ testResource >       </ testResources >       ...    </build>

 

在 eclipse 下,项目源文件夹及资源文件夹展现成这样:

build-helper-maven-plugin 配置多 source resource 文件-编程之家

 

 

1.2 配置多个资源文件夹

从上面 Maven 的默认配置来看,源文件夹和测试源文件夹都只能配置一个,要配置多个源文件夹比较困难。这里我们先展示如何配置多个资源文件夹。

有人会认为,在 eclipse 下将这些文件夹都设置为源文件夹不就 OK 了吗?其实不然, Maven 在构建的时候并不会去读取任何 IDE 的配置信息,所以他不会知道我们在 eclipse 下指定的源文件夹,也不会将这些源文件夹下的代码编译打包。

配置多个资源文件夹的代码:

Xml代码 build-helper-maven-plugin 配置多 source resource 文件-编程之家 build-helper-maven-plugin 配置多 source resource 文件-编程之家build-helper-maven-plugin 配置多 source resource 文件-编程之家

  1. <build>  
  2.    …   
  3.     < resources >  
  4.        < resource >  
  5.           < directory > src/main/resources </ directory >  
  6.        </ resource >  
  7.        < resource >  
  8.           < directory > src/labs/resources </ directory >  
  9.        </ resource >  
  10.    </ resources >  
  11.    …   
  12. </build>  
    <build>       ...        < resources >           < resource >              < directory > src/main/resources </ directory >           </ resource >           < resource >              < directory > src/labs/resources </ directory >           </ resource >       </ resources >       ...    </build>

 

配置好以后, IDE ( eclipse )不会识别我们的配置,我们需要更新项目的配置:

build-helper-maven-plugin 配置多 source resource 文件-编程之家  

更新后, 在 eclipse 下项目源文件夹及资源文件夹展现成这样(多了一个资源文件夹):

build-helper-maven-plugin 配置多 source resource 文件-编程之家  

 

 

1.3 配置读取源文件夹里的资源文件

为了让 Maven 能够从源文件夹下读取资源文件(或将所有资源文件配置到源文件夹下),我们可以这样配置:

Xml代码 build-helper-maven-plugin 配置多 source resource 文件-编程之家 build-helper-maven-plugin 配置多 source resource 文件-编程之家build-helper-maven-plugin 配置多 source resource 文件-编程之家

  1. <build>  
  2.    …   
  3.    < resources >  
  4.        < resource >  
  5.           < directory > src/main/resources </ directory >  
  6.        </ resource >  
  7.        < resource >  
  8.           < directory > src/labs/resources </ directory >  
  9.        </ resource >  
  10.        < resource >  
  11.           < directory > src/main/java </ directory >  
  12.           < includes >  
  13.               < include > **/*.hbm.xml </ include >  
  14.           </ includes >  
  15.        </ resource >  
  16.        < resource >  
  17.           < directory > src/labs/java </ directory >  
  18.           < includes >  
  19.               < include > **/*.hbm.xml </ include >  
  20.           </ includes >  
  21.        </ resource >  
  22.    </ resources >  
  23.    …   
  24. </build>  
    <build>       ...       < resources >           < resource >              < directory > src/main/resources </ directory >           </ resource >           < resource >              < directory > src/labs/resources </ directory >           </ resource >           < resource >              < directory > src/main/java </ directory >              < includes >                  < include > **/*.hbm.xml </ include >              </ includes >           </ resource >           < resource >              < directory > src/labs/java </ directory >              < includes >                  < include > **/*.hbm.xml </ include >              </ includes >           </ resource >       </ resources >       ...    </build>

 

这样,不仅 src/main/resources 和 src/labs/resources 这两个目录下的文件会作为资源文件被打包, src/main/java 和 src/labs/java 目录下的所有 .hbm.xml 文件也都会作为资源文件被打包。否则, Maven 构建时不会打包这两个目录下的 .hbm.xml 文件,导致运行时因为找不到资源文件而抛异常。

 

 

1.4 配置多个源文件夹

前面讲过, Maven 默认只支持一个源文件夹,为了给项目配置多个源文件夹,我们需要用到一个 Maven 插件,目的是在 Maven 构建生命周期内为项目添加源文件夹:

Xml代码 build-helper-maven-plugin 配置多 source resource 文件-编程之家 build-helper-maven-plugin 配置多 source resource 文件-编程之家build-helper-maven-plugin 配置多 source resource 文件-编程之家

  1. <build>  
  2.     …   
  3.     <plugins>  
  4.         …    
  5.         <!– build-helpermaven-plugin, 设置多个源文件夹 –>  
  6.         <plugin>  
  7.             <groupId>org.codehaus.mojo</groupId>  
  8.             <artifactId>build-helper-maven-plugin</artifactId>  
  9.             <version>1.4</version>  
  10.             <executions>  
  11.                 <execution>  
  12.                     <id>add-source</id>  
  13.                     <phase>generate-sources</phase>  
  14.                     <goals>  
  15.                         <goal>add-source</goal>  
  16.                     </goals>  
  17.                     <configuration>  
  18.                         <sources>  
  19.                             <source>${basedir}/src/labs/java</source>  
  20.                             <!– 我们可以通过在这里添加多个source节点,来添加任意多个源文件夹 –>  
  21.                         </sources>  
  22.                     </configuration>  
  23.                 </execution>  
	<build>		...		<plugins>			... 			<!-- build-helper-maven-plugin, 设置多个源文件夹 -->			<plugin>				<groupId>org.codehaus.mojo</groupId>				<artifactId>build-helper-maven-plugin</artifactId>				<version>1.4</version>				<executions>					<execution>						<id>add-source</id>						<phase>generate-sources</phase>						<goals>							<goal>add-source</goal>						</goals>						<configuration>							<sources>								<source>${basedir}/src/labs/java</source>								<!-- 我们可以通过在这里添加多个source节点,来添加任意多个源文件夹 -->							</sources>						</configuration>					</execution>

Xml代码 build-helper-maven-plugin 配置多 source resource 文件-编程之家 build-helper-maven-plugin 配置多 source resource 文件-编程之家build-helper-maven-plugin 配置多 source resource 文件-编程之家

  1.   

Xml代码 build-helper-maven-plugin 配置多 source resource 文件-编程之家 build-helper-maven-plugin 配置多 source resource 文件-编程之家build-helper-maven-plugin 配置多 source resource 文件-编程之家

  1. 还可以加resources:                                    <execution>  
  2.                         <id>add-resource</id>  
  3.                         <phase>generate-sources</phase>  
  4.                         <goals>  
  5.                             <goal>add-test-resource</goal>  
  6.                         </goals>  
  7.                         <configuration>  
  8.                             <resources>  
  9.                                 < resource >  
  10.                                                                                                                      < directory >${basedir} src/labs/resource </ directory >  
  11.                                                                                                                   < /resource >  
  12.                                                                                                           </resources>  
  13.                         </configuration>  
  14.                     </execution>  
  15.   
  16.                 </executions>  
  17.             </plugin>  
  18.             …    
  19.         </plugins>  
  20.         …    
  21.     </build>  
还可以加resources:                                    <execution>						<id>add-resource</id>						<phase>generate-sources</phase>						<goals>							<goal>add-test-resource</goal>						</goals>						<configuration>							<resources>								< resource >                                                                                                                     < directory >${basedir} src/labs/resource </ directory >                                                                                                                  < /resource >                                                                                                          </resources>						</configuration>					</execution>				</executions>			</plugin>			... 		</plugins>		... 	</build>

更新项目配置(操作同2.2)后,在eclipse下项目源文件夹及资源文件夹展现成这样(多了一个源文件夹):

build-helper-maven-plugin 配置多 source resource 文件-编程之家

 

 

——————————————-

http://mojo.codehaus.org/build-helper-maven-plugin/usage.html

http://mojo.codehaus.org/build-helper-maven-plugin/

Maven默认只允许指定一个主Java代码目录和一个测试Java代码目录,虽然这其实是个应当尽量遵守的约定,但偶尔你还是会希望能够指定多个源码目录(例如为了应对遗留项目),build-helper-maven-plugin的add-source目标就是服务于这个目的,通常它被绑定到默认生命周期的generate-sources阶段以添加额外的源码目录。需要强调的是,这种做法还是不推荐的,因为它破坏了 Maven的约定,而且可能会遇到其他严格遵守约定的插件工具无法正确识别额外的源码目录。

build-helper-maven-plugin的另一个非常有用的目标是attach-artifact,使用该目标你可以以classifier的形式选取部分项目文件生成附属构件,并同时install到本地仓库,也可以deploy到远程仓库。

http://mojo.codehaus.org/build-helper-maven-plugin/usage.html