0% found this document useful (0 votes)
95 views

Practical Exercise_ Build and Run a Java Application With Maven, Migrate the Same Application to Gradle-SessionHandouts

This document provides a step-by-step guide to create and build a Java application using Maven in IntelliJ IDEA, including updating the pom.xml file with necessary plugins and running the application. It also details the process of migrating the Maven project to Gradle, including initializing Gradle, updating the build.gradle file, and running the Gradle project. The conclusion confirms the successful creation, packaging, and migration of the project.

Uploaded by

Shruthi Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Practical Exercise_ Build and Run a Java Application With Maven, Migrate the Same Application to Gradle-SessionHandouts

This document provides a step-by-step guide to create and build a Java application using Maven in IntelliJ IDEA, including updating the pom.xml file with necessary plugins and running the application. It also details the process of migrating the Maven project to Gradle, including initializing Gradle, updating the build.gradle file, and running the Gradle project. The conclusion confirms the successful creation, packaging, and migration of the project.

Uploaded by

Shruthi Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical Exercise: Build and Run a Java Application with Maven,

Migrate the Same Application to Gradle

🎯 Part 1: Create and Build a Java Application with Maven


🛠️ Step 1: Create a Maven Project in IntelliJ IDEA
1. Open IntelliJ IDEA
Launch IntelliJ IDEA and click on File → New → Project. 🌟
2. Select Maven
In the New Project window, choose Maven from the options on the left.
Check Create from archetype and select maven-archetype-quickstart.

Click Next. 👨‍💻


3. Enter Project Details
GroupId: com.example
ArtifactId: MVNGRDLDEMO
Click Next and then Finish. ✨
4. Wait for IntelliJ to Load Dependencies
IntelliJ will automatically download the Maven dependencies, so just relax for a moment. 😌

📝 Step 2: Update pom.xml to Add Build Plugin

To compile and package your project into a .jar file, you need to add the Maven Compiler and Jar plugins. 🔧
1. Open the pom.xml file. 📄
2. Add the following inside the <project> tag:

1 <build>
2 <plugins>
3 <!-- Compiler Plugin -->
4 <plugin>
5 <groupId>org.apache.maven.plugins</groupId>
6 <artifactId>maven-compiler-plugin</artifactId>
7 <version>3.8.1</version>
8 <configuration>
9 <source>1.8</source>
10 <target>1.8</target>
11 </configuration>
12 </plugin>
13
14 <!-- Jar Plugin -->
15 <plugin>
16 <groupId>org.apache.maven.plugins</groupId>
17 <artifactId>maven-jar-plugin</artifactId>
18 <version>3.2.0</version>
19 <configuration>
20 <archive>
21 <manifest>
22 <mainClass>com.example.App</mainClass>
23 </manifest>
24 </archive>
25 </configuration>
26 </plugin>
27 </plugins>
28 </build>
29

🔨 Step 3: Build and Run the Maven Project


1. Open IntelliJ IDEA Terminal
Press Alt + F12 to open the terminal. 🖥️
2. Compile and Package the Project
Run the following commands to build the project:

1 mvn clean compile


2 mvn package
3

3. Locate the JAR File


After running the above, your .jar file will be located at:

1 D:\Idea Projects\MVNGRDLDEMO\target\MVNGRDLDEMO-1.0-SNAPSHOT.jar
2

4. Run the JAR File


To run the generated JAR file, use:

1 java -jar target\MVNGRDLDEMO-1.0-SNAPSHOT.jar


2

🚀 Part 2: Migrate Maven Project to Gradle


🔄 Step 1: Initialize Gradle in Your Project
1. Open Terminal in IntelliJ IDEA
Make sure you're in the project directory:

1 cd "D:\Idea Projects\MVNGRDLDEMO"
2
2. Run Gradle Init Command
Execute the following command to migrate your Maven project to Gradle:

1 gradle init --type pom


2

This command will convert your Maven pom.xml into a Gradle build.gradle file. 🏗️

🛠️ Step 2: Review and Update build.gradle

1. Open build.gradle in IntelliJ IDEA.


2. Ensure the following configurations are correct:

1 plugins {
2 id 'java'
3 }
4
5 group = 'com.example'
6 version = '1.0-SNAPSHOT'
7
8 repositories {
9 mavenCentral()
10 }
11
12 dependencies {
13 testImplementation 'junit:junit:4.13.2'
14 }
15
16 jar {
17 manifest {
18 attributes(
19 'Main-Class': 'com.example.App'
20 )
21 }
22 }
23

🔨 Step 3: Build and Run the Gradle Project


1. Clean and Build the Project
To clean and build your Gradle project, run:

1 gradle clean build


2

2. Run the Generated JAR File


Now, run the generated JAR file using:

1 java -jar build/libs/MVNGRDLDEMO-1.0-SNAPSHOT.jar


2

🎉 Conclusion
Congratulations! You have successfully:
1. Created a Maven project in IntelliJ IDEA. 🎉
2. Built and packaged it into a JAR file using Maven. 🛠️
3. Migrated the project from Maven to Gradle. 🔄
4. Built and ran the project using Gradle. 🚀

You might also like