programming


(ant 1.7, pmd 4.2.4)

I’ve recently tried to use PMD – sourcecode inspection tool in an ant powered build. Here is a basic example from a test in the build.xml of a Netbeans project (the pmd jar is located in ant’s home lib directory):

    <target name="pmd" depends="init">
         <!-- see http://pmd.sourceforge.net/ant-task.html -->
        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
        <mkdir dir="${build.test.results.dir}"/>
        <pmd targetjdk="1.6">
            <!-- auxClasspath> 
               <pathelement location="dist/test.jar" /> 
            </auxClasspath -->
            <ruleset>basic</ruleset>
            <!-- ruleset>rulesets/basic.xml</ruleset -->
            <!-- ruleset>rulesets/braces.xml</ruleset>
            <ruleset>rulesets/javabeans.xml</ruleset>
            <ruleset>rulesets/unusedcode.xml</ruleset>
            <ruleset>rulesets/strings.xml</ruleset>
            <ruleset>rulesets/design.xml</ruleset>
            <ruleset>rulesets/coupling.xml</ruleset>
            <ruleset>rulesets/codesize.xml</ruleset>
            <ruleset>rulesets/imports.xml</ruleset>
            <ruleset>rulesets/naming.xml</ruleset -->
            <formatter type="xml" toFile="${build.test.results.dir}/pmd_report.xml" />
            <fileset dir="${src.dir}">
                <include name="**/*.java" />
            </fileset>
        </pmd>
    </target>

More examples you will find at PMD Ant Task.

Running this task from command line ant -v pmd gives a warning

[pmd] Could not find class test.hallo.Main

or running same from Netbeans (using external ant).

Could not find class test.hallo.Main
java.lang.NoClassDefFoundError: org/jaxen/JaxenException
        at net.sourceforge.pmd.rules.XPathRule.getRuleChainVisits(XPathRule.java:87)

After looking around for a while I found this thread Ant could not find class.

Citation from there:

As far as what this will do to the PMD results… these errors come from the Type Resolution code. Only Rules which use the Type resolution capabilities even have a chance to give less than accurate results, and then only in circumstances requiring type information not apparent from the .java file directly. Finally, there only a few Rules in PMD that actively use Type Resolution right now, although I know several folks have written custom Rules using this capability.

Solution?

PMD 5 is not out yet. But I was successful by using the latest snapshot (at time of writing)
pmd-5.0-20081231.082852-77.jar. Also, I had to change the target definition a little:

 <target name="pmd" depends="init">
        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
        <mkdir dir="${build.test.results.dir}"/><!-- wird auch in -pre-test-run aufgerufen -->
        <pmd>
            <auxClasspath> 
               <pathelement location="dist/test.jar" /> 
            </auxClasspath>
<!-- new path for rulesets!! -->
            <ruleset>rulesets/java/basic.xml</ruleset>
            <formatter type="xml" toFile="${build.test.results.dir}/pmd_report.xml" />
            <fileset dir="${src.dir}">
                <include name="**/*.java" />
            </fileset>
        </pmd>
    </target>

Now it’s working.

(netbeans 6.1, external ant 1.7)

Here is a code snippet from build.xml which shows a possible way to customize your build in Netbeans by providing a version number and including it into the MANIFEST.MF of the created distribution jar. The current property version will be stored in a version.properties file. This version number will be retrieved and written back to the file. The script knows then the new version number (custom.version.property) and uses it in the jar creation task (see also Adaptation of Netbeans Ant Build for Integration – CopyLibs Issue).

Dealing with version number:

  <!--
                =================
                Set Version Related
                =================
     -->
     <!-- watch out! if you put version.properties file to src dirs,
     cause the file ist copied 
     to build before it is touched by this tasks, 
     you will probably need to do some further customization -->
     <property name="custom.version.file" value="${basedir}/version.properties"/>
    <target name="-post-init">
        <!-- check if there is a version -->
        <available file="${custom.version.file}" property="custom.version.available"/>
    </target>
    <target name="pre-inc-version" depends="init"  unless="custom.version.available">
        <echo message="No version file found. Creating ${custom.version.file} with Initial Version 0."/>
        <echo file="${custom.version.file}" message="version=0"/>
    </target>
     <target name="inc-version" depends="pre-inc-version">
         <!-- increment version number -->
        <propertyfile file="${custom.version.file}">
            <entry key="version" value="1" type="int" operation="+"/>
        </propertyfile>
        <!-- read version -->
        <property file="${custom.version.file}" prefix="custom"/>
        <echo message="project ${ant.project.name} released with version ${custom.version}" />
    </target>
    <target name="-pre-jar" depends="inc-version">
        <!-- Empty placeholder for easier customization. -->
        <!-- You can override this target in the ../build.xml file. -->
    </target>
    <target name="clean-version" depends="-do-clean" description="Remove version property file">
        <delete file="${custom.version.file}"/>
    </target>

(You could also use ant BuildNumber task.)

Using the version number in jar file creation:

<!--
                =================
                Jar Overriding Default
                =================
     -->
     <target depends="init,compile,-pre-jar,-post-jar" description="Custom Build JAR with libs and version. See -do-jar-with-libs." name="jar">
        <property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
        <echo message="-------------run.classpath------- ${run.classpath}"/>
        <echo message="--------- build.classes.dir.resolved ------ ${build.classes.dir.resolved}"/>
        <pathconvert property="run.classpath.without.build.classes.dir">
            <path path="${run.classpath}"/>
            <map from="${build.classes.dir.resolved}" to=""/>
        </pathconvert>
        <echo message="-------- run.classpath.without.build.classes.dir ---- ${run.classpath.without.build.classes.dir}"/>
        <pathconvert pathsep=" " property="jar.classpath">
            <path path="${run.classpath.without.build.classes.dir}"/>
            <chainedmapper>
                <flattenmapper/>
                <globmapper from="*" to="lib/*"/>
            </chainedmapper>
        </pathconvert>
        <echo message="--- build.classes.dir --- ${build.classes.dir}" />
        <copy todir="${dist.dir}/lib" flatten="true">
            <path>
                <pathelement path="${run.classpath.without.build.classes.dir}"/>
            </path>
        </copy>
        <j2seproject1:jar manifest="${manifest.file}">
            <j2seproject1:manifest>
                <j2seproject1:attribute name="Main-Class" value="${main.class}"/>
                <j2seproject1:attribute name="Class-Path" value="${jar.classpath}"/>
                <j2seproject1:attribute name="Build-Version" value="${custom.version}" />
            </j2seproject1:manifest>
        </j2seproject1:jar>
        <echo>To run this application from the command line without Ant, try:</echo>
        <property location="${dist.jar}" name="dist.jar.resolved"/>
        <echo>java -jar "${dist.jar.resolved}"</echo>
    </target>

(netbeans 6.1, external ant 1.7)

There are some great tools out there to generate a visual overview of the dependencies between the tasks within an ant file. Here is an example for Netbeans build-impl.xml file using Grand:

build-impl ant task dependencies

build-impl ant task dependencies

Here is the snippet from the task in my build.xml (following descriptions in Visual Documentation of Ant Dependencies in 3 Simple Steps):

    <target name="visual-ant-task-dependencies">
        <typedef resource="net/ggtools/grand/antlib.xml" /><!-- if its in your ants lib dir, otherwise use classpath="" -->
        <grand output="build.dot" buildfile="${basedir}/nbproject/build-impl.xml">
             <filter name="fromnode" node="jar"/>
             <!-- filter name="tonode" node="xxxx"/ -->
        </grand>
	<exec executable="dot">
            <!-- arg line="-Tpng -Gsize=11.69,8.27 -Grotate=90 -o build.png ${basedir}/build.dot" / -->
            <arg line="-Tpng -Gsize=23.39,16.54 -o build.png ${basedir}/build.dot" />
        </exec>
    </target>

I uploaded the grand.jar to my ant home /lib directory. Of course you will need graphviz to be installed for the dot command. Have fun!

(Netbeans 6.1, external ant 1.7 )

If you create a standalone Java application project (i.e. one that comes with all used external jars and libs included, so there are copied to dist/lib) in Netbeans, commit the changes to version control, checkout or update the changes to your test and integration server and try to build the same project there, you might run into problems even if you used an external ant for building the same project in Netbeans on your local machine. (Look here for an explanation why jars are not includeable in the distribution jar itself.)

On the integration server the libs will not be copied to the dist/lib folder. The reason is that Netbeans is using an own ant task org.netbeans.modules.java.j2seproject.copylibstask which extends import org.apache.tools.ant.taskdefs.Jar and this jar will just not be found on the integration server except you completly reconstruct your local build environment on the integration server, what will be difficult if you are using a CI system (in my case it’s hudson). So let’s have a closer look at this issue and some possible solutions:

There are two principal ant build files Netbeans is using during the build process: build.xml – user can edit this file, build-impl.xml – user should not edit this one. Also have a look at this post Visualizing Default Ant Task Dependencies for Netbeans build-impl.xml.

Where and when the Libs are copied to dist/lib by the CopyLibs task?

For that we have to look into build-impl.xml. The libs are copied when the jar target ist executed:

 <target 
depends="init,compile,-pre-jar,-do-jar-with-manifest,-do-jar-without-manifest,-do-jar-with-mainclass,-do-jar-with-libraries,-post-jar"
description="Build JAR." 
name="jar"/>

There are several jar related tasks, which are executed when certain conditions are valid, that means certain variables are set. The interesting task here is -do-jar-with-libraries:

 <target depends="init,compile,-pre-pre-jar,-pre-jar" 
if="manifest.available+main.class+mkdist.available" 
name="-do-jar-with-libraries">

This task will be executed if property manifest.available+main.class+mkdist.available is set. Especially if the path to CopyLibs is set.

        <condition property="manifest.available+main.class+mkdist.available">
            <and>
                <istrue value="${manifest.available+main.class}"/>
                <isset property="libs.CopyLibs.classpath"/>
            </and>
        </condition>

Where does this path comes from. It is read in

    <target depends="-pre-init,-init-private,-init-libraries" name="-init-user">
        <property file="${user.properties.file}"/>

For example from: .netbeans/6.1/build.properties.

libs.CopyLibs.classpath=/home/user/bin/netbeans/java2/ant/extra/org-netbeans-modules-java-j2seproject-copylibstask.jar
libs.CopyLibs.javadoc=
libs.CopyLibs.maven-pom=
libs.CopyLibs.src=

That’s a problem! The build.properties file will not be found on the integration server, the properties libs.CopyLIbs.classpath and manifest.available+main.class+mkdist.available will not be set, the task -do-jar-with-libraries will not be executed.

What do you need CopyLibs task for?

Look at how the task is used (I included some echos for better understanding):

<target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available+main.class+mkdist.available" name="-do-jar-with-libraries">
        <property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
        <echo message="-------------run.classpath------- ${run.classpath}"/><echo message="--------- build.classes.dir.resolved ------ ${build.classes.dir.resolved}"/>
        <pathconvert property="run.classpath.without.build.classes.dir">
            <path path="${run.classpath}"/>
            <map from="${build.classes.dir.resolved}" to=""/>
        </pathconvert>
        <echo message="-------- run.classpath.without.build.classes.dir ---- ${run.classpath.without.build.classes.dir}"/>
        <pathconvert pathsep=" " property="jar.classpath">
 
            <path path="${run.classpath.without.build.classes.dir}"/>
            <chainedmapper>
                <flattenmapper/> <target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available+main.class+mkdist.available" name="-do-jar-with-libraries">
                <globmapper from="*" to="lib/*"/>
            </chainedmapper>
        </pathconvert>
        <taskdef classname="org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs" classpath="${libs.CopyLibs.classpath}" name="copylibs"/>
        <copylibs compress="${jar.compress}" jarfile="${dist.jar}" manifest="${manifest.file}" runtimeclasspath="${run.classpath.without.build.classes.dir}">
            <fileset dir="${build.classes.dir}"/>
            <manifest>
                <attribute name="Main-Class" value="${main.class}"/>
                <attribute name="Class-Path" value="${jar.classpath}"/>
            </manifest>
        </copylibs>
        <echo>To run this application from the command line without Ant, try:</echo>
        <property location="${dist.jar}" name="dist.jar.resolved"/>
        <echo>java -jar "${dist.jar.resolved}"</echo>
    </target>

and then study the java code. So not much problems here. Mainly copying some files and using standard jar functions, also providing some README file. Below I will show how you can workaround using this task.

Solutions?

First, you could copy the CopyLibs Task jar somewhere and set the property libs.CopyLibs.classpath in your build.xml to the appropriate location or use project.properties file which comes with the Netbeans build scripts. There are little chances that this task will change and you will have to update the CopyLibs Task jar.

Second, you could build your standalone application using One-JAR overriding the jar target from build-impl.xml.

Third, don’t change build structure but avoid using CopyLibs task. Here is an example how to change build.xml:

First make macrodefs and presetdefs from build-impl.xml callable by declaring appropriate namespaces in build.xml:

<project name="Test" default="default" basedir="."
xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1"
xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3">
    <description>Builds, tests, and runs the project Test.</description>

Then redefine the jar target a little and override it in build.xml (echos only for feedback):

<!-- target depends="init,compile,-pre-pre-jar,-pre-jar" if="manifest.available+main.class+mkdist.available" name="-do-jar-with-libraries" -->
     <target depends="init,compile,-pre-jar,-post-jar" description="Build JAR." name="jar">
        <property location="${build.classes.dir}" name="build.classes.dir.resolved"/>
        <echo message="-------------run.classpath------- ${run.classpath}"/>
        <echo message="--------- build.classes.dir.resolved ------ ${build.classes.dir.resolved}"/>
        <pathconvert property="run.classpath.without.build.classes.dir">
            <path path="${run.classpath}"/>
            <map from="${build.classes.dir.resolved}" to=""/>
        </pathconvert>
        <echo message="-------- run.classpath.without.build.classes.dir ---- ${run.classpath.without.build.classes.dir}"/>
        <pathconvert pathsep=" " property="jar.classpath">
            <path path="${run.classpath.without.build.classes.dir}"/>
            <chainedmapper>
                <flattenmapper/>
                <globmapper from="*" to="lib/*"/>
            </chainedmapper>
        </pathconvert>
        <echo message="--- build.classes.dir --- ${build.classes.dir}" />
        <copy todir="${dist.dir}/lib" flatten="true">
            <path>
                <pathelement path="${run.classpath.without.build.classes.dir}"/>
            </path>
        </copy>
        <j2seproject1:jar manifest="${manifest.file}">
            <j2seproject1:manifest>
                <j2seproject1:attribute name="Main-Class" value="${main.class}"/>
                <j2seproject1:attribute name="Class-Path" value="${jar.classpath}"/>
                <!-- if you deal with versions -->
                <!-- j2seproject1:attribute name="Build-Version" value="${version}" / -->
            </j2seproject1:manifest>
        </j2seproject1:jar>
        <echo>To run this application from the command line without Ant, try:</echo>
        <property location="${dist.jar}" name="dist.jar.resolved"/>
        <echo>java -jar "${dist.jar.resolved}"</echo>
    </target>

… and you’ve got rid of the annoying CopyLibs task thing.

Automation for the people

Here a very simple init script tested on Suse SLES 10. An other good way to create one is looking at the init-Script for tomcat or using just it and installing Jira as WAR into tomcat.

#!/bin/bash
# Jira startup script
# install:
# copy to /etc/init.d/jirad
# ln -s /etc/init.d/jirad /sbin/rcmy-jirad
# insserv /etc/init.d/jirad
 
### BEGIN INIT INFO
# Provides:          jirad
# Required-Start:    $local_fs $network
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Starts Jira server
# Description:       Starts Jira Issue Tracking
### END INIT INFO
# Based on script at  http://confluence.atlassian.com/pages/viewpage.action?pageId=183148
 
RUN_AS_USER=jico
CATALINA_HOME=/opt/jira
 
start() {
        echo "Starting Jira: "
        if [ "x$USER" != "x$RUN_AS_USER" ]; then
          su - $RUN_AS_USER -c "$CATALINA_HOME/bin/startup.sh"
        else
          $CATALINA_HOME/bin/startup.sh
        fi
        echo "done."
}
stop() {
        echo "Shutting down Confluence: "
        if [ "x$USER" != "x$RUN_AS_USER" ]; then
          su - $RUN_AS_USER -c "$CATALINA_HOME/bin/shutdown.sh"
        else
          $CATALINA_HOME/bin/shutdown.sh
        fi
        echo "done."
}
 
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 10
        #echo "Hard killing any remaining threads.."
        #kill -9 `cat $CATALINA_HOME/work/catalina.pid`
        start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
esac
 
exit 0

Say you create XHTML from XML using XSLT transformation (like me from Docbook to XHTML chunks using Docbook XSLT Stylesheets ) and you want to include some Javascript in the XHTML output.

If you serve your XHTML pages with mime-type: application/xhtml+xml rather then text/html , you will have to take care of xml-relevant characters within the script code (see Javascript and XHTML for a short explanation):

<script type="text/javascript">
/* <![CDATA[ */
// content of your Javascript goes here
/* ]]> */
 
or 
 
<script type="text/javascript">
// <![CDATA[ 
 content of your Javascript goes here
//]]>
</script>

Generating this output with xslt can be a little bit tricky. My solution (probably better exist) uses a trick shown in http://www.w3.org/TR/xslt#section-XML-Output-Method:

... xslt code ...
<script type="text/javascript">
	<xsl:text disable-output-escaping="yes">
	<![CDATA[//<![]]><![CDATA[CDATA[]]>
//javascript
	<![CDATA[//]]><![CDATA[]]]]><![CDATA[>]]>
        </xsl:text>
</script>
... xslt code

To exclude certain files and directories from subversion’s version control you can set the ‘svn:ignore’ property as described in Ignoring Unversioned Items.
That works fine but nevertheless you might run into problems if you are using
svn add *
or similiar on linux systems. This is due to the shell expansion of wildcards. The above command is expanded to explicit files names svn add filename and seems to have the same effect as svn add --no-ignore filename.
Tip: Use svn add --force . instead!

Problem:Create an XML Schema which will be binded by JAXB to a class structure following certain constrains. You have certain degrees of freedom for your schema design and later you might also use schema annotations provided by JAXB. Modularize your XML schemas, simplest example is: A.xsd uses &lt;include&gt; to include declarations from schema B.xsd.

I want to try Netbeans’ excellent schema design features. Eventually I want to publish the schemas in a WAR deployed in a web container. That’s why I decided to put the files directly into the /web directory of the web-project.

Proposed Solution:
Trying to use Netbeans’ JAXB Wizard (or see http://wiki.netbeans.org/JAXBWizard) to create a JAXB Binding for A.xsd you will meet certain limitations: schema file changes won’t be visible to the “Regenerate” operation. You could edit the copy of A.xsd created by the JAXB Wizard in src/conf/xml-resources/jaxb/name_of_the_binding to make changes visible to the task but that will not see changes in B.xsd. The reason is that the ant tasks created by the JAXB Wizard do not provide this kind of granularity.

So the solution for me was to create the binding using JAXB Wizard, then overriding the created tasks. I copied &lt;target name="jaxb-code-generation" ... from nbproject/xml_binding_build.xml to build.xml and customized the parameters of the XJC task:

  <import file="nbproject/build-impl.xml"/>
 
    <target name="ask-something">
    <input message="Do you like to customize the best IDE of all times?"
    validargs="Yes, Yes"
    addproperty="answer"/>
<echo message="Answer Yes! ${answer}" />
    </target>
 
     <target name="jaxb-code-generation" depends="xjc-typedef-target">
        <mkdir dir="build/generated/addons/jaxb" xmlns:s="http://xml.netbeans.org/schema/JAXBWizConfig"/>
        <mkdir dir="build/generated/jaxbCache" xmlns:s="http://xml.netbeans.org/schema/JAXBWizConfig"/>
        <mkdir dir="${build.classes.dir}" xmlns:s="http://xml.netbeans.org/schema/JAXBWizConfig"/>
        <mkdir dir="build/generated/jaxbCache/HandleSystemBinding"/>
 
<!-- remove catalog attribut here or change refs in catalog.xml : catalog="catalog.xml" -->
        <xjc package="de.gbv.examples.handesystembinding" destdir="build/generated/jaxbCache/HandleSystemBinding">
            <classpath>
                <pathelement location="${src.dir}"/>
                <pathelement path="${jaxbwiz.xjcrun.classpath}"/>
            </classpath>
            <arg value="-xmlschema"/>
            <arg value="-verbose"/>
 
            <!-- customize this -->
            <schema file="web/A.xsd"/>
 
            <depends file="web/B.xsd"/>
            <!-- end customization -->
 
            <produces dir="build/generated/jaxbCache/HandleSystemBinding"/>
        </xjc>
 
        <copy todir="build/generated/addons/jaxb">
            <fileset dir="build/generated/jaxbCache/HandleSystemBinding"/>
        </copy>
        <javac destdir="${build.classes.dir}" srcdir="build/generated/addons/jaxb" source="${javac.source}" target="${javac.target}" xmlns:s="http://xml.netbeans.org/schema/JAXBWizConfig">
            <sourcepath location="${src.dir}"/>
            <classpath path="${jaxbwiz.gensrc.classpath}"/>
        </javac>
    </target>

Note: Later discussions on netbeans issue tracking showed that there is a simpler way by first running “refresh” on the bindings schema and then doing “clean and build”. For that you don’t need to modify the ant target, nevertheless the discussed modification gives you this with just the “Regenerate” action click.

In simple REST-style webservice situation you sometimes have to model container-item relationships and provide an XML Schema as a means for interface description.
Here is an example how to design the associated XML Schema.

Example of an XML document (container):


&lt;ns2:container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
xmlns:ns2='http://xml.example.org/schema/container'
xsi:schemaLocation='http://xml.example.org/schema/container container.xsd'&gt;

&lt;ns2:item&gt;
   &lt;ns2:value&gt;A&lt;/ns2:value&gt;
&lt;/ns2:item&gt;

&lt;ns2:item&gt;
   &lt;ns2:value&gt;B&lt;/ns2:value&gt;
&lt;/ns2:item&gt;
&lt;/ns2:container&gt;

In REST you often want to give access to an item without the container wrapper. One solution is to provide two schema files – one for item and one for container:

container.xsd:

&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
targetNamespace="http://xml.example.org/schema/container_item"
xmlns:tns="http://xml.example.org/schema/container_item"
elementFormDefault="qualified"&gt;

&lt;xsd:include schemalocation="item.xsd"/&gt;

&lt;xsd:element name="container"&gt;
   &lt;xsd:complextype&gt;
      &lt;xsd:sequence&gt;
          &lt;xsd:element ref="tns:item" maxoccurs="unbounded"/&gt; 
      &lt;/xsd:sequence&gt;
   &lt;/xsd:complextype&gt;
&lt;/xsd:element&gt;

&lt;/xsd:schema&gt;

item.xsd:

&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
targetNamespace="http://xml.example.org/schema/container_item"
xmlns:tns="http://xml.example.org/schema/container_item"
elementFormDefault="qualified"&gt;

&lt;xsd:element name="item" type="tns:itemType"&gt;
   &lt;xsd:complextype name="itemType"&gt;
      &lt;xsd:sequence&gt;
         &lt;xsd:element name="value" type="xsd:string"/&gt;
      &lt;/xsd:sequence&gt;
  &lt;/xsd:complextype&gt;
&lt;/xsd:element&gt;

&lt;/xsd:schema&gt;

Next Page »