我刚刚开始在我的项目中使用 Maven。我所有的生产代码当然都在 main 目录中。当我将单元测试添加到我的 tests 目录时,有没有办法将 main
目录与我的 tests
目录同步?
例如,假设我添加了一个新包 org.bio.mynewpackage
。我必须进入我的 main 文件夹并添加相同的包名称...这很烦人。
有什么想法吗?
最佳答案
这是一个嵌入 GMaven plugin 中的 Groovy 脚本执行。它完全符合您的要求。
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>mirror-folder-structure</id>
<phase>generate-test-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
<![CDATA[
static void createShadow(File base, File shadow){
if(base.exists()&&!shadow.exists())shadow.mkdirs();
base.eachDir { createShadow(it, new File(shadow, it.name))};
}
createShadow(pom.build.sourceDirectory,pom.build.testSourceDirectory);
]]>
</source>
</configuration>
</execution>
</executions>
</plugin>
问题是:它不会自动运行。我已将它绑定(bind)到 generate-test-sources
阶段,但您可以选择任何其他阶段。但是,您必须手动执行该阶段,例如mvn 生成测试源
。
但是,如果您考虑将 Eclipse 与 m2eclipse 插件一起使用,m2eclipse 可让您定义在您保存文件时自动运行的生命周期阶段,这样会更容易。
关于java - Maven - 将 "main"文件夹与 "tests"文件夹同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4302132/