Exercise-4
5. Practical Exercise: Build and Run a Java Application with Maven, Migrate the Same Application to Gradle
Part 1: Build and Run a Java Application Using Maven
Step 1: Create a Maven Project
Open a terminal and run:
Then, navigate into the project folder:
After running the command, your project will have this structure:
OUTPUT:
Modify src/main/java/com/example/App.java
:
Modify pom.xml
:
Compile and Build the JAR
Expected Output:
Part 2: Migrate the Same Application to Gradle
Step 1: Create a New Gradle Project
Navigate outside your Maven project folder and run:
Step 2: Copy Java Files from Maven to Gradle
Manually copy the src
folder from myapp
(Maven project) to myapp-gradle
.
(Note: replace myapp-gradle src folder with myapp src folder)
Step 3: Modify build.gradle.kts
(Kotlin DSL)
Edit build.gradle.kts
:
Step 4: Build and Run the Application with Gradle
Compile and Build the JAR
Run the Application
Expected Output:
Summary
Step | Maven Command | Gradle Command |
---|---|---|
Create Project | mvn archetype:generate | gradle init |
Compile Code | mvn compile | gradle build |
Run Tests | mvn test | gradle test |
Build JAR | mvn package | gradle jar |
Run Application | java -jar target/myapp-1.0-SNAPSHOT.jar | gradle run |
Clear explanation! I like that the final Gradle build still prints “Hello from Maven” — a fun detail. I’ve gone through a similar migration process in a team setting, and build speed was a huge advantage. Curious to know if you’ll also touch on integrating Gradle with CI/CD pipelines.
ReplyDeleteI’ve mostly worked with Maven, but seeing the side-by-side summary with Gradle commands makes it feel less intimidating. Have you run into any build performance differences after the migration? I faced a similar learning curve during a cloud migration project and small tweaks really improved speed.
ReplyDelete