'src/main/java'' 的优点是什么?习俗?
我注意到很多项目都有以下结构:
- 项目-A
- 垃圾箱
- 库
- 源代码
- 主要
- java
- RootLevelPackageClass.java
- java
- 主要
我目前使用以下约定(因为我的项目是 100% java):
- Project-A
- 垃圾箱
- 库
- 源代码
- RootLevelPackageClass.java
我当前没有使用 Maven,但想知道这是否是 Maven 约定,或者是否还有其他原因。有人可以解释为什么第一个版本现在如此受欢迎以及我是否应该采用这个新约定?
I've noticed that a lot of projects have the following structure:
- Project-A
- bin
- lib
- src
- main
- java
- RootLevelPackageClass.java
- java
- main
I currently use the following convention (as my projects are 100% java):
- Project-A
- bin
- lib
- src
- RootLevelPackageClass.java
I'm not currently using Maven but am wondering if this is a Maven convention or not or if there is another reason. Can someone explain why the first version is so popular these days and if I should adopt this new convention or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
主要好处是将
test
目录作为src
的子目录,其目录结构与main
中的目录结构相同:RootLevelPackageClass
的所有包私有方法都是可见的,即可以从TestRootLevelPackageClass
进行测试。由于测试代码也是源代码,因此它的位置应该位于 src 目录下。Main benefit is in having the
test
directory as subdirectory ofsrc
with the same directory structure as the one inmain
:All package private methods of
RootLevelPackageClass
will be visible, i.e. testable fromTestRootLevelPackageClass
. Since the testing code is also source its place should be undersrc
directory.是的,这就是 Maven 约定。
即使您的项目是 100% Java(顺便说一句,这是典型的 Maven),您也经常拥有资源文件(根据 Maven 约定,这些文件位于 src/main/resources 中)或 Web 应用程序内容,或者...所有这些都可以轻松地融入 Maven 系统。
如果您对当前的构建系统(无论它是什么)感到满意,则没有理由切换到 Maven。否则,或者如果开始一个新项目,您可以评估您的选项,包括 Maven。
Yes, this is the Maven convention.
Even if your project is 100% Java (as is typical with Maven btw), you often have resource files (which go to
src/main/resources
according to the Maven convention), or web app stuff, or ... all these fit into the Maven system easily.If you are happy with your current build system (whatever it is), there is no reason to switch to Maven. Otherwise, or if starting a new project, you could evaluate your options, including Maven.
其他人已经告诉你这是一个 Maven 约定,我将回答你的问题:
绝对没有。当然,将代码片段分开以单独的根文件夹是有益的,但通常您可以使用
改为。事实上,Maven 所做的一件大事实际上是非常错误的:它想要将二进制内容添加到源代码存储库,而该源代码存储库仅适用于文本内容!所有二进制内容都应在源代码存储库之外进行管理,其中包括 Web 应用程序中的图像等。
但是好吧,让我们假设您已经决定生活在有点臭的 Maven 生态系统中;那么你当然应该尽可能严格地遵循 Maven 约定。
Others have already told you it's a Maven convention, I'm going to answer your question instead:
Absolutely none. Certainly it's beneficial to separate pieces of code to separate root folders, but usually you could achieve the same with
instead. In fact here's a big thing Maven does that's actually hugely wrong: It wants to add binary content to source code repository which is meant for textual content only! All binary content should be managed outside the source code repository, that includes images in web applications and whatnot.
But OK, lets assume that you've decided to live in the somewhat smelly Maven ecosystem; then you should of course follow the Maven conventions as strictly as possible.
这是一个 Maven 约定。
Maven 基于约定优于配置范例。这意味着:如果您不遵循此约定,则必须配置源所在的位置。恕我直言,这是主要的好处。
Its a Maven convention.
Maven is based on Convention over configuration paradigm. Thats means: if you dont follow this convention you must configure where the sources are located. Thats the main benefit IMHO.
是的,这是一个 Maven 约定,但即使您不使用 Maven,使用它也有好处:
虽然我不认为你应该只是为了切换而切换,当开始一个新项目时,确实没有理由不使用它——除非你在哲学上不同意它如何分解代码。
Yes, this is a maven convention, but even if you're not using maven, there are benefits to using it:
Although I wouldn't argue you should switch just to switch, when starting a new project there's really no reason to not use it-- unless you disagree philosophically with how it breaks the code up.