Running "headless Eclipse" inside Ant task

3.1k views Asked by At

Please note - to anybody that argues that I should "remove the backstory" here, or that this question has "nothing to do with CodePro", I would like to mention that CodePro has very little documentation for how to get their Ant tasks up-and-running properly from the command-line. CodePro is almost 100% UI-driven, which their documentation focuses on. If this question can be completely answered, I believe it will serve as one of the few (if not the only) full pieces of documentation for other developers like myself who just want to run CodePro tasks from the command-line, as part of their build.

I'm trying to get CodePro AnalyTix's Ant tasks working from the command-line.

After reading the documentation, and downloading some examples, it doesn't want you to run tasks the normal way you run externally-defined tasks in Ant, which usually looks like:

  1. Define an xmlns attribute inside the root <project> element for the 3rd party Ant jar
  2. Define 1+ tasks with <taskdef> declarations
  3. Call those defined tasks from inside a <target> element

With CodePro, the Ant tasks depend on many core Eclipse jars/classes. If you download any of their examples, you'll see Windows BATCH files written that demonstrate how they want you to run their Ant tasks, and they all take on the following form:

set JAVAEXE="\Programs\jdk1.5.0_05\jre\bin\java.exe"
set ECLIPSEHOME="\Programs\eclipse_330"
set WORKSPACE="\Programs\eclipse_330_headless\headless_arbitrary_src_multi_audit\workspace"
for /f "delims= tokens=1" %%c in ('dir /B /S /OD %ECLIPSEHOME%\plugins\org.eclipse.equinox.launcher_*.jar') do set EQUINOXJAR=%%c

:run
@echo on
%JAVAEXE% -jar %EQUINOXJAR% -clean -noupdate -application org.eclipse.ant.core.antRunner -data %WORKSPACE% -verbose -file headless.xml %* >headless_out.txt 2>&1

The headless.xml is the Ant file that has the CodePro Ant targets and tasks.

I tried all morning to supply my Ant lib directory with the necessary dependencies it needs to run these CodePro tasks the "normal" way, but found myself beginning to copy the entire Eclipse plugins/ directory into ANT_HOME/lib! I've reverted those changes and am now ready to accept this (rather ugly) way of running Ant from the command-line.

Borrowing from the example above, here's my new setup:

  • build.xml is my "core" Ant buildfile, and is where I will have a target named run-codepro-analysis
  • codepro-analysis.xml is where all my CodePro tasks will be defined (inside of wrapper targets); this is equivalent to headless.xml in the BATCH file above
  • I want buildfile.xml#run-codepro-analysis to call <java> and run the exact same arguments as provided in the BATCH file above, with the following exceptions:
    • Coincidentally, I already have three env vars setup on my machine: JAVA_HOME (pointing to my JDK), ECLIPSE_HOME and WORKSPACE_HOME; I obviously want to use these in lieu of JAVAEXE, ECLIPSEHOME and WORKSPACE
    • I want the output to be redirected to the same console/terminal as I am running ant -buildfile build.xml run-codepro-analysis from; not headless_out.txt or some other file

Seeing that this is a pretty complicated command (with many options, arguments, and even nested argument to options), I'm not really sure how to construct the <java> elements inside the run-codepro-analysis target, but here's what I have so far:

<target name="run-codepro-analysis">
    <java fork="true" failonerror="true"
        jar="${env.ECLIPSE_HOME}/plugins/org.eclipse.equinox.launcher_*.jar">
        <arg line="-clean -noupdate -application org.eclipse.ant.core.antRunner -data ${env.WORKSPACE_HOME} -verbose -file codepro-analysis.xml"/>
</target>

When I run this, the build fails:

[java] Unable to access jarfile /home/myuser/sandbox/workbench/eclipse/indigo-3.7/eclipse/plugins/org.eclipse.equinox.launcher_*.jar

I'm not sure if this is because the wildcard is failing and its actually looking for org.eclipse.equinox.launcher_*.jar, or if something else inside my <java> task is configured incorrectly. Thanks in advance to anyone who can help me get this working. Like I said, there's no other comprehensive documentation on the web (that I could find, anyways) that explains how to get this working as part of a build, especially for Linux developers who can't run BATCH files.

1

There are 1 answers

0
Alexander Pogrebnyak On

Almost there.

jar parameter cannot have wild cards, but this can be easily fixed, use classname and classpath instead (that can use fileset with wild cards).

I also recommend to use single-argument form factor for passing command line args to java task. Less surprises later on

<target name="run-codepro-analysis">
  <java fork="true" failonerror="true"
    classname="org.eclipse.equinox.launcher.Main"
  >
    <arg value="-clean"/>
    <arg value="-noupdate"/>
    <arg value="-application"/>
    <arg value="org.eclipse.ant.core.antRunner"/>
    <arg value="-data"/>
    <arg file="${env.WORKSPACE_HOME}"/>
    <arg value="-verbose"/>
    <arg value="-file"/>
    <arg file="codepro-analysis.xml"/>

    <classpath>
      <fileset dir="${env.ECLIPSE_HOME}/plugins">
        <include name="org.eclipse.equinox.launcher_*.jar"/>
      </fileset>
    </classpath>
  </java>
</target>