How to write a Simple Maven Mojo with Jenesis4Java

During many projects you feel the need to generate some code, to make the Runtime code simpler and clean of meta-informations.

Most of the time programmers rather live with the "dirty" and or complex runtime code instead of writing a small code generator, mostly with the argumentation the code generators are so difficult to write. But this is not true, as the following example will show you.

Project structure

Writing a Maven-plugin (Mojo) that generates Java source code is a easy thing with jenesis4Java. First of all, do we need a Project structure:

|  +--main
|     +--java

The Generator/Mojo pom.xml

The pom file can be very compact and should look like this:

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">



                <!-- The dependency to the maven API -->
                <!-- The dependency to the Jenesis4Java -->
                <!-- We will use Jalopy to format the Generated source code -->


The Generator/Mojo body

As usual in Maven the Mojo Java class speaks for it self.


import net.sourceforge.jenesis4java.Access;
import net.sourceforge.jenesis4java.ClassMethod;
import net.sourceforge.jenesis4java.Comment;
import net.sourceforge.jenesis4java.CompilationUnit;
import net.sourceforge.jenesis4java.Invoke;
import net.sourceforge.jenesis4java.PackageClass;
import net.sourceforge.jenesis4java.Type;
import net.sourceforge.jenesis4java.VirtualMachine;
import net.sourceforge.jenesis4java.jaloppy.JenesisJalopyEncoder;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

 * @goal jenesis4java
 * @phase generate-sources
 * @description generate the java source code
public class JenesisMojo extends AbstractMojo {

     * @parameter expression="${project}"
     * @required
    protected MavenProject project;

     * @parameter expression=
     *            "${}/generated-sources/jenesis4java"
     * @required
    protected File outputJavaDirectory;

     * @parameter
    protected String title;

    public void execute() throws MojoExecutionException, MojoFailureException {
        if (this.project != null) {
        if (!this.outputJavaDirectory.mkdirs()) {
            getLog().error("Could not create source directory!");
        } else {
            try {
            } catch (IOException e) {
                throw new MojoExecutionException("Could not generate Java source code!", e);

    private void generateJavaCode() throws IOException {
        System.setProperty("jenesis.encoder", JenesisJalopyEncoder.class.getName());

        // Get the VirtualMachine implementation.
        VirtualMachine vm = VirtualMachine.getVirtualMachine();

        // Instantiate a new CompilationUnit. The argument to the
        // compilation unit is the "codebase" or directory where the
        // compilation unit should be written.
        // Make a new compilation unit rooted to the given sourcepath.
        CompilationUnit unit = vm.newCompilationUnit(this.outputJavaDirectory.getAbsolutePath());

        // Set the package namespace.

        // Add an import statement for fun.

        // Comment the package with a javadoc (DocumentationComment).
        unit.setComment(Comment.D, "Auto-Generated using the Jenesis Syntax API");

        // Make a new class.
        PackageClass cls = unit.newClass("HelloWorld");
        // Make it a public class.
        // Extend Object just for fun.
        // Implement serializable just for fun.
        // Comment the class with a javadoc (DocumentationComment).
        unit.setComment(Comment.D, "The HelloWorld example class.");

        // Make a new Method in the Class having type VOID and name "main".
        ClassMethod method = cls.newMethod(vm.newType(Type.VOID), "main");
        // Make it a public method.
        // Make it a static method
        // Add the "String[] argv" formal parameter.
        method.addParameter(vm.newArray("String", 1), "argv");

        // Create a new Method Invocation expression.
        Invoke println = vm.newInvoke("System.out", "println");

        if (this.title != null) {
            // Add the Moja parameter string literal as the sole argument.
        } else {
            // Add the Hello World string literal as the sole argument.
            println.addArg(vm.newString("Hello World!"));
        // Add this expression to the method in a statement.

        // Write the java file.

How to use it in your other modules

That's all now we can use it in our other projects. You can build the plugin / Mojo with your other modules maven will sort out the order for you. An example:

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">


                                                        <title>Jenesis 4 Java</title>