另一个针对Java的构建工具能给我带来什么?

如果你使用Gradle而不是其他工具,为什么?


当前回答

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.

其他回答

这不是我的答案,但它确实引起了我的共鸣。这是来自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.

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"
}

我自己并没有因为愤怒而使用Gradle(到目前为止只是一个玩具项目)[作者的意思是他们到目前为止只在一个玩具项目中使用过Gradle,并不是说Gradle是一个玩具项目-见评论],但我想说的是,人们考虑使用它的原因是Ant和Maven的挫折。

在我的经验中,Ant通常是只写的(是的,我知道可以编写漂亮的模块化、优雅的构建,但事实是大多数人不这样做)。对于任何重要的项目来说,它都是令人费解的,并且需要非常小心地确保复杂的构建是真正可移植的。它的命令性可以导致在构建之间复制配置(尽管宏在这里可以提供帮助)。

Maven采取相反的方法,并期望您完全集成到Maven生命周期中。有经验的Ant用户会发现这一点尤其不和谐,因为Maven删除了您在Ant中拥有的许多自由。例如,有一个Sonatype博客列举了许多对Maven的批评和他们的回应。

The Maven plugin mechanism allows for very powerful build configurations, and the inheritance model means you can define a small set of parent POMs encapsulating your build configurations for the whole enterprise and individual projects can inherit those configurations, leaving them lightweight. Maven configuration is very verbose (though Maven 3 promises to address this), and if you want to do anything that is "not the Maven way" you have to write a plugin or use the hacky Ant integration. Note I happen to like writing Maven plugins but appreciate that many will object to the effort involved.

Gradle有望达到Ant和Maven之间的最佳点。它使用Ivy的方法进行依赖项解析。它允许约定优于配置,但也将Ant任务作为第一类公民。它还明智地允许您使用现有的Maven/Ivy存储库。

So if you've hit and got stuck with any of the Ant/Maven pain points, it is probably worth trying Gradle out, though in my opinion it remains to be seen if you wouldn't just be trading known problems for unknown ones. The proof of the pudding is in the eating though so I would reserve judgment until the product is a little more mature and others have ironed out any kinks (they call it bleeding edge for a reason). I'll still be using it in my toy projects though, It's always good to be aware of the options.

管理本地构建也容易得多。Ant和Maven实际上只支持java。存在一些Maven插件,试图处理一些本地项目,但它们没有有效地完成工作。可以编写Ant任务来编译本地项目,但它们太复杂和笨拙了。

我们用JNI和许多其他原生位来做Java。Gradle大大简化了我们的Ant混乱。当我们开始将依赖管理引入本地项目时,它很混乱。我们让Maven来做这件事,但等效的Gradle代码只是Maven所需要的一小部分,人们可以阅读和理解它,而不必成为Maven专家。

Gradle可以用于许多目的——它是比Ant更好的瑞士军刀——但它特别专注于多项目构建。

首先,Gradle是一个依赖编程工具,这也意味着它是一个编程工具。使用Gradle,你可以在你的设置中执行任何随机任务,Gradle会确保所有声明的依赖都正确和及时地执行。您的代码可以以任何类型的布局(树形、平面、分散等)分布在许多目录中。

Gradle有两个不同的阶段:评估和执行。基本上,在评估期间,Gradle会在它应该查找的目录中查找和评估构建脚本。在执行期间,Gradle将执行在评估期间加载的任务,并考虑到任务的相互依赖性。

在这些依赖编程特性之上,Gradle通过与Apache Ivy集成增加了项目和JAR依赖特性。如你所知,Ivy是一个比Maven强大得多、不那么固执己见的依赖管理工具。

Gradle检测项目之间以及项目与jar之间的依赖关系。Gradle与Maven存储库(下载和上传)一起工作,比如iBiblio或你自己的存储库,但也支持你可能拥有的其他类型的存储库基础设施。

在多项目构建中,Gradle既能适应,又能适应构建的结构和架构。您不必像使用Maven那样根据构建工具调整结构或体系结构。

Gradle非常努力地不妨碍你,而Maven几乎从不这样做。惯例很好,灵活性也很好。Gradle提供了比Maven更多的特性,但最重要的是,在许多情况下,Gradle将为您提供从Maven过渡到Maven的轻松路径。