java 什么是 Android Studio 中的 Maven?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33205980/
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
What is Maven in Android Studio?
提问by OBX
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 应用程序的复杂场景,例如多分布和多 apk,
What exactly is the role of Maven in Android Studio?
Maven在Android Studio中的作用到底是什么?
And how is it different from Gradle?
它与 Gradle 有什么不同?
回答by Bruno
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. Some devs don't want a new tool or don't have time to learn how to use it properly.
简短的回答是:这些工具做同样的事情,但方式不同。不同之处在于您如何使用它们来构建您的项目。Apache Maven 采用“遵循我们的约定”的方式,而 Gradle 为您提供了灵活性。一些开发人员不想要新工具或没有时间学习如何正确使用它。
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.
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 于 2004 年发布。Gradle
第一个版本于 2007 年发布,但并没有像Maven
早期那样流行。一些开发人员不喜欢必须学习任何新东西,而且大多数公司不想冒险Maven
为新来的孩子交换已经运行和掌握的工具 ( )。
Gradle became popular when the Android development raised. Android's project has a different project structure from Java EE/Web projects. Trying to use Maven on Android's project just feels unnatural, the tool was not prepared to provide flexibility.
当 Android 开发兴起时,Gradle 开始流行。Android 的项目与 Java EE/Web 项目有不同的项目结构。尝试在 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. 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.
到目前为止,如果您已经知道强制约定,Apache Maven 可以用于简单的 Java EE 项目而不会感到痛苦。因此,不了解 Gradle/未接触 Android 应用程序的开发人员没有动力改用其他工具。
In my opinion, Gradle
is better than Apache Maven
in every way that you could imagine. It gives you flexibility and tries not to get in your way, a "feature" that you cannot find in Apache Maven
. If you don't follow Apache Maven
life cycle, your build will fail, that's it.
在我看来,Gradle
比Apache Maven
你能想象的任何方式都要好。它为您提供灵活性并尽量不妨碍您,这是您在Apache Maven
. 如果你不遵循Apache Maven
生命周期,你的构建就会失败,就是这样。
For example, your code can be spread across many directories in any kind of layout if you are using 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.
例如,如果您使用Gradle
. 如果您打开Maven
并且不遵循“约定”,您将失去一些时间来更改您的 pom(Maven
构建文件),以便能够理解和处理您的文件夹结构。
E.g.: Java source code must be on src/main/java
folder. 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.
例如:Java 源代码必须在src/main/java
文件夹中。如果您有一个旧项目并且结构是src/java
,抱歉,您不能保证所有 Maven 插件都会按预期运行。
Gradle
does us a favor and puts more features for comparison in a very organized chart. Take a look. https://gradle.org/maven_vs_gradle/
Gradle
帮我们一个忙,并在一个非常有组织的图表中放置了更多的功能进行比较。看一看。https://gradle.org/maven_vs_gradle/
回答by Jamil Hasnine Tamim
Mavenis just a tool that manages and simplifies how you build your project. 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.
Maven只是一个管理和简化项目构建方式的工具。在许多其他事情中(运行测试、管理冲突、文档、模块化),它最有用的目的是自动获取项目需要的依赖项以及这些依赖项的依赖项(传递依赖项)(如果有的话)。依赖项通常只是JAR
包含可重用代码的文件。
So what does this mean. Suppose you want to use a library such as OpenCSV
to generate a CSV
file in your application. 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. When you move your project to another PC, copy all the JAR's or it won't work. Maven way: insert something like this in a file named pom.xml
:
那么这是什么意思。假设您想使用一个库,例如 在您的应用程序中OpenCSV
生成一个CSV
文件。非 Maven 方式:谷歌搜索库,检查它是否需要其他库,如果需要,下载它们,将它们放在项目的类路径中然后构建。当您将项目移动到另一台 PC 时,请复制所有 JAR,否则将无法工作。Maven 方式:在名为的文件中插入类似的内容pom.xml
:
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>
Done. Mavenwill do the rest for you when you build (download to local cache if not already existing, fetch dependencies, javadoc etc). 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.
完毕。Maven将在您构建时为您完成剩下的工作(如果尚不存在,则下载到本地缓存,获取依赖项,javadoc 等)。即使你将你的项目复制到另一台没有 JAR 的 PC,删除JAR's
等,如果 Maven 存在,它会在你构建时自动重新下载它们。
For more: Link
更多:链接