Maven 项目中 src/main/java 和 src/test/java 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49995270/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 17:58:17  来源:igfitidea点击:

Difference between src/main/java and src/test/java in a Maven project

javamaven

提问by Dheemanth Bhandarkar

While creating a new Maven project, the directories src/main/javaand src/test/javaare created. With some googling, I came to know that my mainsource code that I use for testing must be placed in src/main/java. But then, what is the purpose of two separate directories. The current answer on a similar question didn't help much.

在创建新的 Maven 项目时,会创建目录src/main/javasrc/test/java。通过一些谷歌搜索,我开始知道我main用于测试的源代码必须放在src/main/java. 但是,两个独立目录的目的是什么。当前对类似问题的回答并没有多大帮助。

回答by lwi

Maven and other build management environments (e.g. gradle) are based on the assumption that you do automated testing via e.g. unit tests. For that you need extra code for testing that should not be included in your final product delivered to your customer.

Maven 和其他构建管理环境(例如 gradle)基于这样的假设:您通过例如单元测试进行自动化测试。为此,您需要额外的测试代码,这些代码不应包含在交付给客户的最终产品中。

Thus, everything that goes into src/main/javais per default packaged into the product that you would deliver for your customer whereas everything that you put into src/test/javais not.

因此,进入的所有内容都src/main/java默认打包到您将为客户交付的产品中,而您投入的所有内容都src/test/java没有。

This is an advantage for various reasons:

由于多种原因,这是一个优势:

  • your delivered products are smaller
  • it is easier to to find testrelated code inside your project
  • you can load various libraries only for testing.
  • ...
  • 您交付的产品较小
  • 在您的项目中更容易找到与测试相关的代码
  • 您可以加载各种库仅用于测试。
  • ...

回答by Chamara Maduranga

As per the Maven configurations, tests class will be found in the src/testdirectory and the source code will be found in the src/maindirectory. So src/main/javais the root directory for your source code & src/test/java/is the root directory for your testcode.

根据Maven的配置,tests类会在src/test目录中,源代码在src/main目录中。所以src/main/java是根目录的源代码&src/test/java/是你的根目录下test的代码。

Ex: Hotel Package, Reservation class

例如:酒店套餐、预订舱位

Source Class file :  src/main/java/Hotel/Reservation.java
Test Class file : src/test/java/Hotel/ReservationTest.java

回答by Moritz Petersen

The reason to have test code and production code (src/main/java) separate is, that it is easier to build the application by just including production code.

将测试代码和生产代码 ( src/main/java) 分开的原因是,只包含生产代码更容易构建应用程序。

回答by Bejond

src/main/javaplaces your code that use for real production. src/test/javaplaces your test use case code, like junit test. These codes would be executed when doing maven package things. These codes won't be packaged to your war or jar file. Which means these codes won't for real production.

src/main/java放置用于实际生产的代码。 src/test/java放置您的测试用例代码,例如 junit 测试。这些代码会在做 maven 包的事情时执行。这些代码不会打包到您的 war 或 jar 文件中。这意味着这些代码不会用于实际生产。

Plus: Unit test codes are not required to be packaged in production. You don't need to and should not to put them in src/main/javafolder.

另外:单元测试代码不需要在生产中打包。您不需要也不应该将它们放在src/main/java文件夹中。

回答by Vinod Bokare

To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages.here

为了使类型更易于查找和使用,避免命名冲突并控制访问,程序员将相关类型的组捆绑到包中。这里