Yes you can build Java like a grown up and let Jenkins do the heavy lifting. This guide walks through a practical Maven and Jenkins pipeline that compiles code runs tests and stores artifacts so you can stop doing the same five steps manually every time someone pushes a typo.
What you will need
Keep it simple and realistic. You will need a standard Maven project with a pom.xml JUnit tests so the pipeline has something to report a Jenkins server with JDK and Maven configured and a Git repository that Jenkins can access with credentials. A Multibranch pipeline makes life easier if you like branches more than chaos.
Prepare the Maven project
Create the usual Maven layout and add a pom.xml with groupId artifactId and version. Add JUnit tests so builds can fail in interesting ways and the surefire reports have something to show. If you want faster builds set up a shared local repository folder so agents can cache dependencies between runs.
Configure Jenkins server
Install a JDK and register a Maven installation under Global Tool Configuration. Create credentials for your Git host and decide whether a Multibranch pipeline or a single pipeline job fits your workflow. Multibranch will scan branches automatically which is great if developers like making dozens of feature branches for fun.
Example Jenkinsfile
Here is a short declarative pipeline to get you started. It checks out code builds with Maven runs tests publishes JUnit results and archives artifacts.
pipeline {
agent any
tools {
jdk 'jdk11'
maven 'maven3'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
sh 'mvn clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
junit 'target/surefire-reports/*.xml'
}
}
stage('Archive') {
steps {
archiveArtifacts 'target/*.jar'
}
}
}
post {
always {
echo 'Pipeline finished'
}
}
}
Run the pipeline and handle failures
Trigger a build manually or push a commit and watch the console log as Maven downloads dependencies compiles code runs tests and packages artifacts. If tests fail inspect the surefire reports in target for stack traces and flaky assertions. Fix tests or fix code but do not fix blame settings.
Agent types and commands
On Unix agents use sh 'mvn ...' and on Windows agents use bat 'mvn ...'. Pick the right command for the agent and do not try to run Windows commands on Unix unless you enjoy learning painful lessons.
Publish artifacts and test results
Use archiveArtifacts to keep build outputs and use the junit step to publish test results to the Jenkins UI. When the code reaches release quality add steps to deploy to a Maven repository or Nexus using your preferred credentials plugin and repository manager.
Performance and caching tip
Cache Maven dependencies on build agents by sharing a local repository or mounting a cache directory. That can cut build time dramatically when many builds run and developers push frequently.
Recap
This walkthrough covered wiring a Maven project into Jenkins creating a Jenkinsfile running a declarative pipeline collecting JUnit reports and archiving artifacts. The result is a repeatable CI workflow that reduces manual steps speeds feedback and gives you more time to write clever commit messages.