另一个针对Java的构建工具能给我带来什么?
如果你使用Gradle而不是其他工具,为什么?
另一个针对Java的构建工具能给我带来什么?
如果你使用Gradle而不是其他工具,为什么?
当前回答
我在一定程度上同意Ed Staub的观点。与maven相比,Gradle无疑更强大,从长远来看也提供了更多的灵活性。
在执行从maven到gradle的评估之后,我们决定在两个问题上坚持使用maven本身 我们遇到gradle(速度比maven慢,代理不工作)。
其他回答
这不是我的答案,但它确实引起了我的共鸣。这是来自ThoughtWorks 2012年10月的“技术雷达”:
Two things have caused fatigue with XML-based build tools like Ant and Maven: too many angry pointy braces and the coarseness of plug-in architectures. While syntax issues can be dealt with through generation, plug-in architectures severely limit the ability for build tools to grow gracefully as projects become more complex. We have come to feel that plug-ins are the wrong level of abstraction, and prefer language-based tools like Gradle and Rake instead, because they offer finer-grained abstractions and more flexibility long term.
管理本地构建也容易得多。Ant和Maven实际上只支持java。存在一些Maven插件,试图处理一些本地项目,但它们没有有效地完成工作。可以编写Ant任务来编译本地项目,但它们太复杂和笨拙了。
我们用JNI和许多其他原生位来做Java。Gradle大大简化了我们的Ant混乱。当我们开始将依赖管理引入本地项目时,它很混乱。我们让Maven来做这件事,但等效的Gradle代码只是Maven所需要的一小部分,人们可以阅读和理解它,而不必成为Maven专家。
这可能有点争议,但Gradle并没有隐藏它是一种成熟的编程语言的事实。
Ant + Ant -contrib本质上是一种图灵完整的编程语言,没有人真的想用它来编程。
Maven试图采取相反的方法,试图完全声明性,并强迫您在需要逻辑时编写和编译插件。它还强加了一个完全不灵活的项目模型。Gradle结合了所有这些工具的优点:
它遵循约定优于配置(类似Maven),但仅限于您想要的程度 它允许您像在Ant中那样编写灵活的自定义任务 它提供了优于Ant和Maven的多模块项目支持 它有一个DSL,使80%的事情变得简单,20%的事情成为可能(不像其他构建工具使80%的事情变得简单,10%的事情成为可能,而10%的事情实际上是不可能的)。
Gradle是我使用过的最具可配置性和灵活性的构建工具。它需要一些前期投资来学习DSL和诸如配置之类的概念,但如果你需要一个没有废话且完全可配置的JVM构建工具,那么它是很难被取代的。
We use Gradle and chose it over Maven and Ant. Ant gave us total flexibility, and Ivy gives better dependency management than Maven, but there isn't great support for multi-project builds. You end up doing a lot of coding to support multi-project builds. Also having some build-by-convention is nice and makes build scripts more concise. With Maven, it takes build by convention too far, and customizing your build process becomes a hack. Also, Maven promotes every project publishing an artifact. Sometimes you have a project split up into subprojects but you want all of the subprojects to be built and versioned together. Not really something Maven is designed for.
With Gradle you can have the flexibility of Ant and build by convention of Maven. For example, it is trivial to extend the conventional build lifecycle with your own task. And you aren't forced to use a convention if you don't want to. Groovy is much nicer to code than XML. In Gradle, you can define dependencies between projects on the local file system without the need to publish artifacts for each to a repository. Finally, Gradle uses Ivy, so it has excellent dependency management. The only real downside for me thus far is the lack of mature Eclipse integration, but the options for Maven aren't really much better.
Gradle很好地结合了Ant和Maven,从这两个框架中取其精华。来自Ant的灵活性和来自Maven的约定优于配置、依赖管理和插件。
因此,如果你想要一个标准的java构建,比如在maven中,但测试任务必须执行一些自定义步骤,它可能如下所示。
build.gradle:
apply plugin:'java'
task test{
doFirst{
ant.copy(toDir:'build/test-classes'){fileset dir:'src/test/extra-resources'}
}
doLast{
...
}
}
最重要的是,它使用groovy语法,它提供了比ant/maven的xml更强大的表达式。
它是Ant的超集——你可以用更好的、类似groovy的语法在gradle中使用所有Ant任务。
ant.copy(file:'a.txt', toDir:"xyz")
or
ant.with{
delete "x.txt"
mkdir "abc"
copy file:"a.txt", toDir: "abc"
}