Java 为什么是行家?有什么好处?

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

Why maven? What are the benefits?

javamavenbuildbuild-process

提问by trix

What are the main benefits of using maven compared to let's say ant ? It seems to be more of a annoyance than a helpful tool. I use maven 2, with plain Eclipse Java EE (no m2eclipse), and tomcat.

与 ant 相比,使用 maven 的主要好处是什么?它似乎比一个有用的工具更令人烦恼。我使用 maven 2、纯 Eclipse Java EE(没有 m2eclipse)和 tomcat。

Supporters of maven believe that

maven 的支持者认为

  1. Maven lets you get your package dependencies easily

  2. Maven forces you to have a standard directory structure

  1. Maven 让你轻松获取你的包依赖

  2. Maven 强制你有一个标准的目录结构

In my experience

在我的经验中

  1. Figuring out package dependencies is really not that hard. You rarely do it anyway. Probably once during project setup and few more during upgrades. With maven you'll end up fixing mismatched dependencies, badly written poms, and doing package exclusions anyway.

  2. Slow FIX-COMPILE-DEPLOY-DEBUG cycle, which kills productivity. This is my main gripe. You make a change, the you have to wait for maven build to kick in and wait for it to deploy. No hot deployment whatsoever.

  1. 弄清楚包的依赖关系真的没有那么难。反正你很少这样做。可能在项目设置期间一次,在升级期间可能更多。使用 maven,您最终将修复不匹配的依赖项、写得不好的 poms 以及无论如何都要进行包排除。

  2. 缓慢的 FIX-COMPILE-DEPLOY-DEBUG 循环,这会降低生产力。这是我的主要抱怨。您进行更改,您必须等待 maven 构建启动并等待它部署。没有任何热部署。

Or am I just doing it wrong ? Please point me to the right direction, I'm all ears.

还是我只是做错了?请指出我正确的方向,我全神贯注。

回答by Stephen C

Figuring out dependencies for small projects is not hard. But once you start dealing with a dependency tree with hundreds of dependencies, things can easily get out of hand. (I'm speaking from experience here ...)

弄清楚小项目的依赖关系并不难。但是,一旦您开始处理具有数百个依赖项的依赖项树,事情就很容易失控。(我是从这里的经验说话......)

The other point is that if you use an IDE with incremental compilation and Maven support (like Eclipse + m2eclipse), then you should be able to set up edit/compile/hot deploy and test.

另一点是,如果您使用具有增量编译和 Maven 支持的 IDE(如 Eclipse + m2eclipse),那么您应该能够设置编辑/编译/热部署和测试。

I personally don't do this because I've come to distrust this mode of development due to bad experiences in the past (pre Maven). Perhaps someone can comment on whether this actually works with Eclipse + m2eclipse.

我个人不这样做,因为由于过去的糟糕经历(Maven 之前),我开始不信任这种开发模式。也许有人可以评论这是否真的适用于 Eclipse + m2eclipse。

回答by user433555

Maven can provide benefits for your build process by employing standard conventions and practices to accelerate your development cycle while at the same time helping you achieve a higher rate of success. For a more detailed look at how Maven can help you with your development process please refer to The Benefits of Using Maven.

Maven 可以通过采用标准约定和实践来加速您的开发周期,同时帮助您实现更高的成功率,从而为您的构建过程带来好处。要更详细地了解 Maven 如何帮助您完成开发过程,请参阅使用 Maven 的好处。

回答by Thorbj?rn Ravn Andersen

Maven is one of the tools where you need to actually decideup front that you like it and want to use it, since you will spend quite some time learning it, and having made said decision once and for all will allow you to skip all kinds of doubt while learning (because you likeit and wantto use it)!

Maven 是一种工具,您需要预先决定您喜欢它并想要使用它,因为您将花费相当多的时间来学习它,并且一劳永逸地做出上述决定将允许您跳过所有类型学习的时候怀疑(因为你喜欢它并且使用它)!

The strong conventions help in many places - like Hudson that can do wonders with Maven projects - but it may be hard to see initially.

强大的约定在很多地方都有帮助——比如 Hudson 可以用 Maven 项目创造奇迹——但最初可能很难看到。

edit: As of 2016 Maven is the only Java build tool where all three major IDEs can use the sources out of the box. In other words, using maven makes your build IDE-agnostic. This allows for e.g. using Netbeans profiling even if you normally work In eclipse

编辑:截至 2016 年,Maven 是唯一一个所有三个主要 IDE 都可以使用开箱即用源的 Java 构建工具。换句话说,使用 maven 使您的构建与 IDE 无关。这允许例如使用 Netbeans 分析,即使您通常在 eclipse 中工作

回答by Goibniu

I've never come across point 2? Can you explain why you think this affects deployment in any way. If anything maven allows you to structure your projects in a modularised way that actually allows hot fixes for bugs in a particular tier, and allows independent development of an API from the remainder of the project for example.

我从来没有遇到过第 2 点?你能解释一下为什么你认为这会以任何方式影响部署。如果 maven 允许您以模块化方式构建项目,该方式实际上允许针对特定层中的错误进行热修复,并允许从项目的其余部分独立开发 API。

It is possible that you are trying to cram everything into a single module, in which case the problem isn't really maven at all, but the way you are using it.

您可能试图将所有内容都塞进一个模块中,在这种情况下,问题根本不是 maven,而是您使用它的方式。

回答by YoK

Maven can be considered as complete project development tool not just build tool like Ant. You should use Eclipse IDE with maven pluginto fix all your problems.

Maven 可以被认为是完整的项目开发工具,而不仅仅是像 Ant 那样的构建工具。您应该使用带有 maven 插件的 Eclipse IDE来解决所有问题。

Here are few advantages of Maven, quoted from the Benefits of using Mavenpage:

以下是 Maven 的一些优点,引用自使用 Maven好处页面:

Henning

  • quick project setup, no complicated build.xml files, just a POM and go
  • all developers in a project use the same jar dependencies due to centralized POM.
  • getting a number of reports and metrics for a project "for free"
  • reduce the size of source distributions, because jars can be pulled from a central location

Emmanuel Venisse

  • a lot of goals are available so it isn't necessary to develop some specific build process part contrary to ANT we can reuse existing ANT tasks in build process with antrun plugin

Jesse Mcconnell

  • Promotes modular design of code. by making it simple to manage mulitple projects it allows the design to be laid out into muliple logical parts, weaving these parts together through the use of dependency tracking in pom files.
  • Enforces modular design of code. it is easy to pay lipservice to modular code, but when the code is in seperate compiling projects it is impossible to cross pollinate references between modules of code unless you specifically allow for it in your dependency management... there is no 'I'll just do this now and fix it later' implementations.
  • Dependency Management is clearly declared. with the dependency management mechanism you have to try to screw up your jar versioning...there is none of the classic problem of 'which version of this vendor jar is this?' And setting it up on an existing project rips the top off of the existing mess if it exists when you are forced to make 'unknown' versions in your repository to get things up and running...that or lie to yourself that you know the actual version of ABC.jar.
  • strong typed life cycle there is a strong defined lifecycle that a software system goes thru from the initiation of a build to the end... and the users are allowed to mix and match their system to the lifecycle instead of cobble together their own lifecycle.. this has the additional benefit of allowing people to move from one project to another and speak using the same vocabulary in terms of software building

Vincent Massol

  • Greater momentum: Ant is now legacy and not moving fast ahead. Maven is forging ahead fast and there's a potential of having lots of high-value tools around Maven (CI, Dashboard project, IDE integration, etc).

亨宁

  • 快速项目设置,无需复杂的 build.xml 文件,只需一个 POM 即可
  • 由于集中式 POM,项目中的所有开发人员都使用相同的 jar 依赖项。
  • “免费”获取项目的大量报告和指标
  • 减少源分布的大小,因为可以从中心位置拉出罐子

埃马纽埃尔·韦尼斯

  • 有很多目标可用,因此没有必要开发一些与 ANT 相反的特定构建过程部分,我们可以使用 antrun 插件在构建过程中重用现有的 ANT 任务

杰西麦康奈尔

  • 促进代码的模块化设计。通过简化管理多个项目,它允许将设计布置为多个逻辑部分,通过在 pom 文件中使用依赖项跟踪将这些部分编织在一起。
  • 强制代码的模块化设计。很容易对模块化代码进行口头服务,但是当代码处于单独的编译项目中时,除非您在依赖项管理中明确允许它,否则不可能在代码模块之间交叉授粉引用......没有'我会现在就做这件事,然后在以后的实现中修复它。
  • 明确声明了依赖管理。使用依赖管理机制,你必须尝试搞砸你的 jar 版本......没有经典问题“这是哪个供应商 jar 版本?” 如果当您被迫在存储库中制作“未知”版本以启动和运行时,将它设置在现有项目上会消除现有混乱的顶部...... ABC.jar 的实际版本。
  • 强类型生命周期有一个强定义的生命周期,软件系统从构建开始到结束都要经历这个生命周期……并且允许用户将他们的系统与生命周期混合和匹配,而不是将他们自己的生命周期拼凑在一起。 . 这有一个额外的好处,它允许人们从一个项目转移到另一个项目,并在软件构建方面使用相同的词汇

文森特·马索尔

  • 更大的动力:Ant 现在是传统的,并没有快速前进。Maven 正在快速前进,并且有可能拥有许多围绕 Maven 的高价值工具(CI、仪表板项目、IDE 集成等)。

回答by Pascal Thivent

Figuring out package dependencies is really not that hard. You rarely do it anyway. Probably once during project setup and few more during upgrades. With maven you'll end up fixing mismatched dependencies, badly written poms, and doing package exclusions anyway.

弄清楚包的依赖关系真的没有那么难。反正你很少这样做。可能在项目设置期间一次,在升级期间可能更多。使用 maven,您最终将修复不匹配的依赖项、写得不好的 poms 以及无论如何都要进行包排除。

Not that hard... for toy projects. But the projects I work on have many, really many, of them, and I'm very glad to get them transitively, to have a standardized naming scheme for them. Managing all this manually by hand would be a nightmare.

没那么难……对于玩具项目。但是我从事的项目有很多,真的很多,我很高兴能够传递它们,为它们制定标准化的命名方案。手动管理所有这些将是一场噩梦。

And yes, sometimes you have to work on the convergence of dependencies. But think about it twice, this is not inherent to Maven, this is inherent to any system using dependencies (and I am talking about Java dependencies in general here).

是的,有时您必须处理依赖项的收敛。但是仔细想想,这不是 Maven 固有的,这是任何使用依赖项的系统所固有的(我在这里谈论的是一般的 Java 依赖项)。

So with Ant, you have to do the samework except that you have to do everything manually: grabbing some version of project A and its dependencies, grabbing some version of project B and its dependencies, figuring out yourself what exact versions they use, checking that they don't overlap, checking that they are not incompatible, etc. Welcome to hell.

所以对于 Ant,你必须做同样的工作,除了你必须手动完成所有事情:获取项目 A 的某个版本及其依赖项,获取项目 B 的某个版本及其依赖项,弄清楚他们使用的确切版本,检查确保它们不重叠,检查它们是否不兼容,等等。欢迎来到地狱。

On the other hand, Maven supports dependency management and will retrieve them transitively for me and gives me the tooling I need to manage the complexity inherent to dependency management: I can analyze a dependency tree, control the versions used in transitive dependencies, exclude some of them ifrequired, control the converge across modules, etc. There is no magic. But at least you have support.

另一方面,Maven 支持依赖管理,并会为我传递性地检索它们,并为我提供管理依赖管理固有复杂性所需的工具:我可以分析依赖树,控制传递依赖中使用的版本,排除一些他们如果需要,控制收敛跨模块等,有没有神奇。但至少你有支持。

And don't forget that dependency management is only a small part of what Maven offers, there is much more (not even mentioning the other tools that integrates nicely with Maven, e.g. Sonar).

并且不要忘记依赖管理只是 Maven 提供的一小部分,还有更多(甚至没有提到与 Maven 很好地集成的其他工具,例如Sonar)。

Slow FIX-COMPILE-DEPLOY-DEBUG cycle, which kills productivity. This is my main gripe. You make a change, the you have to wait for maven build to kick in and wait for it to deploy. No hot deployment whatsoever.

缓慢的 FIX-COMPILE-DEPLOY-DEBUG 循环,这会降低生产力。这是我的主要抱怨。您进行更改,您必须等待 maven 构建启动并等待它部署。没有任何热部署。

First, why do you use Maven like this? I don't. I use my IDE to write tests, code until they pass, refactor, deploy, hot deploy and run a local Maven build when I'm done, before to commit, to make sure I will not break the continuous build.

首先,你为什么这样使用Maven?我不。我使用我的 IDE 编写测试、代码直到它们通过、重构、部署、热部署并在我完成后运行本地 Maven 构建,然后再提交,以确保我不会破坏连续构建。

Second, I'm not sure using Ant would make things much better. And to my experience, modular Maven builds using binary dependencies gives me faster build time than typical monolithic Ant builds. Anyway, have a look at Maven Shellfor a ready to (re)use Maven environment (which is awesome by the way).

其次,我不确定使用 Ant 会让事情变得更好。根据我的经验,使用二进制依赖项的模块化 Maven 构建比典型的整体 Ant 构建提供了更快的构建时间。无论如何,看看Maven Shell以准备(重新)使用 Maven 环境(顺便说一句,这很棒)。

So at end, and I'm sorry to say so, it's not really Maven that is killing your productivity, it's you misusing your tools. And if you're not happy with it, well, what can I say, don't use it. Personally, I'm using Maven since 2003 and I never looked back.

所以最后,我很抱歉地说,并不是真正的 Maven 正在扼杀你的生产力,而是你滥用了你的工具。如果你对它不满意,好吧,我能说什么,不要使用它。就个人而言,我从 2003 年开始使用 Maven,而且我从未回头。

回答by Luca Botti

Maven advantages over ant are quite a few. I try to summarize them here.

Maven 相对于 ant 的优势很多。我试着在这里总结一下。

Convention over Configuration
Maven uses a distinctive approach for the project layout and startup, that makes easy to just jump in a project. Usually it only takes the checkount and the maven command to get the artifacts of the project.

约定优于配置
Maven 为项目布局和启动使用了一种独特的方法,这使得直接跳转到项目中变得容易。通常只需要 checkoutt 和 maven 命令来获取项目的工件。

Project Modularization
Project conventions suggest (or better, force) the developer to modularize the project. Instead of a monolithic project you are often forced to divide your project in smaller sub components, which make it easier debug and manage the overall project structure

项目模块化
项目约定建议(或更好地,强制)开发人员将项目模块化。您经常被迫将项目划分为更小的子组件,而不是整体项目,这使得调试和管理整个项目结构变得更加容易

Dependency Management and Project Lifecycle
Overall, with a good SCM configuration and an internal repository, the dependency management is quite easy, and you are again forced to think in terms of Project Lifecycle - component versions, release management and so on. A little more complex than the ant something, but again, an improvement in quality of the project.

依赖管理和项目生命周期
总体而言,通过良好的 SCM 配置和内部存储库,依赖管理非常容易,并且您再次被迫考虑项目生命周期 - 组件版本、发布管理等。比 ant 稍微复杂一点,但同样是项目质量的提高。

What is wrong with maven?
Maven is not easy. The build cycle (what gets done and when) is not so clear within the POM. Also, some issue arise with the quality of components and missing dependencies in public repositories.
The best approach (to me) is to have an internal repository for caching (and keeping) dependencies around, and to apply to release management of components. For projects bigger than the sample projects in a book, you will thank maven before or after

maven 有什么问题?
Maven 并不容易。在 POM 中,构建周期(完成什么以及何时完成)并不是很清楚。此外,公共存储库中的组件质量和缺少依赖项也会出现一些问题。
最好的方法(对我来说)是拥有一个内部存储库来缓存(和保留)依赖项,并应用于组件的发布管理。对于比书中示例项目更大的项目,你会在之前或之后感谢maven

回答by Nikhil Pahariya

Maven is a powerful project management tool that is based on POM (project object model). It is used for projects build, dependency and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT. Maven helps to manage- Builds,Documentation,Reporing,SCMs,Releases,Distribution. - maven repository is a directory of packaged JAR file with pom.xml file. Maven searches for dependencies in the repositories.

Maven 是一个强大的项目管理工具,它基于 POM(项目对象模型)。它用于项目构建、依赖和文档。它像 ANT 一样简化了构建过程。但是它比ANT先进太多了。Maven 有助于管理构建、文档、报告、SCM、发布、分发。- maven 存储库是打包 JAR 文件和 pom.xml 文件的目录。Maven 在存储库中搜索依赖项。

回答by Syed Aqeel Ashiq

This should have been a comment, but it wasn't fitting in a comment length, so I posted it as an answer.

这应该是评论,但它不适合评论长度,所以我将其发布为答案。

All the benefits mentioned in other answers are achievable by simpler means than using maven. If, for-example, you are new to a project, you'll anyway spend more time creating project architecture, joining components, coding than downloading jars and copying them to lib folder. If you are experienced in your domain, then you already know how to start off the project with what libraries. I don't see any benefit of using maven, especially when it poses a lot of problems while automatically doing the "dependency management".

其他答案中提到的所有好处都可以通过比使用 maven 更简单的方法来实现。例如,如果您是一个项目的新手,那么与下载 jars 并将它们复制到 lib 文件夹相比,您将花费更多的时间来创建项目架构、加入组件、编码。如果您在自己的领域有经验,那么您已经知道如何使用哪些库开始项目。我没有看到使用 maven 的任何好处,尤其是当它在自动进行“依赖管理”时带来很多问题时。

I only have intermediate level knowledge of maven, but I tell you, I have done large projects(like ERPs) without using maven.

我只有 maven 的中级知识,但我告诉你,我做过大型项目(如 ERPs)而没有使用 maven。