lab4

 Same Application to Gradle

Part A: Build and Run a Java Application with Maven

1. Create the Maven Project

1. Open your Terminal.

2. Generate the Maven Project using the Quickstart Archetype:

Type the following command and press Enter:

mvn archetype:generate -DgroupId=com.example -DartifactId=HelloMaven -

DarchetypeArtifactId=maven-archetype-quickstart

DinteractiveMode=false -

o What it does: This command creates a new Maven project with the group ID

com.example and the artifact ID HelloMaven. The archetype sets up a basic Java

application, including a sample test.

o Expected Output:

Maven will display messages as it downloads dependencies and generates the

project files.

o Screenshot Tip:

Capture the terminal output showing the successful project generation.

3. Change Directory into the Newly Created Project:

4. cd HelloMaven

o Screenshot Tip:

Capture the output of the pwd or ls command to show the project structure.

2. Explore the Maven Project Structure

Your project directory should have a structure similar to this:

HelloMaven/

├── pom.xml

└── src

├── main

│ └── java

└── com

└── example

└── App.java

└── test

└── java

└── com

└── example

└── AppTest.java

• pom.xml: The Maven configuration file (POM) that defines your project’s

coordinates, dependencies, and plugins.

• src/main/java: Contains your application’s source code.

• src/test/java: Contains unit tests.

3. Build the Maven Project

1. Compile and Package the Application:

2. mvn package

o What it does: This command compiles the source code, runs tests, and

packages your application into a JAR file (located in the target directory).

o Expected Output:

You should see a “BUILD SUCCESS” message along with information on the

created JAR file.

4. Run the Maven Application

1. Run the Application Using the JAR File:

Execute the following command:

java -cp target/HelloMaven-1.0-SNAPSHOT.jar com.example.App

o What it does:

This command runs the com.example.App class from the JAR file generated in

the previous step.

o Expected Output:

The output should display:

o Hello World!

(This is the default message printed by the generated App.java.)

o Screenshot Tip:

Capture the terminal output showing “Hello World!” as proof that the

application ran successfully.

Part B: Migrate the Application to Gradle

In this part, you will create a Gradle project that contains the same Java application code, then

build and run it using Gradle.

1. Create a New Gradle Project

1. Open a New Terminal Window or Navigate Back to Your Workspace.

2. Create a New Directory for the Gradle Project:

3. mkdir HelloMavenGradle

4. cd HelloMavenGradle

o What it does:

Creates and navigates into a folder named HelloMavenGradle for your new

Gradle project.

5. Initialize the Gradle Project Using the Java Application Type:

6. gradle init --type java-application

o What it does:

Gradle will generate a basic Java application project with a default project

structure.

o Expected Output:

You will see interactive prompts or a confirmation message stating that the

project has been generated.

2. Adjust the Gradle Project to Use the Same Code

The Gradle project structure will look similar to this:

HelloMavenGradle/

├── build.gradle

├── settings.gradle

└── src

├── main

│ └── java

└── App.java

└── test

└── java

└── AppTest.java

A. Replace or Update the Source Code:

1. Copy the Application Code from the Maven Project:

o Open

the

Maven

project

(HelloMaven/src/main/java/com/example/App.java).

directory

o Copy its contents (which should print “Hello World!”) into the Gradle project.

2. Ensure the Package Declaration Matches:

o In the Maven project, the class is in the package com.example.

o If the Gradle project’s default App.java is not in this package, create the proper

directory structure:

o mkdir -p src/main/java/com/example

o Move (or copy) the App.java file into the com/example folder:

o mv src/main/java/App.java src/main/java/com/example/

o Do the same for the test file if you wish to migrate tests.

B. Update the Gradle Build Script

1. Open the build.gradle File in Your Editor:

2. nano build.gradle

3. Modify the application Block to Set the Correct Main Class:

Change or add the following in the file:

application {

// Update the mainClass to reflect the package structure mainClass =

'com.example.App'

}

3. Build and Run the Gradle Application

1. Build the Project:

2. gradle build

o What it

does:

Compiles the source code, runs tests, and packages the application.

o Expected

Output:

Look for a “BUILD SUCCESSFUL” message.

3. Run the Application Using Gradle:

4. gradle run

o What it

does: Uses the application plugin to run the main class defined

in the build.gradle file.

o Expected

Output:

The output should display:

o Hello World! 

Comments

Popular posts from this blog

lab2

lab1