0% found this document useful (0 votes)
7 views13 pages

Build Java App with Maven & Gradle

This document outlines a practical exercise for a DevOps course, focusing on building and running a Java application using Maven and migrating it to Gradle. It includes step-by-step instructions for creating a Maven project, editing the pom.xml file, and writing Java code in the App.java file. The provided code demonstrates a simple application that prints a greeting and calculates the sum of two integers.

Uploaded by

Bhavana Biradar
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)
7 views13 pages

Build Java App with Maven & Gradle

This document outlines a practical exercise for a DevOps course, focusing on building and running a Java application using Maven and migrating it to Gradle. It includes step-by-step instructions for creating a Maven project, editing the pom.xml file, and writing Java code in the App.java file. The provided code demonstrates a simple application that prints a greeting and calculates the sum of two integers.

Uploaded by

Bhavana Biradar
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

Prof. K.

Navya
Assistant Professor
Department of ISE

LAB PROGRAM -4

Course Name : DEVOPS


Course Code:BCSL657D
Practical Exercise: Build and Run a Java Application with
Maven, Migrate the Same Application to Gradle
Step 1: Creating a Maven Project
You can create a Maven project using the mvn command (or through
your IDE, as mentioned earlier). But here, I’ll give you the
essential [Link] and Java code.

CommandLine:

mvn archetype:generate -DgroupId=[Link] -DartifactId=maven-


example -DarchetypeArtifactId=maven-archetype-quickstart -
DinteractiveMode=false
Step 2: Open The [Link] File

• You can manually navigate the project folder named call maven-
example and open the file [Link] and copy the below code and paste it
then save it.
Step 3: Open Java Code ([Link]) File

• Open a file [Link] inside the src/main/java/com/example/ directory.


• After opening the [Link] copy the below code and paste it in that file
then save it.
package [Link];
public class App {
public static void main(String[] args) {
[Link]("Hello, Maven");
[Link]("This is the simple realworld example....");
int a = 5;
int b = 10;
[Link]("Sum of " + a + " and " + b + " is " + sum(a, b));
}
public static int sum(int x, int y) {
return x + y;
}
}

Common questions

Powered by AI

The App.java file demonstrates several basic Java programming concepts, including package declarations, the main method for execution, printing to the console, and defining and calling methods. It showcases variable declaration, initialization, and method usage through the 'sum' method which adds two integers. When executed, the output includes 'Hello, Maven', 'This is the simple realworld example....', and 'Sum of 5 and 10 is 15', explaining basic input/output and function calls in Java .

To add a new dependency in a Maven project, include the dependency information, such as groupId, artifactId, and version, in the pom.xml file under the <dependencies> section. This addition allows Maven to automatically download and include the specified library from its repository into the project during the build process. It improves efficiency by automatically managing transitive dependencies, ensuring all necessary libraries are fetched and versions are aligned, thereby preventing conflicts and ensuring smooth builds .

The 'sum' method in App.java represents fundamental principles of code reuse and maintainability by encapsulating the logic for addition in a single method. This approach avoids repetition of code and allows the same code to be reused for summing integers in different parts of the program. It enhances maintainability by focusing changes to the summation logic within one method, rather than scattered code, reducing errors and promoting cleaner and more readable code architecture .

Migrating a Java application from Maven to Gradle can enhance deployment by leveraging Gradle's more flexible and concise syntax, allowing developers to write custom build scripts more easily. Gradle provides incremental builds, which can significantly reduce build times, and enhanced dependency management capabilities. Additionally, Gradle’s build cache optimizes repeated tasks and parallel execution features improve performance, collectively improving the efficiency and reliability of continuous deliveries in DevOps environments .

The development cycle in traditional Java development often involves manual dependency management and builds, leading to longer cycles, potential inconsistencies, and integration issues. In contrast, a DevOps approach using tools like Maven streamlines the process by automating builds, testing, and deployment with integrated dependency management and continuous integration support. This approach enhances collaboration, reduces human errors, and accelerates feedback and delivery cycles, which is critical in rapidly evolving development environments .

Maven offers several advantages over traditional project management methods: it simplifies dependency management by allowing automatic downloading of project dependencies from a central repository, standardizes project structures across teams, integrates with a range of development tools, and provides powerful scripting and automation capabilities for build management. It supports seamless integration with tools for testing, documentation, and reporting, facilitating continuous integration practices essential in DevOps .

The pom.xml file acts as the cornerstone of a Maven project, defining the project's dependencies, build directory, plugins, goals, and other configurations. It allows automatic management of dependencies, reducing the potential for version conflicts. The pom.xml facilitates the build lifecycle in Maven, handling compilation, testing, packing, and deployment processes through a declarative approach, ensuring consistency and reliability in builds across different environments .

To create and configure a Maven project, follow these steps: First, use the mvn command to generate a Maven project with the appropriate groupId and artifactId using the 'maven-archetype-quickstart' archetype. Second, manually navigate to the project folder, open the pom.xml file, and paste the provided configuration code. Third, create and edit the App.java file under src/main/java/com/example/ to include the provided Java code. Save each file after changes .

Maven enhances collaboration and consistency among development teams through its standardized project structure and lifecycle. By promoting a uniform setup, Maven eliminates environment discrepancies, thereby reducing 'works on my machine' issues. It facilitates easy onboarding of team members, ensuring that everyone uses the same project conventions and dependencies. Additionally, by automating builds, testing, and reporting, it alleviates many repetitive, manual tasks, enabling teams to focus on development rather than configuration management .

The command 'mvn archetype:generate' initiates the generation of a new Maven project based on a specified archetype, serving as a project template. It facilitates rapid project setup by providing a pre-defined structure and default configurations. Customization options include defining groupId, artifactId, version, package name, and choosing different archetypes to cater to specific project requirements, enabling tailored setups while maintaining consistency in configurations across different environments .

You might also like