You want thunderbird to keep your preformatted text in emails? Here is a plugin for this: https://addons.mozilla.org/en-US/thunderbird/addon/2351

… for instance to send a configuration file to majordomo list manager.

Situation: You have a lot of groups as groupOfNames in you ldap directory. How to find all such groups a known member belongs to?

The goupOfNames object might look similiar to this:

dn: cn=TestGroup, o=TestOne, dc=rekk, dc=de
objectClass: dcObject
objectClass: groupOfNames
cn: cn=TestGroup
dc: dc=TestGroup
description: Some test group
member: uid=00001
member: uid=00011
member: uid=schwarz,dc=Test DC,dc=rekk,dc=de

The query would be for uid=schwarz:
(member=uid=schwarz,dc=Test DC,dc=rekk,dc=de)

or more specific:
(&(objectclass=groupofnames)(member=uid=schwarz,dc=Test DC,dc=rekk,dc=de)

Also see LDAP Query Adventures.

(ubuntu 9.04 gnome, netbeans 6.5, Java 1.6)

I assume you are familiar with: FaqNetbeansConf , Change the Font Size .

Setting the font size of your editors is easy : Tools – Options – Font and Colors .

If you want to change font size of the menues and panels of the GUI and you are using startup options

--laf javax.swing.plaf.metal.MetalLookAndFeel

you can set the GUI font size:
--fontsize 13

and it will work without any problems.

In case you are using the default GTK Theme you will find that the last option doesn’t change the font size of the menues. Here it helps to edit the ~/.gtkrc-2.0 configuration file:

# This file was written by KDE
# You can edit it in the KDE control center, under "GTK Styles and Fonts"

include "/usr/share/themes/Qt/gtk-2.0/gtkrc"

style "user-font"
{
  font_name="Verdana 11"
}
widget_class "*" style "user-font"

gtk-theme-name="Qt"
gtk-font-name="Verdana 11"

Change entries like “Verdana 11″ according to your needs.

This would be easy in kde, but in gnome some extra steps are needed. I changed to gnome just recently being impressed by the clarity and simplicity of the user interface. Being used to the “kde way” I needed some time to figure that out.

This is an example for kdissert:

Create a wrapper script for example kdissert.sh in ~/bin/launch and make it executable

#!/bin/bash                                                                                                     
cd ~/Known
nohup kdissert > /dev/null 2>&1 &
exit 0

Then use the panel menu to create a Custom Application Launcher : Add to panel -> Custom Application Launcher -> Choose Type: Application, Command: /home/yourusername/bin/launch/kdissert.sh

Another way is to use shell directly. But watch out – you will need to source your .bashrc if needed:

[Desktop Entry]
Version=1.0
Encoding=UTF-8
Name=netbeans with some environment settings
Exec=/bin/bash -c  '. ~/.bashrc; ~/workarea/nb_run.sh'
# to run it in a terminal:
# Exec=gnome-terminal --command "/bin/bash -c  '. ~/.bashrc; ~/workarea/nb_run.sh' "
Icon=/home/kostja/bin/netbeans-6.5rc2/nb6.5/netbeans.png
Categories=Development;Java;IDE
StartupNotify=true
Type=Application

Recently I had to face a limitation of subversion – you can set files and patterns for ignoring files (using svn:ignore) but you can not specify certain files or patterns for files to be included. In my situation, I wanted to build an Ivy repository and put only the ivy files under the version control cause all other artifacts could be rebuild using SCM.

So I came up with a simple bash script, which is run just before commit and is actually called from an ant script in a build environment:

#!/bin/bash
#
# adds only certain files to repository, adds
# all intermediate directories on their paths,
# it also works with empty space in path (except newline)
#
# @todo Add some bells and whistles!
 
REPO_NAME=<some_repo>
echo "Running $0 on" `date` "with $REPO_NAME"
 
# this file will help to limit the search, if it's modified on commit
# for example the log file of last commit, the idea here is to run 
# this script always before commit
REF_TIME_FILE=<some_file>
REP_PATH=/some/path/to/your/copies/root
 
FILE_PATTERN='ivy.xml*'
DIR=`pwd`
 
cd $REP_PATH
IFS_ORIG=$IFS
# this allows paths with whitespace, but not with newline
IFS=$'\n'
#IFS="$(echo)" another way to get newline
for ivy in `find ./ -newer $REF_TIME_FILE  -type f  -name "$FILE_PATTERN"  -print`
do
	echo "found fresh file $ivy"
	p=$ivy
	while [ $p != "." ]
	do 
			all_paths=${all_paths}${del}${p}
			p=${p%/*}	
			del=:
	done
done
 
IFS=$IFS_ORIG
#echo $all_paths | tr ':' '\n' | sort | uniq |  tr '\n' '\0' | xargs -0 -i echo {}
echo $all_paths | tr ':' '\n' | sort | uniq |  tr '\n' '\0' | xargs -0  svn add --non-recursive  2>&1 | grep "^A"
 
 
cd $DIR
echo "Finish $0 on" `date` "with $REPO_NAME"

It’s not a big deal but might help you to spare some time. There might be better ways to do it and I wonder if same can be done using “plain ant”.

(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

Next Page »