If you want reproducible Java builds but also like the idea of not being surprised at 2 a m by a broken pipeline, this guide uses Apache Ant and Jenkins to get you from source to artifact with minimal drama. We will verify tools, create a simple Ant build file, wire up a Jenkins job, archive artifacts, and run the job. Yes it is boring, and yes it works.
First prove to the computer that it has Java and Ant installed. Run these commands on the build agent or developer machine.
java -version
ant -version
Next confirm Jenkins is reachable at the host and port you expect. If Jenkins is hiding behind a reverse proxy, make friends with whoever set that up. If you have agents, make sure the agent user can run Ant and reach the Java compiler.
Keep the Ant build file simple. Put build.xml under source control with the rest of your code so it does not go missing between coffee breaks.
<project name="sample" default="jar" basedir=".">
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="dist"/>
<jar destfile="dist/myapp.jar" basedir="build/classes"/>
</target>
</project>
This gives you three obvious targets, clean, compile, and jar. The javac and jar tasks are the usual suspects for Java projects managed with Ant.
You have two main paths in Jenkins, a Freestyle project or a Pipeline job. Both work for CI automation with Ant.
If you prefer a Jenkinsfile in source control here is a compact Declarative Pipeline that runs Ant and archives the result.
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'ant clean jar'
}
}
}
post {
always {
archiveArtifacts 'dist/*.jar'
}
}
}
On Windows agents change the shell step to bat 'ant clean jar'. The pipeline keeps CI automation configuration next to your code which is a very good habit.
Archiving artifacts means you can download the jar from Jenkins when someone says it worked on my machine. Configure post build actions in Freestyle jobs or the post block in Pipeline jobs to save your dist folder. Use environment variables in the job or Jenkinsfile to control version numbers or build modes across branches.
That is the gist of using Apache Ant with Jenkins to create reliable build jobs for Java projects. It is straightforward, it works, and it will save you time unless you enjoy late night debugging sessions. Now go make CI less dramatic.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.