Search code examples
mavenantheadlessjsmooth

maven -> ant -> jsmoothgen : How to provide -Djava.awt.headless=true?


I have a situation where we wrap a jar with JSmooth to get an suitable exe file.

This has traditionally been built by ant, and as part of our general mavenification the current, short-term solution has been to use maven-antrun-plugin to set a property and invoke ant.

Unfortunately this approach fails when building on Unix (as there is no X11 display available) and the solution is to invoke the JVM with -Djava.awt.headless=true. I would like to do this in my pom.xml but cannot identify where to do this.

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <phase>package</phase>
            <configuration>
                <target>
                    <!-- create one-jar and exefy it -->
                    <property name="maven.project.build.finalName" value="${project.build.finalName}" />
                    <!-- note: fails on headless Linux for now -->
                    <ant />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

It is ok to fork a new JVM directly but not to rely on platform specifics.

How can I do this correctly?


Solution

  • The ant manual has a section titled "Running Ant via Java" that shows how to do just what you want. A slightly tweaked version of their example is reproduced below:

    <java
            classname="org.apache.tools.ant.launch.Launcher"
            fork="true"
            failonerror="true"
            dir="${basedir}"
            taskname="headless-ant"
    >
        <classpath>
            <pathelement location="${ant.home}/lib/ant-launcher.jar"/>
        </classpath>
        <arg value="-buildfile"/>
        <arg file="${ant.file}"/>
        <arg value="-Dbasedir=${basedir}"/>
        <jvmarg value="-Djava.awt.headless=true"/>
    </java>
    

    If you put that snippet in place of the <ant> element in your snippet, it should do the trick.