Show List
How to convert Maven Project to Gradle Project
In this demo we are going to convert a sample Maven project to Gradle project.
We are going to use Spring Boot rest Web Service maven project that we created in the earlier chapter using-swagger-ui-with-spring-boot-rest-web-service for the demo. Clone/Download the application source code provided in the tutorial and open in IDE. I am using IntelliJ Idea IDE for the demo.
Here is the application with Maven project structure:
Gradle Init
Run "gradle init" command from the project root folder. I the image below I have selected Groovy as the language for gradle.
As the build is completed, you should see a message to load the gradle project. Click on the link to load. If you do not see the message, then close and reopen the project in IDE to reload.
PS C:\Users\mail2\Downloads\swagger-with-Spring-Boot-master> gradle init Found a Maven build. Generate a Gradle build from this? (default: yes) [yes, no] yes Select build script DSL: 1: Groovy 2: Kotlin Enter selection (default: Groovy) [1..2] 1 Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] yes > Task :init Maven to Gradle conversion is an incubating feature. Get more help with your project: https://docs.gradle.org/7.5.1/userguide/migrating_from_maven.html BUILD SUCCESSFUL in 3m 1s 2 actionable tasks: 2 executed
As the project is reloaded, you should see the Gradle project structure:
Generated build.gradle
Dependencies, repositories, tasks and configurations get created in build.gradle based on the information present in pom.xml:
/*
* This file was generated by the Gradle 'init' task.
*
* This project uses @Incubating APIs which are subject to change.
*/
plugins {
id 'java'
id 'maven-publish'
}
repositories {
mavenLocal()
maven {
url = uri('https://repo.maven.apache.org/maven2/')
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.7.3'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.4'
implementation 'org.springframework.boot:spring-boot-starter-web:2.7.3'
runtimeOnly 'com.h2database:h2:2.1.214'
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.7.3'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
description = 'demo'
java.sourceCompatibility = JavaVersion.VERSION_1_8
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
Running the application
The application can now be run as usual gradle application:
2:50:38 PM: Executing task ':DemoApplication.main()'... Starting Gradle Daemon... Gradle Daemon started in 2 s 167 ms > Task :compileJava > Task :processResources > Task :classes > Task :DemoApplication.main() . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.3) 2022-12-19 14:50:54.283 INFO 13276 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 11.0.15 on sm15 with PID 13276 (C:\Users\mail2\Downloads\swagger-with-Spring-Boot-master\build\classes\java\main started by mail2 in C:\Users\mail2\Downloads\swagger-with-Spring-Boot-master) 2022-12-19 14:50:54.287 INFO 13276 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default" 2022-12-19 14:50:55.201 INFO 13276 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2022-12-19 14:50:55.261 INFO 13276 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 50 ms. Found 1 JPA repository interfaces. 2022-12-19 14:50:56.365 INFO 13276 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-12-19 14:50:56.366 INFO 13276 --- [ main] o.a.catalina.core.AprLifecycleListener : An older version [1.2.28] of the Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.30] 2022-12-19 14:50:56.366 INFO 13276 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded Apache Tomcat Native library [1.2.28] using APR version [1.7.0]. 2022-12-19 14:50:56.366 INFO 13276 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true]. 2022-12-19 14:50:56.367 INFO 13276 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true] 2022-12-19 14:50:56.370 INFO 13276 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.1.1k 25 Mar 2021] 2022-12-19 14:50:56.382 INFO 13276 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-12-19 14:50:56.382 INFO 13276 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65] 2022-12-19 14:50:56.577 INFO 13276 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-12-19 14:50:56.577 INFO 13276 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2223 ms 2022-12-19 14:50:56.880 INFO 13276 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2022-12-19 14:50:57.134 INFO 13276 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2022-12-19 14:50:57.210 INFO 13276 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2022-12-19 14:50:57.285 INFO 13276 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.10.Final 2022-12-19 14:50:57.518 INFO 13276 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final} 2022-12-19 14:50:57.689 INFO 13276 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect Hibernate: drop table if exists student CASCADE Hibernate: create table student (student_id integer generated by default as identity, grade varchar(255), name varchar(255), primary key (student_id)) 2022-12-19 14:50:58.385 INFO 13276 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] 2022-12-19 14:50:58.397 INFO 13276 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2022-12-19 14:50:58.942 WARN 13276 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2022-12-19 14:50:59.860 INFO 13276 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-12-19 14:50:59.873 INFO 13276 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 6.108 seconds (JVM running for 6.62) Hibernate: insert into student (student_id, grade, name) values (default, ?, ?) Hibernate: insert into student (student_id, grade, name) values (default, ?, ?) Hibernate: insert into student (student_id, grade, name) values (default, ?, ?) Hibernate: insert into student (student_id, grade, name) values (default, ?, ?)
Similar to the old Maven application, Swagger UI can be accessed from http://localhost:8080/swagger-ui/index.html
Leave a Comment