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 |
Comments
Post a Comment