如何在不运行测试类的情况下将测试类打包到jar中? [英] How to package test classes into the jar without running them?

查看:284
本文介绍了如何在不运行测试类的情况下将测试类打包到jar中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将我的测试类包含在jar包中,但没有运行它们。经过一些谷歌搜索,我尝试了 mvn包-DskipTests ,但我的测试类根本没有添加到jar中。

I'm struggling with including my test classes into the jar package, but not running them. After some googling, I've tried mvn package -DskipTests, but my test classes are simply not added to the jar.

任何想法?

推荐答案

如果您遵循maven约定,那么您的测试类在 src /下测试/ JAVA
maven永远不会将测试源子目录的内容打包到工件中。

if youre following maven conventions then your test classes are under src/test/java. maven never packages the contents of the test sources subdirectory into the artifact.

你有(至少......)3个替代品:

you have (at least ...) 3 alternativs:

使用正常来源进行测试

如果你真的希望它们打包(为什么?)然后你应该将测试类放在 src / main / java 下,它们将被视为普通源,它们的编译类被打包到工件中(通常是* .jar)

if you REALLY want them packaged (why?) then you should place the test classes under src/main/java where they will be treated as normal source and their compiled classes packaged into the artifact (usually *.jar)

你不会遇到各种各样的问题。例如,你的工件将对junit有一个编译范围的依赖,这可能会干扰使用你的jar的其他模块。

you imght run into all sorts of issues doing this. for example your artifact will have a compile-scoped dependency on junit, which might interfere with other modules using your jar.

你可能还要配置运行的maven插件如果你这样做,测试要知道你的测试类(也就是说,如果你想要在构建过程中运行测试)。例如,surefire和failafe插件。

also, you might have to configure the maven plugins running the tests to be aware of your test classes if you do this (that is if you ever do want to run tests as part of your build). for example the surefire and failsafe plugins.

配置maven jar插件以构建测试jar

请参阅 maven jar插件文档。他们甚至有一个标题为如何创建一个包含测试类的jar的部分 - 这些说明将导致您的测试被打包到一个单独的jar文件中(这可能是一个更好的主意)。

see the maven jar plugin documentation. they even have a section titled "How to create a jar containing test classes" - the instructions there will cause your tests to be packaged into a separate jar file (which is probably a much better idea).

直接使用程序集插件创建jar

yuo can 禁用jar插件的默认执行,而是添加执行程序集插件到包阶段创建一个jar。你需要写一个汇编描述符为此(不像上面的链接那样复杂)。这将使yu完全控制生产的jar中包含的内容。最好先复制一个预定义的程序集

yuo could disable the default execution of the jar plugin and instead add an execution of the assembly plugin to the package phase to create a jar. you will need to write an assembly descriptor for this (not as complicated as the above link makes it out to be). this will give yu full control over what get included in the jar produced. its best to start by copying one of the predefined assemblies

这篇关于如何在不运行测试类的情况下将测试类打包到jar中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆