java Maven:从构建中排除测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14717743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven: Excluding tests from build
提问by Brian
I have some classes I'm using as tests in my src/test/java folder of my project. When I run maven using the standard maven compile plugin. Those items are compiled into .class files and are included in the jar where the compiled code is packaged.
我在项目的 src/test/java 文件夹中有一些用作测试的类。当我使用标准的 maven 编译插件运行 maven 时。这些项目被编译成 .class 文件,并包含在打包编译代码的 jar 中。
I've created these tests for myself to run within eclipse, prior to running maven and building my release. They are just sanity tests and should not be included in the build. I'd rather not put them in a seperate project, because, to me, they make sense here. How can I tell maven that I do not want it to compile/include the files in that directory?
在运行 maven 和构建我的版本之前,我已经为自己创建了这些测试以在 eclipse 中运行。它们只是健全性测试,不应包含在构建中。我宁愿不把它们放在一个单独的项目中,因为对我来说,它们在这里很有意义。我如何告诉 Maven 我不希望它编译/包含该目录中的文件?
I beleive the maven compiler plugin is generating the jar as follows:
我相信 Maven 编译器插件正在生成 jar,如下所示:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
采纳答案by Anders R. Bystrup
I understand from your comment on this answerthat the "tests" aren't unit tests, but just ordinary classes that you want excluded from the final artifact? As such, your best option is to make use of the <exclude>
tag with the maven-jar-pluginas follows:
我从你对这个答案的评论中了解到,“测试”不是单元测试,而只是你想从最终工件中排除的普通类?因此,您最好的选择是使用<exclude>
带有maven-jar-plugin的标签,如下所示:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>**/yoursortoftestpackage/YourSortOfTestClass*</exclude>
</excludes>
</configuration>
</plugin>
Hope that helps!
希望有帮助!
Cheers,
干杯,
回答by basiljames
Put your custom test files in a folder src/localtest/java
i think, maven will not know it is there.
将您的自定义测试文件放在src/localtest/java
我认为的文件夹中,maven 不会知道它在那里。
回答by Y.G.
Use the option of -Dmaven.test.skip
will skip both compilation and execution of the tests. ( use -DskipTests
just skips the test execution, the tests are still compiled)
使用选项-Dmaven.test.skip
将跳过测试的编译和执行。( use-DskipTests
只是跳过测试执行,测试仍然编译)
mvn clean install -Dmaven.test.skip
mvn clean install -Dmaven.test.skip
回答by Jane Doh
Annotation for junit @Ignore
will ignore the class while building in maven.
@Ignore
在 maven 中构建时,junit 的注释 将忽略该类。
Edit:
编辑:
You can configure maven-surefire-plugin.
<project>
<build>
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>