Showing posts with label ant. Show all posts
Showing posts with label ant. Show all posts

Friday, November 2, 2012

Ant related tasks in Maven

After using Maven dependency from within Ant, this time I had to find a way to call ant task from with Maven.  While trying to do a simple test around JMSMessageID behaviour (read about it on my java blog) with JBoss, I needed to include jbossall-client.jar on the classpath.  Here are couple of ways of doing this and their related issues.

Approach#1
Include jbossall-client as a dependency from JBoss public repository. Co-ordinates are org.jboss.jbossas:jboss-as-client, but this results in Maven downloading huge number of artifacts that are not requried. We would need to selectively include the required or exclude the unnecessary artifacts, which is not an easy job given the huge number.  See here and here for some help on this approach.

Approach#2
I tried adding a dependency with scope as system, providing the absolute path to the jbossall-client.jar.  Using system scope has its own issues, most critical being the dependency not being included on the classpath.  After going through lot of answers on stackoverflow zeroed on two approaches again - creating a local repository or using dependency and jar plugins.  But the problem is jbossall-client.jar

In JBoss 4.0, jbossall-client.jar used to include all the required jars/classes for a standalone client, which is changed since JBoss 5. Now the jar is sleek and refers all the jars through manifest. Issue with this is the jars won't be available if we jbossall-client.jar to some other location as the paths in the manifest are relative.

At this point I gave up and started running my Test directly from cmd by including the jbossall-client.jar in the classpath.  Then I came across two Maven plugins maven-ant-plugin and maven-antrun-plugin.  The former generates build scripts out of pom and the later can run any ant target from within pom.  The antrun plugin is what I tried as I felt the effort to setup maven local repository just to run my test program did not seem worth.  Here is what I came up with:



      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <phase>test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <target>
                <property name="runtime_classpath" refid="maven.runtime.classpath" />
                <java classname="com.sample.Tester">
                  <classpath>
                    <pathelement path="${runtime_classpath}" />
                  </classpath>
                    <pathelement location="JBOSS_HOME/client/jbossall-client.jar" />
                </java>
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>




Note that jbossall-client.jar is included later on the classpath otherwise it will try to bootstrap log4j (slf4j is part of jbossall-client.jar)


Please share if you done this differently.

Monday, October 29, 2012

Ant with maven dependency support - using maven-ant-tasks

Got hold of a nice book recently - Harnessing Hibernate from Oreilly.  While reading that, came to know about this ant task.  It would of immense help when you have an existing ant build and are in the process of migrating to Maven.  Using this task we can continue to use our build script and have all dependencies taken care by Maven.

Official page for Maven-Ant-Tasks

One can use the get task of Ant to dynamically download the jar and then include it in the build:

<get src="http://www.apache.org/dyn/closer.cgi/maven/binaries/maven-ant-tasks-2.1.3.jar"
  dest="${lib}"
  verbose="true"
  usetimestamp="true" />



A straight copy from above site:


<project ... xmlns:artifact="antlib:org.apache.maven.artifact.ant">
  ...
  <path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.3.jar" />
  <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
           uri="antlib:org.apache.maven.artifact.ant"
           classpathref="maven-ant-tasks.classpath" />
  ...
</project>


Once setup, declaring dependencies is very easy.  Look at the usage page to know how to see how to configure repositories, reference your settings.xml from local maven install.  But note that to use this task you do not need maven to be installed at all!!  It downloads the artifacts to user.home/.m2 folder.

Monday, May 3, 2010

Debugging ANT script

Instead of using the echo target for debugging, we can use the record target.  This records all the activity as per the specified level of verbosity.  Here is how to use this (from ant docs):

The following build.xml snippet is an example of how to use the recorder to record just the <javac> task:
    ...
    <compile >
        <record name="log.txt" action="start"/>
        <javac ...
        <record name="log.txt" action="stop"/>
    <compile/>
    ...