“src/main/java”约定的优点是什么?

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

What is the advantage of the 'src/main/java'' convention?

javacoding-styleconventions

提问by Chris

I've noticed that a lot of projects have the following structure:

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

  • Project-A
    • bin
    • lib
    • src
      • main
        • java
          • RootLevelPackageClass.java
  • 项目-A
    • 垃圾桶
    • 源文件
      • 主要的
        • 爪哇
          • RootLevelPackageClass.java

I currently use the following convention (as my projects are 100% java):

我目前使用以下约定(因为我的项目是 100% java):

  • Project-A
    • bin
    • lib
    • src
      • RootLevelPackageClass.java
  • 项目-A
    • 垃圾桶
    • 源文件
      • 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?

我目前没有使用 Maven,但我想知道这是否是 Maven 约定,或者是否还有其他原因。有人可以解释为什么第一个版本现在如此受欢迎,我是否应该采用这个新约定?

Chris

克里斯

采纳答案by Boris Pavlovi?

Main benefit is in having the testdirectory as subdirectory of srcwith the same directory structure as the one in main:

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

  • Project-A
    • bin
    • lib
    • src
      • main
        • java
          • RootLevelPackageClass.java
        • resources
      • test
        • java
          • TestRootLevelPackageClass.java
        • resources
  • 项目-A
    • 垃圾桶
    • 源文件
      • 主要的
        • 爪哇
          • RootLevelPackageClass.java
        • 资源
      • 测试
        • 爪哇
          • TestRootLevelPackageClass.java
        • 资源

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

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

回答by Péter T?r?k

Yes, this is the Maven convention.

是的,这是 Maven 约定。

Even if your project is 100% Java (as is typical with Maven btw), you often have resource files (which go to src/main/resourcesaccording to the Maven convention), or web app stuff, or ... all these fit into the Maven system easily.

即使您的项目是 100% Java(就像 Maven 顺便说一句的典型),您通常也有资源文件(src/main/resources根据 Maven 约定),或者 Web 应用程序的东西,或者……所有这些都可以轻松地融入 Maven 系统.

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。

回答by sourcerebels

Its a Maven convention.

它是一个 Maven 约定。

Maven is based on Convention over configurationparadigm. Thats means: if you dont follow this convention you must configure where the sources are located. Thats the main benefit IMHO.

Maven 基于约定优于配置范式。这意味着:如果您不遵循此约定,则必须配置源所在的位置。恕我直言,这就是主要的好处。

回答by Esko

Others have already told you it's a Maven convention, I'm going to answer your question instead:

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

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
  • [根]
    • 源文件
      • com.org.net
        • 你的班
    • 测试
      • com.org.net
        • 你的测试类
    • 垃圾桶
    • 资源

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.

反而。事实上,Maven 做的一件大事实际上是非常错误的:它想将二进制内容添加到仅用于文本内容的源代码存储库中!所有二进制内容都应该在源代码存储库之外进行管理,包括 Web 应用程序中的图像等等。

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 约定。

回答by ndp

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

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

  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
  1. 项目的新手将更容易上手,因为它是“标准”
  2. 这个约定是灵活的,并且为非 Java 代码和您目前没有的其他东西提供了一个位置。这是它受欢迎的原因之一,您可能会发现它比您自己提出的方案发展得更好
  3. 如果你想在某个时候转移到 maven 会很容易

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.

虽然我不会争辩说你应该切换只是切换,但在开始一个新项目时,真的没有理由不使用它——除非你在哲学上不同意它如何分解代码。