Tagged: ant Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 11:01 am on October 13, 2010 Permalink |
    Tags: ant, archives, build, , , , script, ,   

    How-to: Get timestamp in ANT scripts 

    We use a lot of ANT scripts to build our archives. And archives without version number/timestamp is a big pain. We faced a lot of issues mainly integration issues. This is a small piece of script which can be used to add timestamp to an archive’s name.

    This is done using the tstamp task in ANT. tstamp is highly configurable and uses the java.text.SimpleDateFormat date/time patterns.

    <target name="timestamp.target">
    	<tstamp>
    		<format property="current.time"
    			pattern="yyyyMMdd_HHmmss" />
    		<format property="archive.name"
    			pattern="'MyArchive_'yyyyMMdd_HHmmss'.jar'" />
    	</tstamp>
    	<echo>${current.time}</echo>
    	<echo>${archive.name}</echo>
    </target>
    

    I got the following output when I tried to run the above build script:

    Buildfile: /home/subin/sampleProject/build.xml
    timestamp.target:
         [echo] 20101013_103329
         [echo] MyArchive_20101013_103329.jar
    BUILD SUCCESSFUL
    Total time: 524 milliseconds
    

    So, we have tstamp, which formats (format) the time/date information and assigns the result to the specified property. The echo statements demonstrate how we can use those values further.

    Hope this helps. Have a nice day.

     
  • Subinkrishna Gopi 3:45 pm on August 25, 2009 Permalink |
    Tags: ant, , , , , , ,   

    How-to: Write web services using Axis2 

    This is a very basic post on writing a web service provider and consumer using Axis2. You can find such posts anywhere in the web. Here I’ve tried to make it as simple as I can.

    Set up: What all we need to do?

    1. Axis2 runtime.
    I tried with WAR distribution from http://ws.apache.org/axis2/download/1_5/download.cgi
    2. A web/app server. I am using Tomcat.

    Using the Axis2 runtime & setting it up

    Extract the Axis2 WAR distribution and keep it in the deploy directory of the server. In case of Tomcat keep it in webapps. This is how my directory structure look like.

    image

    The WEB-INF is the most important directory. Tell you why. Unlike our normal WEB-INF in archives, this directory hold some special sub-directories. Here goes which contains what.

    classes – compiled Java classes. We can find some Axis specific classes here.
    conf – axis.xml (Axis configuration file)
    lib – All necessary Axis2 libraries (JARs)
    modules – Don’t ask me. Even I’m not sure. Did I mention that I’m also a beginner? 🙂
    services – All web service archives & services.list

    Coding: What we need to write?

    1. The service provider. A Java class.
    2. service.xml. The web services descriptor.
    3. build.xml. To build and deploy the web services archive.
    4. The service consumer. Another Java class to consume the services offered

    And we are good to go now. Let’s make our hands dirty with some Java code. This is very simple and kudos to Axis2.

    The service provider: SampleService.java

    package subin.rnd.ws;
    public class SampleService
    {
      public WsOutput doSomething(WsInput anInput)
      {
        System.out.println("doSomething()");
        WsOutput anOutput = new WsOutput();
        anOutput.setResponseString("I did some thing to " + anInput.getName());
        return (anOutput);
     }
    }
    

    WsInput is a sample input class to demonstrate that we can have more complex IO is possible. Similarly WsOutput is the output class. Instead of using WsInput / WsOutput for IO, we can use normal data types like integer, float, string etc too.

    WsInput.java

    package subin.rnd.ws;
    import java.io.Serializable;
    public class WsInput implements Serializable
    {
     private String name;
     public void setName(String name)
     {
       this.name = name;
     }
    
     public String getName()
     {
       return (this.name);
     }
    }
    

    WsOutput.java

    package subin.rnd.ws;
    import java.io.Serializable;
    public class WsOutput implements Serializable
    {
     private String responseString;
    
     public void setResponseString(String response)
     {
       this.responseString = response;
     }
     public String getResponseString()
     {
       return (this.responseString);
     }
    }
    

    We have the Java part of the web-service ready. But that’s not enough. We need to deploy the web-service as an AAR – Axis Archive – file. An AAR  is just another zip file (like a JAR) with a funky extension :D. The AAR should contain the class files along with the services.xml – web service descriptor.

    Web service descriptor: services.xml

    <service name="SampleWs" scope="application">
     <description>Subin's sample webs service</description>
     <messagereceivers>
     <messagereceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
     class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
     <messagereceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
     class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
     </messagereceivers>
     <operation name="doSomething" />
    <parameter name="ServiceClass">subin.rnd.ws.SampleService</parameter>
    </service>
    

    The services.xml is the place where we define the details of the services being offered.

    Build file: build.xml

    <project name="my.webservice.test" default="build.aar">
    <property name="deploy.dir" value=".../webapps/axis2.war/WEB-INF/services" />
    <property name="file.name" value="subinws.aar" />
    
     <target name="build.aar">
       <javac srcdir="src" destdir="bin" />
       <echo>Copying services.xml to bin</echo>
       <copy file="META-INF/services.xml"
           tofile="bin/META-INF/services.xml" overwrite="true"/>
       <jar basedir="bin" destfile="${file.name}" />
       <echo>Deleting services.xml from bin</echo>
       <delete dir="bin/META-INF" />
       <copy file="${file.name}" tofile="${deploy.dir}/${file.name}"  />
     </target>
    
    </project>
    

    So I hope we have an AAR ready, which is copied to the services directory. But we’ve not done yet. Now we have to make an entry in services/services.list file. Just insert the name of the ARR file at the end of it – in this case “subinws.aar”.

    Please wait for part 2.

     
  • Subinkrishna Gopi 10:31 am on February 12, 2009 Permalink |
    Tags: ant, , , , , ,   

    How-To: Create a WAR file using ANT 

    In one of my previous posts I mentioned about building a JAR using ANT.  This post – building a WAR (Web Application Archive) – is just an add-on to that.

    Structure of a WAR file

    Application.war
    |-- META-INF
    |  |-- manifest.mf
    |-- WEB-INF
    |  |-- web.xml   - deployment descriptor
    |  |-- classes   - class files organized in packages
    |  |-- lib       - other libraries
    |
    |-- <other files, directories etc.>

    Deployment descriptor
    This is a sample deployment descriptor from Java Servlet Specification version 2.4. Get the descriptor (.pdf)

    Build file

    <project name="my.enterprise.project" default="build.my.war">
    <property name="deploy.dir" value="/my/deploy/dir" />
    <property name="file.name" value="Application.war" />
      <target name="build.my.war">
        <fileset dir="contents">
          <include name="**/*"/>
        </fileset>
        <war destfile="${file.name}" webxml="conf/web.xml">
          <classes dir="bin" />
        </war>
        <echo>Copying ${file.name}...</echo>
        <copy file="${file.name}" todir="${deploy.dir}" />
        <delete file="${file.name}" />
      </target>
    </project>
    

    The above build file assumes that all the non-java resources and JSPs are within the “contents” directory. The build file will pick the class files from the “bin” directory within the base. Here the build file is not compiling existing source files. To compile Java files there is another ANT task –  javac.

    <javac srcdir="src" destdir="bin" classpathref="application.classpath"/>
    

    Add the javac task before the war task in the build file.

    Defining the class path

    <path id="application.classpath">
       <fileset dir="/path/to/my/lib">
          <include name="javax.servlet.jar"/>
       </fileset>
    </path>
    

    Define the path before the target – build.my.war.

    Read more
    http://ant.apache.org/manual/CoreTasks/war.html
    http://ant.apache.org/manual/CoreTasks/javac.html
    http://ant.apache.org/manual/dirtasks.html

     
  • Subinkrishna Gopi 4:16 pm on January 29, 2009 Permalink |
    Tags: ant, jar, , , ,   

    Running an executable JAR from command-line 

    An “executable JAR” is nothing but a JAR in which the entry point of execution is defined. That definition can be given in its manifest file (META-INF/Manifest.mf).

    The general structure of a JAR file:

    +-MyJar.jar
    |-+-META-INF
    |-|----Manifest.mf
    |-+-com
    |-|-+-package
    |-|-|----MyJavaFile.java
    |-|-|----MySecondJavaFile.java

    The manifest file should contain the details of the JAR file. It keeps the attribute as key-value pairs.

    The structure of a manifest file:

    Main-Class: com.package.MyJavaFile
    Specification-Title: "My Classes"
    Specification-Version:
    "1.0"
    Specification-Vendor: "Sun Microsystems, Inc."
    Implementation-Title:
    "subin.util"
    Implementation-Version: "build01"
    Implementation-Vendor: "Sun Microsystems, Inc."

    If the executable JAR is dependent on any other JAR, we need to specify it in the Manifest itself (even though there is an option to do it while invoking the application thru java -cp).

    Class-Path: aJar.jar anotherJar.jar

    I had some problems in building a JAR file using the conventional jar -cvf command. Whatever I do, it was not picking my custom manifest (I’m very bad). So, I used ANT to build a JAR.

    ANT file to build a JAR:

    <project name="Demo" default="build.jar.file">
      <target name="build.jar.file">
        <jar destfile="myJar.jar" basedir=".">
          <manifest>
            <attribute name="Main-Class" value="com.package.MyJavaFile" />
            <attribute name="Class-Path" value="aJar.jar anotherJar.jar" />
          </manifest>
        </jar>
        <copyfile dest="/path/to/deploy" src="myJar.jar"/>
      </target>
    </project>
    
    • use ANT or Eclipse IDE to build the JAR

    Running the JAR from the command-line:

    $ java -jar myJar.jar [optional parameters]

    External Links:
    http://en.wikipedia.org/wiki/Manifest_file
    http://en.wikipedia.org/wiki/JAR_file
    http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html
    http://java.sun.com/docs/books/tutorial/deployment/jar/ – Packaging tutorial from Sun
    http://ant.apache.org/manual/
    http://en.wikipedia.org/wiki/Apache_Ant

     
    • Subinkrishna G 5:27 pm on January 29, 2009 Permalink

      In the ANT build file I’ve mentioned only two attributes – Class-Path & Main-Class. These two are the most important attributes in this scenario. So using the “attribute” tag in “manifest” we can define all the other attributes too.

    • Mathew Varghese 2:22 pm on February 13, 2009 Permalink

      Add-on: If we don’t want to keep dependent JARs external, we can bundle within our target JAR. Use zipfileset ANT task.

      <zipfileset src="firstJar.jar" includes="**/*.java **/*.class"/>

      Read more: http://ant.apache.org/manual/CoreTypes/zipfileset.html

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel