To run Maven with Jenkins using a custom Docker build, you can follow these steps:
FROM jenkins/jenkins:latestUSER root# Install MavenRUN apt-get update && apt-get install -y maven# Switch back to the Jenkins userUSER jenkins
In this Dockerfile, we start with the official Jenkins image (jenkins/jenkins:latest
), switch to the root user to install Maven using apt-get
, and then switch back to the Jenkins user.
docker build -t my-custom-jenkins .
This command builds the Docker image based on the Dockerfile in the active directory and tags it as my-custom-jenkins
.
docker run -d -p 8080:8080 -p 50000:50000 -v $(pwd):/my_files my-custom-jenkins
This command starts a Docker container based on the custom Jenkins image. It maps the container on port 8080
(Jenkins web interface) and 50000
(Jenkins agent communication) to the corresponding ports on the host machine. -v $(pwd):/my_files my-custom-jenkins
The command adds the current working directory to the Docker image. The active directory contains the maven application and POM file.
Go to the web browser and navigate to http://localhost:8080
. This should take you to the Jenkins web interface.
During the initial setup, Jenkins will ask for an administrator password. To obtain the password, run the following command:
docker exec <container_id> cat /var/jenkins_home/secrets/initialAdminPassword
Replace <container_id>
with the ID or name of your running Jenkins container. Copy the generated password and type it into the Jenkins web interface to proceed with the setup. Return to the Jenkins web interface and paste the copied administrator password into the provided field. Then, click the "Continue" button.
In the next step, Jenkins offers you two options: "Install suggested plugins" and "Select plugins to install." Choose the "Install suggested plugins" option to let Jenkins install the recommended plugins automatically.
Jenkins will start installing the plugins selected in the previous step. This will take a few minutes as it downloads and installs the required plugins.
After installing the plugins, Jenkins will ask you to create an admin user. Fill in the required information, including the username, password, full name, and email address. Remember these credentials, as they will be used to access Jenkins.
Once the admin user is created, Jenkins will display a "Jenkins is ready!" message. You can click the "Start using Jenkins" button to access the Jenkins dashboard.
We can verify that the Maven Integration plugin is installed on the Jenkins dashboard. Click "Manage Jenkins" in the left sidebar and select "Manage Plugins." You should see the Maven Integration plugin listed among the installed plugins in the Installed tab.
If the plugin is unavailable, go to the available plugins and install the Maven integration plugin.
create Jenkins job.
Enter the job name, select Maven app from the listed project's type, then press “OK”.
Add path to POM file.
Add post-build batch commands and save the job.
cd /my_files/maven-demo-master/mvn clean test
Now we have successfully configured the Jenkins job. Click on "Build Now" to test the app.
Free Resources