简体   繁体   English

什么是 Android Studio 中的 Maven?

[英]What is Maven in Android Studio?

There are lot of Answers on how to use Maven in Android and work with it.关于如何在 Android 中使用 Maven 并使用它,有很多答案。

However, As Gradle being already available, which manages the build process of Android projects and support complex scenarios in creating Android applications, such as Multi-distribution and Multi-apk,但是,由于 Gradle 已经可用,它管理 Android 项目的构建过程并支持创建 Android 应用程序的复杂场景,例如 Multi-distribution 和 Multi-apk,

What exactly is the role of Maven in Android Studio? Maven在Android Studio中到底是什么角色?

And how is it different from Gradle?它与 Gradle 有什么不同?

The short answer is: The tools do the same thing but in different ways.简短的回答是:这些工具做同样的事情,但方式不同。 The difference is how you can use them to build your project.不同之处在于您如何使用它们来构建您的项目。 Apache Maven goes for "follow our convention" way and Gradle gives you flexibility. Apache Maven 采用“遵循我们的约定”的方式,而 Gradle 为您提供了灵活性。 Some devs don't want a new tool or don't have time to learn how to use it properly.一些开发人员不想要新工具或没有时间学习如何正确使用它。

Now the long answer...现在长答案...

To understand properly why some devs are attached to Apache Maven, we have to look at some years in the past.要正确理解为什么有些开发人员会依赖 Apache Maven,我们必须回顾过去几年。

Apache Maven is a build automation tool just like Gradle . Apache Maven是一个构建自动化工具,就像Gradle一样。

Maven got released on 2004. Gradle first version got out on 2007 but it did not become as popular as Maven in the early days. Maven 于 2004 年发布Gradle第一个版本于 2007 年发布,但在早期并没有像Maven那样流行。 Some devs don't like having to learn anything new and most companies don't want to risk the exchange of an already running and mastered tool ( Maven ) for the new kid on the block.一些开发人员不喜欢学习任何新东西,大多数公司不想冒险为新来的孩子交换已经运行和掌握的工具 ( Maven )。

Gradle became popular when the Android development raised.当 Android 开发兴起时,Gradle 开始流行。 Android's project has a different project structure from Java EE/Web projects. Android 的项目与 Java EE/Web 项目有不同的项目结构。 Trying to use Maven on Android's project just feels unnatural, the tool was not prepared to provide flexibility.尝试在 Android 的项目上使用 Maven 感觉不自然,该工具没有准备好提供灵活性。

Until now, Apache Maven can be used on simple Java EE projects without being a pain in the ass if you already know the forced conventions.到目前为止,如果您已经知道强制约定,Apache Maven 可以用于简单的 Java EE 项目而不会感到痛苦。 So devs that aren't aware of Gradle/did not get in touch with Android apps don't have motivation to change to another tool.因此,不了解 Gradle/未接触 Android 应用程序的开发人员没有动力改用其他工具。

In my opinion, Gradle is better than Apache Maven in every way that you could imagine.在我看来, Gradle在你能想象到的各个方面都比Apache Maven好。 It gives you flexibility and tries not to get in your way, a "feature" that you cannot find in Apache Maven .它为您提供了灵活性并尽量不妨碍您,这是您在Apache Maven找不到的“功能”。 If you don't follow Apache Maven life cycle, your build will fail, that's it.如果你不遵循Apache Maven生命周期,你的构建就会失败,就是这样。

For example, your code can be spread across many directories in any kind of layout if you are using Gradle .例如,如果您使用Gradle ,您的代码可以以任何类型的布局分布在许多目录中。 If you are on Maven and you don't follow the 'convention', you'll lose some hours changing your pom ( Maven 's build file) to be able to understand and handle your folder structure.如果您在Maven并且不遵循“约定”,那么您将失去一些时间来更改您的 pom( Maven的构建文件)以能够理解和处理您的文件夹结构。

Eg: Java source code must be on src/main/java folder.例如:Java 源代码必须在src/main/java文件夹中。 If you got an old project and the structure is src/java , sorry, You have no guarantee that all Maven plugins will run as expected.如果您有一个旧项目并且结构是src/java ,抱歉,您不能保证所有 Maven 插件都会按预期运行。

Gradle does us a favor and puts more features for comparison in a very organized chart. Gradle帮了我们一个忙,并在一个非常有组织的图表中放置了更多的功能进行比较。 Take a look.看一看。 https://gradle.org/maven_vs_gradle/ https://gradle.org/maven_vs_gradle/

Maven is just a tool that manages and simplifies how you build your project. Maven只是一个管理和简化项目构建方式的工具。 Among many other things (running tests, managing conflicts, documentation, modularization), its most useful purpose is that of automatically fetching dependencies that your project needs and dependencies of those dependencies (transitive dependencies) if any.在许多其他事情中(运行测试、管理冲突、文档、模块化),它最有用的目的是自动获取项目需要的依赖项以及这些依赖项的依赖项(传递依赖项)(如果有的话)。 Dependencies are usually just JAR files that contain re-usable code.依赖项通常只是包含可重用代码的JAR文件。

So what does this mean.那么这是什么意思。 Suppose you want to use a library such as OpenCSV to generate a CSV file in your application.假设您想使用OpenCSV等库在您的应用程序中生成CSV文件。 Non-Maven way: Google search for the library, check if it needs other libraries and if so download them, put them in your project's classpath then build.非 Maven 方式:谷歌搜索库,检查它是否需要其他库,如果需要,下载它们,将它们放在项目的类路径中然后构建。 When you move your project to another PC, copy all the JAR's or it won't work.当您将项目移至另一台 PC 时,请复制所有 JAR,否则将无法工作。 Maven way: insert something like this in a file named pom.xml : Maven 方式:在名为pom.xml的文件中插入类似的内容:

 <dependency>
        <groupId>net.sf.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>2.3</version>
    </dependency>

Done.完毕。 Maven will do the rest for you when you build (download to local cache if not already existing, fetch dependencies, javadoc etc). Maven将在您构建时为您完成剩下的工作(如果尚不存在,则下载到本地缓存,获取依赖项,javadoc 等)。 Even if you were to copy your project to another PC without the JAR's, delete the JAR's etc, if Maven is present it will re-download them automatically when you build.即使您将项目复制到另一台没有 JAR 的 PC,删除JAR's等,如果 Maven 存在,它也会在您构建时自动重新下载它们。

For more: Link更多: 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM