'src/main/java'' 的优点是什么?习俗?

发布于 2024-09-05 06:43:40 字数 446 浏览 6 评论 0原文

我注意到很多项目都有以下结构:

  • 项目-A
    • 垃圾箱
    • 源代码
      • 主要
        • java
          • RootLevelPackageClass.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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自演自醉 2024-09-12 06:43:40

主要好处是将 test 目录作为 src 的子目录,其目录结构与 main 中的目录结构相同:

  • Project-A
    • 垃圾箱
    • 源代码
      • 主要
        • java
          • RootLevelPackageClass.java
        • 资源
      • 测试
        • java
          • TestRootLevelPackageClass.java
        • 资源

RootLevelPackageClass 的所有包私有方法都是可见的,即可以从 TestRootLevelPackageClass 进行测试。由于测试代码也是源代码,因此它的位置应该位于 src 目录下。

Main benefit is in having the test directory as subdirectory of src with the same directory structure as the one in main:

  • Project-A
    • bin
    • lib
    • src
      • main
        • java
          • RootLevelPackageClass.java
        • resources
      • test
        • java
          • TestRootLevelPackageClass.java
        • resources

All package private methods of RootLevelPackageClass will be visible, i.e. testable from TestRootLevelPackageClass. Since the testing code is also source its place should be under src directory.

疧_╮線 2024-09-12 06:43:40

是的,这就是 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.

差↓一点笑了 2024-09-12 06:43:40

其他人已经告诉你这是一个 Maven 约定,我将回答你的问题:

绝对没有。当然,将代码片段分开以单独的根文件夹是有益的,但通常您可以使用

  • [root] 实现相同的效果
    • 源代码
      • com.org.net
        • 你的班级
    • 测试
      • com.org.net
        • 你的测试.class
    • 垃圾箱
    • 资源

改为。事实上,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

  • [root]
    • src
      • com.org.net
        • Your.class
    • test
      • com.org.net
        • YourTest.class
    • lib
    • bin
    • resources

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.

纵山崖 2024-09-12 06:43:40

这是一个 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.

惯饮孤独 2024-09-12 06:43:40

是的,这是一个 Maven 约定,但即使您不使用 Maven,使用它也有好处:

  1. 刚接触该项目的人将更容易上手,因为它是一个“标准”,
  2. 该约定很灵活并且有一个地方可以存放非 Java 代码和其他目前还没有的东西。这是它受欢迎的原因之一,你可能会发现它比你自己想出的方案发展得更好,
  3. 如果你想在某个时候迁移到 Maven,那会很容易

虽然我不认为你应该只是为了切换而切换,当开始一个新项目时,确实没有理由不使用它——除非你在哲学上不同意它如何分解代码。

Yes, this is a maven convention, but even if you're not using maven, there are benefits to using it:

  1. people new to the project will have an easier time coming up to speed, since it's a "standard"
  2. this convention is flexible and has a place for non-Java code and other things you don't have at this point. This is one reason it's popular and you may find it evolves better than a scheme you come up with on your own
  3. if you want to move to maven at some point it will be easy

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文