Platforms and Technology

Nexus Implementation using Ant Build scripts

Istock 948997140 (1)

Ant and NexusNexus implementation using Ant Build scripts

In general, we keep the required compile-time jars in the local file system to use at the time of build. To avoid local file system dependencies and inconsistencies, we can use central repositories. By integrating with repositories like Maven and Nexus, we can save the memory, and it is easy to manage the versions of the required jars just by specifying its versions in build files.

Achieving this in Gradle is quite easy as we can declare the required dependencies under the dependencies block.

In Ant, we don’t have any off-the-shelf feature to achieve this.
But still, we can do this by downloading the required jars from the repositories before starting the build.

The basic idea is

  1. Download the jars from the given URL.
  2. Build the ear or war file with the help of downloaded jars.
  3. Delete the downloaded jars after the ear/war file is created.

 

  1. Download the Jars:
  • Create a target with the name “downloadJars”.
  • Create a new folder using “mkdir” with the name “classpath_jars”.
  • Use “get” task to download the jars (“get” is used to fetch files from the server).
  • Set the URL of the jar in src attribute and destination folder in dest attribute in our case (classpath_jars).
<target name="downloadJars">
    <mkdir dir="classpath jars"/>
    <get dest="${basedir}/classpath jars/jboss-servlet-api 4.0 spec-1.0.0.Final-redhat-1.jar" verbose="true" src="https://nexusrepol:8081/repository/web boss hosted system layers/base/javax/servlet/api/main/jboss-servlet-api 4.0 ec-1.0.0.Final-redhat-1.jar"/>
    <get dest="${basedir}/classpath jars/jboss-transaction-api 1.2 spec-1.1.1. Final-redhat-1.jar" verbose="true" src="https://nexusrepol:8081/repository/webjboss hosted/system/layers/baseljavax/transaction/api/main/jboss-transaction-ap i 1.2 spec-1.1.1. Final-redhat-1.jar"/>
    <get dest="${basedir}/classpath jars/javax. persistence-api-2.2.0. redhat-1.jar" verbose="true" src="https://nexusrepol:8081/repository/web_jboss hosted system layers baseljavax/persistence api main/javax. persistence-api -2.2.0. redhat-1.jar"/>
    <get dest="${basedir}/classpath jars/javax.mail-1.6.1. redhat-1.jar" verbose="true" src="https://nexusrepol:8081/repository/web_jboss hosted/system/layers/base/javax/mail/api/main/javax.mail-1.6.1. redhat-1.ja id=1:6, s.redhat 1.jar" a verbose intrue" adilmain javax. mai 1-1.6.1. redhat-1.
    <get dest="${basedir}/classpath jars/activation-1.1.1.redhat-5.jar" verbose="true" src="https://nexusrepo1:8081/repository/web_jboss hosted/system/layers/base/javax/activation/api/main/activation-1.1.1. redha t-5.jar"/>
</target>
  • After this set the classpath as shown in the below image
<path id="class.path"
<fileset dir="${basedir}/classpath jars">
    <include name="**/*.*"/>
</fileset>
</path>
  1. Build the ear or war file using the downloaded jars.
  • Create a target to compile the code as shown below.
<!-- Compile code -->
<target name="compile">
    <echo message="**** Compiling Code ****"/>
    <!-- Emit the property to the ant console -->
    <javac debug="${debug]" srcdir="${local.src_dir)" 
    destdir="${local.build dir}/WEB-INF/classes">
        <classpath refid="class.path"/>
    </javac>
</target>
<!-- End of Compile code -->
  • Create a target to generate a war/ear file with the compiled code.
  • Specify the tasks “downloadJars” and “compile” in the “depends” attribute to maintain the order of the tasks.
<!-- Preparing War -->
<target name="create war" depends="downloadJars, compile">
    <copy overwrite="yes" todir="${local.build dir}/WEB-INF/classes">
        <fileset dir="${local.src dir}"/>
    </copy>
    <copy overwrite="yes" todir="${local..build dir}"/>
    <copy overwrite="yes" file="${local.main dir}/config/${env}/environment.properties" tofile="${local.build dir}/WEB-INF/classes/environment.properties"/>
    <jar compress="true" jarfile="${local.war dir}/App.war" basedir="${local.build dir}"/>
</target>
<!-- End of Preparing War -->
  1. Delete the downloaded jars after the ear/war file is created.
  • By deleting the downloaded Jars, we would accomplish the advantage of using the central repository.
<!-- Entry-level Targets -->
<target name="build-all" depends="init, downloadJars, perform-checkout, create war">
    <echo> Build successful !!</echo>
    <delete dir="${basedir}/classpath jars"/>
</target>

Downloading jars from Nexus:

CLI output:

downloadJars:
[mkdir] Created dir: C:\Users\sr7417\Desktop\Nexus_impl\classpath_jars
[get] Getting: http://nexusrepo1:8081/repository/web_jboss_hosted/system/layers/base/javax/servlet/api/main/jbossservlet-api_4.0_spec-1.0.0.Final-redhat-1.jar
[get] ...
[get] Getting: http://nexusrepo1:8081/repository/web_jboss_hosted/system/layers/base/javax/transaction/api/main/jb oss-transaction-api_1.2_spec-1.1.1.Final-redhat-1.jar
[get] .
(get] Getting: http://nexusrepo1:8081/repository/web_jboss_hosted/system/layers/base/javax/persistence/api/main/ja vax.persistence-api-2.2.0.redhat-1.jar
[get] .........
(get) Getting: http://nexusrepo1:8081/repository/web_jboss_hosted/system/layers/base/javax/mail/api/main/javax.mai 1-1.6.1.redhat-1.jar
[getl ....................................
[get] Getting: http://nexusrepo1:8081/repository/web_jboss_hosted/system/layers/base/javax/activation/api/main/act ivation-1.1.1. redhat-5.jar
[get] .

Jars downloaded in the folder:

Downloaded files

 

Reference:

https://ant.apache.org/manual/tasksoverview.html

About the Author

Sundharamurali Ramachandran is a technical consultant with over 3 years of experience. He is a full-stack developer and has worked with various technology stacks, including Java, Angular, Spring Boot, JBoss, and WebSphere.

More from this Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up