Maven Project
Maven Project
Prerequisites:
- Eclipse IDE,GitHub,Jenkins,Docker,Maven
->
Version: 1.0-SNAPSHOT
Packaging: jar
Click Finish.
->
Code:
package com.example;
public class App {
public static void main(String[] args) {
System.out.println("Hello, World!");
}}
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<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>
<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>
-> this will build the project and create a JAR file in the target folder.
->name it hello-world-mavin
Select Git.
Click on Add Build Step and select Invoke Top-Level Maven Targets.
->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.
->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
->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 .
WORKDIR /app
COPY . .
FROM openjdk:8-jre
WORKDIR /app
->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