最新在项目中需要修改Maven创建的目录结构,需要添加一些source目录。熟悉maven的同学都知道,如果需要修改目录结构,可以通过修改pom.xml完成。具体如下:
- <build>
- <directory>${project.basedir}/target</directory>
- <outputDirectory>${project.build.directory}/classes</outputDirectory>
- <finalName>${project.artifactId}-${project.version}</finalName>
- <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
- <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
- <!-- TODO: MNG-3731 maven-plugin-tools-api < 2.4.4 expect this to be relative... -->
- <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
- <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
- <resources>
- <resource>
- <directory>${project.basedir}/src/main/resources</directory>
- </resource>
- </resources>
- <testResources>
- <testResource>
- <directory>${project.basedir}/src/test/resources</directory>
- </testResource>
- </testResources>
- </build>
Maven能够按照标准化的方式来编译、打包等就是依赖于安装目录apache-maven-2.2.1\lib下的maven-2.2.1-uber.jar下面的org\apache\maven\project\pom-4.0.0.xml来完成的。那如果需要增加源代码包和测试源代码包该如何办呢? 使用build-helper来完成。
build-helper是maven的plugin, 官网:http://mojo.codehaus.org/build-helper-maven-plugin/, 使用说明:http://mojo.codehaus.org/build-helper-maven-plugin/usage.html
(1)增加源代码包
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>build-helper-maven-plugin</artifactId>
- <version>1.5</version>
- <executions>
- <execution>
- <id>add-source</id>
- <phase>generate-sources</phase>
- <goals>
- <goal>add-source</goal>
- </goals>
- <configuration>
- <sources>
- <source>src/junit/java</source>
- <source>src/junit/resources</source>
- </sources>
- </configuration>
- </execution>
- </executions>
- </plugin>
(2)添加测试源代码包
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>build-helper-maven-plugin</artifactId>
- <version>1.5</version>
- <executions>
- <execution>
- <id>add-test-source</id>
- <phase>generate-test-sources</phase>
- <goals>
- <goal>add-test-source</goal>
- </goals>
- <configuration>
- <sources>
- <source>some directory</source>
- ...
- </sources>
- </configuration>
- </execution>
- </executions>
- </plugin>
添加测试源代码包后,通过mvn eclipse:eclipse生成eclipse工程之后,却无法在eclipse的源代码build path中看到,而添加源代码包是可以的。查阅了官网的bug库,该bug至今处于open状态。参考:https://issues.sonatype.org/browse/MNGECLIPSE-2387
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/578003,如需转载请自行联系原作者