简体   繁体   English

Intellij IDEA 将 junit 测试放在 jar 中

[英]Intellij IDEA puts junit tests in jar

How to tell Intellij IDEA not to include test classes in artifact?如何告诉 Intellij IDEA 不要在工件中包含测试类? I'm working on a lot of projects so it's better if there's a solution like "ignore everything in project-dir/test directory", or even better "put in the jar only classes from project-dir/src .我正在处理很多项目,所以最好有一个解决方案,比如“忽略project-dir/test目录中的所有内容”,或者更好的是“只将project-dir/src类放入 jar 中。

How are you configuring the jar file in IntelliJ IDEA? IntelliJ IDEA中的jar文件是怎么配置的?

If I go to Project Structure (cmd + ; on Mac, ctrl + shift + s on Windows) and select "Artifacts", when I add a new artifact using "From modules with dependencies" it gives me the option of including tests or not:如果我转到项目结构(在 Mac 上为 cmd + ;,在 Windows 上为 ctrl + shift + s)并选择“工件”,当我使用“来自具有依赖项的模块”添加新工件时,它给了我是否包含测试的选项:

来自具有依赖项的模块

If I add a new jar file using "Empty", then I can choose exactly what appears in the jar file:如果我使用“Empty”添加一个新的 jar 文件,那么我可以准确选择 jar 文件中出现的内容:

添加文件

Here I select "Module Output" and this should only add the class files from your module, not the test files (assuming you have your test directory marked as a test directory, and not as a source directory).在这里,我选择“模块输出”,这应该只添加模块中的类文件,而不是测试文件(假设您将测试目录标记为测试目录,而不是源目录)。

If you look to the left of that screenshot, behind the project structure window, you can see that I have production code in src/main/java and test code in src/test/java, and this test code is coloured green - this does not appear in my jar file if I configure it this way.如果你看那个截图的左边,在项目结构窗口后面,你可以看到我在 src/main/java 中有生产代码,在 src/test/java 中有测试代码,并且这个测试代码是绿色的 - 这是如果我以这种方式配置它,则不会出现在我的 jar 文件中。

You should take a look at Gradle.你应该看看Gradle。 Gradle will provide you with jar tasks that will exclude test files, or those that you ignore. Gradle 将为您提供 jar 任务,这些任务将排除测试文件或您忽略的那些。 Gradle will only package files in the main directory. Gradle 只会打包主目录中的文件。

src/main/java源代码/主/Java

src/test/java源代码/测试/Java

I'm assuming you are not using a build system, and have all files in the root folder.我假设您没有使用构建系统,并且所有文件都在根文件夹中。 Without direction the IDE will not know which files are tests files and include them in the jar.如果没有方向,IDE 将不知道哪些文件是测试文件并将它们包含在 jar 中。

You need a build system like Ant, Maven or Gradle.你需要一个像 Ant、Maven 或 Gradle 这样的构建系统。 I recommend Gradle it will build as an IntelliJ project, and there are many plugins for the IDE.我推荐 Gradle 它将构建为 IntelliJ 项目,并且有许多适用于 IDE 的插件。

Right click on project-dir/test Mark Directory as ->Test Source root Right click on all directory inside project-dir/test and Mark Directory as ->excluded右键单击 project-dir/test 将目录标记为 ->Test Source root 右键单击​​ project-dir/test 中的所有目录并将目录标记为 ->excluded

Now build Artifact it will remove test case现在构建 Artifact 它将删除测试用例

Also check in ctrl+alt+shift+s (file-> project structure ) Click on Artifact From left panel-> now on right side screen From output layout remove all junit or test case related jar还要检查 ctrl+alt+shift+s(文件-> 项目结构)单击 Artifact 从左侧面板-> 现在在右侧屏幕从输出布局中删除所有与 junit 或测试用例相关的 jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM