gradle适配maven项目_手把手,一步步教你将Maven项目迁移到Gradle

本文详细记录了一个Java混编Kotlin的Maven项目迁移到Gradle的过程,包括如何使用Gradle自动生成配置、转换Kotlin DSL、引入Spring Boot Bom依赖管理、设置Nexus私服、打包FatJar、编译Kotlin和Java代码、Spock单元测试以及Lombok和QueryDSL的处理。同时,文章探讨了Gradle的并行编译和缓存,以及迁移后的构建速度对比。
摘要由CSDN通过智能技术生成

最近嫌弃项目编译比较慢,又听说Gradle构建速度会比Maven快很多(官方的说法是至少2倍),于是萌生了将已有项目的Maven编译,迁移到Gradle编译的想法。当然,万事开头难,所以我决定先从一个项目开始试水。下面先简单的说下这个项目的架构概况:该项目是一个Java项目,混编了一部分Kotlin代码,并且使用了Spock作为单元测试框架。

该项目是一个Maven多模块项目,父模块定义了节点,统一管理了所有子模块的依赖,子模块中无需再定义依赖的版本号。

该项目有API模块,基于Spring Boot编写,需要利用Spring Boot Maven Plugin生成可执行的FatJar。

项目使用了lombok,需要基于lombok的注解生成器生成最终代码。

项目使用了QueryDSL,需要使用QueryDSL的插件,将数据库Entity类编译为QClass,并在源码中引用。

该项目需要发布到Nexus私服

该项目应该涵盖了大部分Maven项目需要用到的编译配置,下面,我就通过这个项目的实战经验。带大家一步步将项目从Maven构建,迁移到全新的Gradle构建。

基于以上的概况,要将该项目从使用Maven构建,迁移到Gradle构建,就需要在Gradle中实现以上同等的逻辑。那么,就开始干吧~

基于POM,自动生成Gradle配置

Gradle官方非常贴心的内置了插件,可以一键将Maven POM转换为Gradle配置。只需要在项目目录内执行:

gradle init

然后,按照提示,将该Maven项目转换为一个Gradle项目即可。Gradle会自动为我们生成根项目的build.gradle、settings.gradle和gradle wrapper文件夹,当然,还有每个子项目的build.gradle文件(太强大了,笔芯Gradle♥️)

然鹅,光是这样当然是不够的,自动生成的Gradle配置是无法通过编译的...我们还需要继续按照原有的编译配置继续调整Gradle配置文件。

另外,听说Gradle的Kotlin DSL对IDE更友好,而且是类型安全的。因此,我决定把自动生成的Groovy DSL改写为Kotlin DSL(自动转换工具目前没有提供生成Kotlin DSL的选项,遗憾)

DSL的转换不是必须的,不过这里也简单说下,过程不是很复杂,基本就是把单引号'换成",另外把省略括号的地方补上括号即可。其他写法差异比较大的,基本Google就可以解决,这一步难度不大。

引入Spring Boot Bom依赖版本管理

众所周知,基于Spring Boot的项目,一定离不开Spring Boot Bom依赖管理。这个spring boot的bom文件内定义了很多依赖的版本,例如spring-boot-jpa、spring-boot-web等依赖的版本。这样,我们在开发时,就可以减少依赖冲突,也不用自己再额外定义版本号了。

在使用Maven构建时,引入Spring Boot Bom有两种做法,第一种就是官方教程中常用的,将它作为模块的Parent:

org.springframework.boot

spring-boot-starter-parent

2.2.6.RELEASE

但是,这种做法必须将该bom作为模块的parent,有一定局限性。因此,我一般是将他作为依赖管理引入:

org.springframework.boot

spring-boot-dependencies

2.2.6.RELEASE

pom

import

同时,因为项目也使用了Spring Cloud组件,我也在中引入了spring-cloud-dependenciesbom文件。

下面,就是要解决如何在Gradle中应用上面这套,我们在Maven-Spring Boot项

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learn how to use Gradle's powerful dependency management through extensive code samples, and discover how to define, customize, and deploy dependencies About This Book Be in total control of your dependencies Deploy your artifacts to repositories with Gradle Learn through code snippets and real-life examples Who This Book Is For If you work on Java projects, use Gradle as a build automation tool, and you use dependencies in your project, this is the book for you. Additionally, if you want to deploy your project artifacts as dependencies for other developers using Gradle, you've found the right book. In Detail Gradle is the next generation in build automation. It allows you to define dependencies for your project in a clear way and also customize how they are resolved to suit your needs. It offers fine-grained control over how to publish your artifacts to Maven and Ivy repositories. Gradle Dependency Management defines dependencies for your Java-based project and customizes how they are resolved. You will learn how to configure the publication of artifacts to different repositories. Packed with plenty of code samples, you will understand how to define the repositories that contain dependencies. Following this, you will learn how to customize the dependency resolution process in Gradle. Table of Contents Chapter 1. Defining Dependencies Chapter 2. Working with Repositories Chapter 3. Resolving Dependencies Chapter 4. Publishing Artifacts Chapter 5. Publishing to a Maven Repository Chapter 6. Publishing to Bintray Chapter 7. Publishing to an Ivy Repository
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值