Maven 在理论上和实践中做了什么?什么时候值得使用? [英] What does Maven do, in theory and in practice? When is it worth to use it?

查看:21
本文介绍了Maven 在理论上和实践中做了什么?什么时候值得使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我即将开始一个 Java 项目,只是为了练习.我读过关于 Maven 的文章,但我实际上不明白什么时候应该使用它.

I am about to start a Java project just for practice. I've read about Maven, but I don't actually understand when it is meant to be used.

你能给我一些实用的建议吗?Maven 有很大帮助吗?Maven 对我的项目有什么实际作用?

Can you give me some practical tips? Does Maven help a lot? What does Maven actually do for my project?

推荐答案

它的作用

Maven 是一个构建管理工具",它用于定义您的 .java 文件如何编译为 .class,打包成 .jar(或 .war.ear)文件,使用工具(前/后)处理,管理您的 CLASSPATH,以及构建项目所需的所有其他类型的任务.它类似于 Apache AntGradleMakefiles 在 C/C++ 中,但它试图完全独立于其中,通过合并其他常见任务(例如下载和下载),您不应该需要任何其他工具或脚本.安装必要的库等

Maven is a "build management tool", it is for defining how your .java files get compiled to .class, packaged into .jar (or .war or .ear) files, (pre/post)processed with tools, managing your CLASSPATH, and all others sorts of tasks that are required to build your project. It is similar to Apache Ant or Gradle or Makefiles in C/C++, but it attempts to be completely self-contained in it that you shouldn't need any additional tools or scripts by incorporating other common tasks like downloading & installing necessary libraries etc.

它也是围绕构建可移植性"主题设计的,因此您不会遇到在一台计算机上使用相同构建脚本的相同代码但在另一台计算机上无法运行的问题(这是一个已知问题,我们有Windows 98 机器的 VM,因为我们无法在其他任何地方编译我们的某些 Delphi 应用程序).正因如此,这也是在使用不同 IDE 的人之间开展项目的最佳方式,因为 IDE 生成的 Ant 脚本很难导入其他 IDE,但现在所有 IDE 都理解并支持 Maven (IntelliJ, EclipseNetBeans).即使您最终不喜欢 Maven,它最终也会成为所有其他现代构建工具的参考点.

It is also designed around the "build portability" theme, so that you don't get issues as having the same code with the same buildscript working on one computer but not on another one (this is a known issue, we have VMs of Windows 98 machines since we couldn't get some of our Delphi applications compiling anywhere else). Because of this, it is also the best way to work on a project between people who use different IDEs since IDE-generated Ant scripts are hard to import into other IDEs, but all IDEs nowadays understand and support Maven (IntelliJ, Eclipse, and NetBeans). Even if you don't end up liking Maven, it ends up being the point of reference for all other modern builds tools.

为什么要使用它

关于 Maven 的三个方面非常很好.

There are three things about Maven that are very nice.

  1. Maven 将(在您声明您正在使用哪些库之后)下载您使用的所有库他们为您使用的库.这非常好,并且使处理许多库变得非常容易.这可以让您避免依赖地狱".它类似于 Apache Ant 的 Ivy.

  1. Maven will (after you declare which ones you are using) download all the libraries that you use and the libraries that they use for you automatically. This is very nice, and makes dealing with lots of libraries ridiculously easy. This lets you avoid "dependency hell". It is similar to Apache Ant's Ivy.

它使用约定优于配置",因此默认情况下您不会需要定义你想要做的任务.您不需要像在 Ant 或 Makefile 中那样编写编译"、测试"、打包"或清理"步骤.只需将文件放在 Maven 期望它们的位置,它就会立即工作.

It uses "Convention over Configuration" so that by default you don't need to define the tasks you want to do. You don't need to write a "compile", "test", "package", or "clean" step like you would have to do in Ant or a Makefile. Just put the files in the places in which Maven expects them and it should work off of the bat.

Maven 也有很多不错的插件,你可以安装它们来处理来自 使用 JAXB 从 XSD 模式生成 Java 类测量Cobertura 的测试覆盖率.只需将它们添加到您的 pom.xml 中,它们就会与您想做的所有其他事情集成.

Maven also has lots of nice plug-ins that you can install that will handle many routine tasks from generating Java classes from an XSD schema using JAXB to measuring test coverage with Cobertura. Just add them to your pom.xml and they will integrate with everything else you want to do.

最初的学习曲线很陡峭,但(几乎)每个专业 Java 开发人员都使用 Maven 或希望他们这样做.您应该在每个项目中使用 Maven,尽管如果您需要一段时间来习惯它,并且有时您希望自己可以手动完成工作,请不要感到惊讶,因为学习新东西有时会很痛苦.但是,一旦您真正习惯了 Maven,您就会发现构建管理几乎不需要任何时间.

The initial learning curve is steep, but (nearly) every professional Java developer uses Maven or wishes they did. You should use Maven on every project although don't be surprised if it takes you a while to get used to it and that sometimes you wish you could just do things manually, since learning something new sometimes hurts. However, once you truly get used to Maven you will find that build management takes almost no time at all.

如何开始

最好的起点是Maven in 5分钟".它将让您开始一个项目,让您可以使用所有必要的文件和文件夹设置进行编码(是的,我建议使用快速入门原型,至少一开始是这样).

The best place to start is "Maven in 5 Minutes". It will get you start with a project ready for you to code in with all the necessary files and folders set-up (yes, I recommend using the quickstart archetype, at least at first).

开始使用后,您需要更好地了解该工具的用途.为此,Better Builds with Maven"是了解其工作原理的最彻底的地方,但是,Maven:完整参考" 是最新的.阅读第一个理解,然后使用第二个作为参考.

After you get started you'll want a better understanding over how the tool is intended to be used. For that "Better Builds with Maven" is the most thorough place to understand the guts of how it works, however, "Maven: The Complete Reference" is more up-to-date. Read the first one for understanding, but then use the second one for reference.

这篇关于Maven 在理论上和实践中做了什么?什么时候值得使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆