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
Difference between src/main/java and src/test/java in a Maven project
提问by Dheemanth Bhandarkar
While creating a new Maven project, the directories src/main/java
and src/test/java
are created. With some googling, I came to know that my main
source 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/java
和src/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/java
is per default packaged into the product that you would deliver for your customer whereas everything that you put into src/test/java
is 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/test
directory and the source code will be found in the src/main
directory. So src/main/java
is the root directory for your source code & src/test/java/
is the root directory for your test
code.
根据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/java
places your code that use for real production.
src/test/java
places 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/java
folder.
另外:单元测试代码不需要在生产中打包。您不需要也不应该将它们放在src/main/java
文件夹中。