I downloaded then javapp.zip file, extracted and have the jar.
I also have a verysimple javac HelloWorls.java ready to test.
I can not find exactly how to use this wonderful tool - I am not using ANT at the moment. Can anyone point me to a list of instructions on what to do to invoke the preprocessor from the command line?
Thanks,
Adam
How to invoke the preprocessor
(13 posts) (2 voices)-
Posted 11 months ago #
-
I think I just need how to load the xml definition files that has the preprocessing info and defines and the syntax for the input and output java files
Posted 11 months ago # -
OK, I built a simple ant build.xml file and inserted the referenced code from the slashdev website (At end of this post), but Get an error - Not sure what I am missing here.
I can call any just fine and compile java directly - just failing to successfully preprocess
Any input would be greatly appreciated.
Adampreprocess:
BUILD FAILED
C:\FOUO_SC650\java\build.xml:21: 'HelloWorld.java', line 5, char 6: Token not recognised in state 'ifcond'complete build.xml file
<project name="HelloWorld" basedir="." default="main"><property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/><property name="main-class" value="HelloWorld"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target><taskdef resource="javapp-defs.xml" />
<property name="os_version" value="4.2" /><target name="preprocess">
<javapp destdir="build\staging" prefix="//#">
<fileset dir="src" includes="**/*.java" />
<property name="PROJ1" value="SC650" />
</javapp>
</target><target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target><target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target><target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target><target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>
<project name="HelloWorld" basedir="." default="main"><property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/><property name="main-class" value="HelloWorld"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target><taskdef resource="javapp-defs.xml" />
<property name="os_version" value="4.2" /><target name="preprocess">
<javapp destdir="build\staging" prefix="//#">
<fileset dir="src" includes="**/*.java" />
<property name="PROJ1" value="SC650" />
</javapp>
</target><target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target><target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target><target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target><target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>
Posted 11 months ago # -
Can you post your .java file as well?
Posted 11 months ago # -
Here you go
public class HelloWorld
{
public static void main(String args[])
{
//#if defined(PROJ1) <<fails right here
System.out.println("Hello Project 1: ${PROJ1}");
//#else
System.out.println("Hello Project 2: ${PROJ2}");
//#endif
}
}Posted 11 months ago # -
I also added this to supress an ant warning: includeantruntime="false"
and the dependancy for preprocess<target name="compile" depends="preprocess">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false"/>
</target>Posted 11 months ago # -
Strange. I don't seem to have the same problem in my tests. I did however get an error due to the PROJ2 property being undefined. Here is my test files:
# build.xml
<project default="preprocess"> <taskdef resource="javapp-defs.xml" /> <target name="preprocess"> <javapp destdir="staging" prefix="//#"> <fileset dir="src" includes="**/*.java" /> <property name="PROJ1" value="SC650" /> <property name="PROJ2" value="SC750" /> </javapp> </target> </project># HelloWorld.java
public class HelloWorld { public static void main(String args[]) { //#if defined(PROJ1) System.out.println("Hello Project 1: ${PROJ1}"); //#else System.out.println("Hello Project 2: ${PROJ2}"); //#endif } }Posted 11 months ago # -
Success - the fundamental problem was the //#ifdef, this does not seemed to be supported. I changed it to //#if defined(PROJ1) and that works
This was an assumption on my part - I'll read the syntax doc a little closer.One oddity:
If I defined PROJ1 and PROJ2 in build.xml - the preprocess works, PROJ1 is printed outIf I defined ONLY PROJ1 - it fails, stating: property 'PROJ2' not defined
If I ONLY define PROJ2 - it works, PROJ2 is printed out
Am I missing something here - It seems the code would be VERY dependant on how the
"if defined() is used/ordered and how the properties are declared.If I defined only PROJ1, I would expect the PROJ2 to be preprocessed out and no error to be thrown?
Thanks for the help. I appreciate it.
AdamPosted 11 months ago # -
I found another oddity - perhaps you could comment on.
If I change the property definitions in ant's build.xml file, this does not cause a new preprocess to occur, I believe because the src files did not change. That it turn does not cause a recompile of the files.
The result is the build does not match the required definitions.If I clean and and run again, I get what I expected.
Is there a way to force the preprocess to occurs based on the change of the build.xml file? - This may be more of an ant question than javapp question.
Thanks,
AdamPosted 11 months ago # -
The behavior with PROJ1 and PROJ2 properties is most certainly a bug (or more appropriately something I didn't consider closely enough while developing javapp).
Regarding the property changes in the build.xml file not causing a re-compile, yes I think this is really better left up to the ant script itself. For example use the <uptodate> task to check if build related files are newer than pre-processor output.
<target name="clean" if="buildxml_is_newer"> <delete dir="staging" /> </target> <target name="preprocess" depends="check-build-files,clean"> ... </target> <target name="check-build-files"> <uptodate srcfile="build.xml" property="buildxml_is_newer"> <srcfiles dir="staging" includes="**/*" /> </uptodate> </target>Posted 11 months ago # -
Great, Thanks
I resolved the PROJ1 and PROJ2 by defining a seperate control property ACTIVE_PROJECT.
I also discovered that using the #if ${prop} == xxx, that xxx must be a numeric value.
if you make changes in the future, a string based comparison would be useful too.ACTIVE_PROJECT=MyProject vs. ACTIVE_PROJECT=1234
Thanks for the help, I have a working ant script and controls now
Posted 11 months ago # -
If you surround the literal "xxx" with double quotes string comparison is (*should*) be performed.
Also to overcome the issue with a property being undefined you don't even need to give it a value in Ant. Something like ACTIVE_PROJECT= would suffice.
There should be section in the README regarding the types of comparisons available and the syntax for the literal values inside expressions.
Posted 11 months ago #
Reply
You must log in to post.