0% found this document useful (0 votes)
32 views

Maven Project

Good

Uploaded by

vignangudla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Maven Project

Good

Uploaded by

vignangudla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Maven project

Prerequisites:
- Eclipse IDE,GitHub,Jenkins,Docker,Maven

->open eclipse and go to File > New > project

->Select Maven project from the wizard,then clicl next.

->Ensure Create a simple project(skip archetype selection) is checked,then click Next.

->

->Enter the following:

 Group Id: com.example

 Artifact Id: hello-world

 Version: 1.0-SNAPSHOT

 Packaging: jar

Click Finish.

->Now,a Maven project will be created in Eclipse


Step 2:Write a Hello world program.

->navigate to src/main/java folder inside your project in eclipse.

->create a new package com.example inside src/main/java.

->

- create a new java class App.java inside the com.example package:

Code:

package com.example;
public class App {
public static void main(String[] args) {
System.out.println("Hello, World!");
}}

Step 3: Update pom.xml

->open pom.xml file in the root of your maven project.


->ensure it contains the following dependencies
<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

</dependencies>

->add the required plugins

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>

<artifactId>helloworld-maven</artifactId>

<version>0.0.1-SNAPSHOT</version>

<properties>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

</properties>

<build>

<plugins>

<!-- Maven Compiler Plugin -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>

<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>

<!-- Maven Assembly Plugin -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-assembly-plugin</artifactId>

<version>3.3.0</version>

<configuration>

<descriptorRefs>

<descriptorRef>jar-with-dependencies</descriptorRef>

</descriptorRefs>

<archive>

<manifest>

<mainClass>com.example.App</mainClass>

</manifest>

</archive>

</configuration>

<executions>

<execution>

<id>make-assembly</id>

<phase>package</phase>

<goals>

<goal>single</goal>

</goals>

</execution>

</executions>
</plugin>

</plugins>

</build>

</project>

Step 4 :Build the Maven project Locally

->right-click on the project in eclipse

->select runs as > Maven Build……

->In the Goals fiels,type clean package .


->click run.

-> this will build the project and create a JAR file in the target folder.

Step 5: Push the Maven Project to Github.

->Goto github and click new repository.

->name it hello-world-mavin

->copy the remote url for the repository

->Initialize Git in your project folder using terminal

->navigate to the directory.


git init
git remote add origin https://github.com/your-username/hello-world-maven.git git push -u origin
master
git add .
git commit -m “initial commit”
git push -u origin master

Step 6:Set up Jenkins

->open jenkins in your web browser and log in.


->Install the necessary Jenkins plugins:

 Go to Manage Jenkins > Manage Plugins.

 Install Git, Maven, and Docker Pipeline plugins.

->Go back to the Jenkins dashboard and click on New Item.

->Select Freestyle Project, name it hello-world-maven, and click OK.

->In the Source Code Management section:

 Select Git.

 Add the GitHub URL of your repository.


 If necessary, add your GitHub credentials.

->In the Build section:

 Click on Add Build Step and select Invoke Top-Level Maven Targets.

 Set the Goals to clean package.

->save the configuration.

Step 7:Configure Docker in Jenkins

->Install Docker on the machine where Jenkins is running if it's not installed yet.

 To verify Docker is running, use docker --version and docker ps in your terminal.

->In Jenkins, navigate to Manage Jenkins > Configure System.

->Scroll to the Docker section and ensure the Docker configuration is correct.

->In your Jenkins project (hello-world-maven), add a Post-build Action to create a Docker image

->Go to the Post-build Actions section.

->Add a build step Execute shell and add the following commands to build and run your Docker
image:
docker build -t hello-world-maven-app .

docker run -d -p 8080:8080 hello-world-maven-app

->Save the Jenkins configuration.

Step 8 Create a Dockerfile in Your Project


->In the root of your project, create a file named Dockerfile.

->in that file add

FROM maven:3.6.3-jdk-8 AS build

WORKDIR /app

COPY . .

RUN mvn clean package

FROM openjdk:8-jre

WORKDIR /app

COPY --from=build /app/target/hello-world-1.0-SNAPSHOT.jar /app/hello-world.jar

# Run the application

ENTRYPOINT ["java", "-jar", "/app/hello-world.jar"]

->push the Dockerfile to github.

->in Jenkins in the item,click on build now this will create a docker image.remember while building
always start the docker desktop.

->You can verify that the Docker image has been created by running the following command on the
Jenkins server:
docker images

-> To check if the application is running in a container:


docker ps

->The application should be running on port 8080.


->you can also see the images in the docker desktop.

You might also like