For this part, please refer to Experiment 2: Working with Maven. Follow the steps outlined in Experiment 2 to:
HelloMaven).mvn archetype:generate -DgroupId=com.example -DartifactId=HelloMaven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mvn package.java -cpEnsure you have successfully built and run the HelloMaven project using Maven before proceeding to Part B.
Step 1: Ensure You Are in the Maven Project Directory
Make sure your current directory in the terminal is the root of your Maven project (HelloMaven).
Step 2: Initialize Gradle in the Existing Maven Project
Run the gradle init command. Gradle will detect the existing project structure and attempt to generate appropriate build files. Select Groovy DSL.
gradle init

Step 3: Explore the Updated Project Structure
After running gradle init, new files and directories will be added to your HelloMaven folder, including gradlew, gradlew.bat, settings.gradle, and potentially a build.gradle (or build.gradle.kts) file in the root or within a subfolder like app (depending on how gradle init interpreted your project). Use the tree command again to see the changes:
tree

Step 4: Understand and Update the Generated Gradle Build Script Gradle has generated a build script.
Open the build.gradle file using command:
gedit build.gradle
Replace the entire content of the generated build script with the appropriate example below. The example here is configured to use JUnit 4 and correctly point the application plugin to the com.example.App class. Without application plugin gradle run will not work.
Groovy DSL (build.gradle):
/*
* This file was generated by the Gradle 'init' task.
* Modified for Experiment 4 to enable 'gradle run' for an application.
*/
plugins {
// Use the application plugin to add support for building and running a Java application
id 'application'
id 'java-library'
id 'maven-publish'
}
repositories {
mavenLocal()
maven {
url = uri('<https://repo.maven.apache.org/maven2/>')
}
}
dependencies {
// Use JUnit 4 for testing.
testImplementation libs.junit.junit
// Add any other dependencies from your pom.xml here if needed
// Example: implementation 'com.otherlibrary:other-library:1.0'
}
group = 'com.example'
version = '1.0-SNAPSHOT'
description = 'HelloMaven'
java.sourceCompatibility = JavaVersion.VERSION_1_8 // Or a higher version if needed
// Define the main class for the application.
// This is required by the 'application' plugin to run the application.
application {
mainClass = 'com.example.App' // Make sure this matches the package and class name of your App.java
}
// The publishing block is for publishing the library - not needed for 'gradle run'
// Keep or remove based on your project's full requirements.
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
// You can add other tasks or configurations here as needed
Save the changes to the build script.