The AttributeCompiler Ant Task

This is the process your source files have to go through:

+------------+                              +--------------------+
|Java Sources|----> Attribute Compiler ---->|Generated Java Files|   
+------------+                              +--------------------+
      |                                                 |
      |                                                 |
      |               +-------------+                   |
      +-------------->|Java Compiler|<------------------+
                      +-------------+
                             |
                             v
                    +-----------------+
                    |Java .class files|
                    +-----------------+

This section will focus on the "Attribute Compiler" step. As is implied by the diagram, the Attribute Compiler compiles Java source files into other Java source files.

This is how the compiler is used:

<taskdef resource="org/apache/commons/attributes/anttasks.properties"/>
      
<attribute-compiler 
    destdir="temp/" 
    attributepackages="my.attributes;my.otherattributes">
    <fileset dir="src/" includes="*.java"/>
</attribute-compiler>
Parameter Required Description
destdir Yes Destination directory for generated source files
attributepackages No A semi-colon separated list of package names. Attributes in these packages can be used without specifying their fully-qualified names, even if they are not in the same package as the class they are being attached to, and even if they are not imported. (The compiler generates import statements in the generated source files.)

After the attribute compiler has generated the new source files, you should feed them and your own source files to the Java compiler.

Do I Have to Use the Attribute Compiler?

No, you don't. You can add attributes to a class by programmatically creating an attribute repository in the class's static initializer. See the Javadoc for RuntimeAttributeRepository for an example. It's not pretty, but it works.

How Do I Use It With Maven?

If you use Maven you can just install the Commons-Attributes plugin and add the following lines to your project.properties:

######################################################################
# Commons-Attributes
######################################################################
org.apache.commons.attributes.enable=true

#
# Optional, enables attribute indexing
#
org.apache.commons.attributes.index.enable=true

#
# Optional, equivalent to specifying attributepackages for the compiler
#
org.apache.commons.attributes.attributepackages=mypackage;myotherpackage

Attribute compilation will happen automatically.