从 Maven 2/3 迁移到 Gradle
我一直在研究 Gradle,看起来很有趣。我认为能够用 XML 以外的任何其他语言编写脚本非常酷,而且我不清楚多语言 Maven POM 文件是否仍然是一个可行的选择。
我正在开发一个使用 Maven 2.2.1 作为构建工具的项目。这个项目:
- 是多模块
- 使用(也是我们自己的) 插件
- 依赖于 Artifactory 代理存储库
有没有从 Maven 迁移到 Gradle 的经验?陷阱、痛点、极端情况?任何形式的体验都是非常受欢迎的。
I have been looking into Gradle and looks pretty interesting. I think being able to write your scripts in any other language than XML is pretty cool, and it is not clear to me whether polyglot Maven POM files are still a feasible option.
I am working on a project that uses Maven 2.2.1 as build tool. This project:
- is multi-module
- uses (also our own) plugins
- relies on an Artifactory proxy repository
Are there any experiences out there on migrations from Maven to Gradle? Gotchas, pain points, corner cases? Any sort of experience is very welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从 Maven 到 Gradle 的迁移并不像从 Ant 到 Maven 那样容易(至少目前是这样)。您可以轻松地重用 Ant 脚本,并使它们成为 Gradle 构建中的一等公民。 Gradle 路线图上有一项针对 还有 Maven 构建的深度导入。
到目前为止,我已将两个企业 Maven 构建迁移到 Gradle。它们都是使用标准 Maven 插件的多模块项目。我基本上重写了 Gradle 的构建方式,这至少需要一些关于 Gradle 的知识。根据我的经验,您也可以轻松地在 Gradle 中运行相同的构建。 Gradle 并没有真正限制你,而且相当灵活。在此过程中,您可能会发现自己必须编写一个尚不存在的自定义插件,具体取决于您使用的 Maven 插件。然而,已经有了大量的插件。到目前为止,我还没有遇到真正的障碍。尽管 Gradle 文档非常好,您可能会发现自己阅读了大量 Gradle 论坛帖子来找到问题的解决方案。一些标准 Maven 功能不受开箱即用的支持,例如
provided
范围或 就地战争。然而,有一些简单的方法可以解决这个问题。我没有使用过 Artifactory 存储库。我处理的是 Nexus 存储库。据我所知,Gradle 的人对 Artifactory 有很好的支持。 编辑: JFrog 提供了 Artifactory Gradle 插件。一个好的开始方法是使用迁移工具 Maven2Gradle,它可以让您从 Maven 构建生成 Gradle 脚本。就我个人而言,我还没有使用它。我并行开发了 Gradle 构建和 Maven 构建,这没有造成任何麻烦。 Maven 将其
输出在
target
下,Gradle 在build
下。确保您的团队为变革做好准备。让他们尝试 Gradle 构建并熟悉该工具。完全迁移后,您将对构建的可维护性和可扩展性感到非常满意。添加自定义构建逻辑非常容易,您将会庆幸自己离开了 XML 领域。在性能方面,您也不会退后一步。增量构建功能很好地完成了它的工作。
The migration from Maven to Gradle isn't as easy as from Ant to Maven (at the least at the moment). You can easily reuse Ant scripts and make them first class citizens in your Gradle build. There's a task on the Gradle roadmap for deep import of Maven builds as well.
So far I migrated two enterprise Maven builds to Gradle. Both of them were multi-module projects using standard Maven plugins. I basically rewrote the builds the Gradle way which requires at least some knowledge about Gradle. Based on my experience you can easily get the same build running in Gradle as well. Gradle doesn't really box you in here and is fairly flexible. Along the way you might find yourself having to write a custom plugin that doesn't exist yet depending on what Maven plugins you are using. However, there's already a wide breath of plugins out there. So far I haven't run into a real roadblock yet. Even though the Gradle documentation is pretty good you might find yourself reading a lot of Gradle forum posts to find the solution to one of your problems. Some of the standard Maven features are not supported out-of-the-box e.g. a
provided
scope or WAR inplace. However, there're easy ways around it. I haven't used Artifactory repositories. The ones I dealt with were Nexus repositories. As far as I know the Gradle guys have good support for Artifactory though. Edit: JFrog provides a Artifactory Gradle plugin.A good way to start is to use the migration tool Maven2Gradle which let's you generate a Gradle script from your Maven build. Personally, I didn't use it yet. I developed the Gradle build side-by-side with the Maven build which didn't cause any trouble. Maven put its
output under
target
, Gradle underbuild
. Make sure you prepare your team for the change. Let them try out the Gradle build and get familiar with the tool.Once you are fully migrated you'll be very happy about the maintainability and extensibility of your build. It's very easy to add custom build logic and you're going to be grateful that you left XML-land. In terms of performance you are not going to make a step back as well. The incremental build feature does its job very well.
您可能还想阅读这篇文章 我将 Maven 项目移植到 gradle 的经验。
You may also want to read through this write-up of my experience porting a maven project to gradle.
在 pom.xml 所在的目录中,只需运行以下命令:
gradle init --type pom
这应该将您的 maven pom.xml 转换为 build.gradle
In the directory where you have pom.xml just run the below command:
gradle init --type pom
This should convert your maven pom.xml to build.gradle
如果您希望构建输出位于
'target'
下而不是下,您可以随时将 gradle 下的
像 Maven 一样:buildDir
更改为'target'
>'build'You can always change the
buildDir
to be'target'
under gradle if you want the build output to go under'target'
instead of'build'
like maven:maven2Gradle 似乎已被替换为能够从命令行运行 gradle init (尽管这是一个相当实验性的功能)。从 Maven 项目中进行一些基本的 Gradle 设置似乎可以正常工作。
maven2Gradle seems to have been replaced with being able to run
gradle init
from the command line (though it's a rather experimental feature). It seems to work ok for doing some basic Gradle setup from a Maven project.