当前位置: 代码迷 >> Web前端 >> maven中配备非标准的java source resource webxml的方法
  详细解决方案

maven中配备非标准的java source resource webxml的方法

热度:641   发布时间:2012-12-28 10:29:05.0
maven中配置非标准的java source resource webxml的方法
最近在做一个非标准目录的项目,但想使用maven管理,标准的java source路径为src/main/java
resource路径为src/main/resource.而这个项目现在的路径为src/java和src/conf.因此,在maven的pom中加上如下配置即可:
<build>
......
           <sourceDirectory>src/java</sourceDirectory>
	  <testSourceDirectory>src/java.test</testSourceDirectory>
	   	<resources>
			<resource>
				<directory>src/conf</directory>
				<includes>
					<include>**/*</include>
				</includes>
			</resource>
		
			<resource>
				<directory>src/java</directory>
				<includes>
					<include>**/*</include>
				</includes>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
		</resources>
		<testResources>
			<testResource>
				<directory>src/conf.test</directory>
				<includes>
					<include>**/*</include>
				</includes>
			</testResource>
			<testResource>
					<directory>src/java.test</directory>
					<includes>
						<include>**/*</include>
					</includes>
					<excludes>
						<exclude>**/*.java</exclude>
					</excludes>
			</testResource>
		</testResources>	
......

</build>

这样maven打包时,就会编绎在src/java文件夹下的java文件,并将src/conf下的配置文件放到target的classes下;

如果还有其它在打包时,自动生成的java代码,或是多个java文件目录,那可以考虑使用专门做这个工作的plugin,配置如下:
<plugins>
	......			
           <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/java</source>
                                   <source>自定义的folder</source>
				</sources>
			 </configuration>
		  </execution>
		   <execution>
			<id>add-test-source</id>
			<phase>generate-sources</phase>
			<goals>
			    <goal>add-test-source</goal>
			 </goals>
			<configuration>
			    <sources>
                                <source>${basedir}/src/java.test</source>
                                 <source>自定义的folder</source>
			    </sources>
			 </configuration>
			</execution>
		   </executions>
	</plugin>
......
</plugins>


另外,如果web project中的webapp资源也不在标准的maven-war-plugin默认路径:${basedir}/src/main/webapp下,那么需要加入如下配置
<plugins>
......
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-war-plugin</artifactId>
		<version>2.1-beta-1</version>
		<configuration>
          <!-- 这里是你的webapp目录 -->	   
                    <warSourceDirectory>${basedir}/src/webroot</warSourceDirectory>
		<archive>
                   <addMavenDescriptor>false</addMavenDescriptor>
		   <manifestEntries>
                        <timestamp>${timestamp}</timestamp>
		   </manifestEntries>
		</archive>
		</configuration>
	</plugin>
......
</plugins>
  相关解决方案