Practical Exercise_ Build and Run a Java Application With Maven, Migrate the Same Application to Gradle-SessionHandouts
Practical Exercise_ Build and Run a Java Application With Maven, Migrate the Same Application to Gradle-SessionHandouts
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
1 D:\Idea Projects\MVNGRDLDEMO\target\MVNGRDLDEMO-1.0-SNAPSHOT.jar
2
1 cd "D:\Idea Projects\MVNGRDLDEMO"
2
2. Run Gradle Init Command
Execute the following command to migrate your Maven project to Gradle:
This command will convert your Maven pom.xml into a Gradle build.gradle file. 🏗️
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
🎉 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. 🚀