I have an annotation processor that is depended on other libraries e.g. Guava, Commons. I already set it up with Eclipse and Maven. However, I want to use it with an Apache Ant's build file. So, I created following target:
<target name="compile.frontend" depends="compile.main">
<mkdir dir="${target}target/frontend/classes" />
<javac encoding="utf-8" destdir="${target}target/frontend/classes" debug="on" nowarn="true" source="1.6" target="1.6" optimize="on" includeantruntime="false">
<src path="src/application/java"/>
<classpath>
<fileset dir="lib">
<include name="main/*.jar" />
</fileset>
<pathelement location="${target}target/main/classes" />
</classpath>
<compilerarg line="-processorpath ${basedir}/lib/main/MyProcessor-1.0.jar;${basedir}/lib/main/javapoet-1.7.0.jar;${basedir}/lib/main/guava-17.0.jar;${basedir}/lib/main/commons-lang-2.6.jar"/>
<compilerarg line="-processor com.me.MyProcessor"/>
<compilerarg line="-s ${target}target/frontend/classes/"/>
</javac>
</target>
The error I get is the processor-generated class I instantiated is missing, "cannot find symbol".
The dependencies I added with a ";" separated list are the same dependencies I made Eclipse use the processor properly. I suspect from the output folder <compilerarg line="-s..., since when I remove a dependency i.e. JavaPoet I get a "java.lang.NoClassDefFoundError" of JavaPoet. Any suggestion?
The problem was regarding some directory exclusion using exclude name under javac lines:
<exclude name="**/folder_name/**"/>
There is also no need to add compilerarg lines as I did at the original question. Having the libs under classpath is enough.
Related
I am trying to include the javafx.controls module in my project and compile it through ant, rather then use javaFX javac --module-path "..\lib\javafx-sdk-17.0.2\lib" --add-modules javafx.controls App.java
java --module-path "..\lib\javafx-sdk-17.0.2\lib" --add-modules javafx.controls App commands to compile the file(s).
This is how my ant build.xml looks like right now:
<project name="projectName" default="dist">
<property file="build.properties"/>
<property file="${user.home}/build.properties"/>
<path id="run.classpath">
<fileset dir="${dist.dir}">
<include name="${project.name}.jar"/>
</fileset>
</path>
<target name="compile">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}"
destdir="${build.dir}"
debug="${compile.debug}"
deprecation="${compile.deprecation}"
optimize="${compile.optimize}"
includeantruntime="false">
<classpath>
<fileset dir="${lib.dir}/javafx-sdk-17.0.2/lib">
<include name="**/*.jar" />
</fileset>
</classpath>
<compilerarg line="--add-modules ${lib.dir}/javafx-sdk-17.0.2/lib/javafx.controls"/>
</javac>
</target>
<target name="dist" depends="compile">
<mkdir dir="${dist.dir}"/>
<jar jarfile="${dist.dir}/${project.name}.jar"
basedir="${build.dir}"
manifest="${src.dir}/Manifest.mf">
<zipgroupfileset dir="${lib.dir}/javafx-sdk-17.0.2/lib" includes="**/*.jar"/>
</jar>
</target>
<target name="build" depends="dist">
<java jar="${dist.dir}/${project.name}.jar" fork="true">
</java>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>
</project>
The error I keep on getting is: error: bad name in value for --add-modules option: 'C:\xampp\htdocs\projectName/lib/javafx-sdk-17.0.2/lib/javafx.controls'. I have looked up the error, but the answers are very vague in my opinion and have not helped me get more knowledge in regards to the error.
What you are doing wrong
You are providing an incorrect value to --add-modules.
You are not providing a string to ant that matches the example command in your question.
In your question, for the example javac command, you have:
--module-path "..\lib\javafx-sdk-17.0.2\lib" --add-modules javafx.controls
but in your ant script you have
--add-modules ${lib.dir}/javafx-sdk-17.0.2/lib/javafx.controls
You have tried to compress the --module-path argument with the --add-modules argument. Those arguments must remain separate and have different values.
ant javac support for the Java Platform Module System
The ant javac task has a few parameters for working with the Java module system, you should use appropriate ones:
modulepath
Use this to reference the directory set containing your JavaFX modules (e.g. the SDK lib directory).
Do not put the JavaFX libraries on the class path, they are modules and belong on the module path.
Specific changes required for your ant script
I am not familiar enough with the ant argument syntax to provide the exact ant XML elements for the correct definitions of the arguments for your case.
If somebody knows the exact values required in ant element syntax, they can edit this answer and put the values here.
Background info
Read the javac man page if you want to understand what the --module-path and --add-modules arguments are, what data should be supplied to them, and the format required for that data.
Also review understanding java 9 modules.
Alternate approach: defining module info
As an alternative to adding modules via the --add-modules VM argument, you can instead define a module-info.java file in your application to make it modular and require the modules there.
This will work fine if you aren't depending on non-modular code.
Even if you define a module-info.java file, you still need to set the --module-path for the compiler and runtime so that it can find the module implementations. Build tools such as Maven will do this automatically for defined dependencies.
However, ant isn't aware of build dependencies and needs manual help with parameters to support the module system. You will need to set the module path manually via modulepath arguments to the compiler. Similarly, you will need to set VM arguments for runtime module support if you have a java execution task in ant.
solution
<target name="compile">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}"
destdir="${build.dir}"
debug="${compile.debug}"
deprecation="${compile.deprecation}"
optimize="${compile.optimize}"
includeantruntime="false">
<classpath>
<fileset dir="${lib.dir}/javafx-sdk-17.0.2/lib">
<include name="**/*.jar" />
</fileset>
</classpath>
<compilerarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
<compilerarg line="--add-modules javafx.controls"/>
</javac>
</target>
<target name="build" depends="dist">
<java jar="${dist.dir}/${project.name}.jar" fork="true">
<jvmarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
<jvmarg line="--add-modules javafx.controls"/>
</java>
</target>
First use the compilerarg with the line, to indicate what you want to compile during the javac process.
<compilerarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
<compilerarg line="--add-modules javafx.controls"/>
You then provide these lines within the java process as well.
<jvmarg line="--module-path ${lib.dir}/javafx-sdk-17.0.2/lib/"/>
<jvmarg line="--add-modules javafx.controls"/>
I am beginner in Maven and Ant projects.I have a trouble with some Ant tasks.I don't know how to build class-path of dependencies in Ant; while it can be done in maven projects by:
mvn dependency:build-classpath
How can i do above task in Ant?
You can specify it using classpath tag in your build.xml you can refer example below
<target name="compile" depends="init" description="compile the source ">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}/classes" includeantruntime="false">
<classpath>
<fileset dir="${hadoop.home}/">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${hadoop.home}">
<include name="hadoop-common-*.jar"/>
</fileset>
</classpath>
</javac>
</target>
Ant is more like an imperative programming language. You don't have the amount of meta-information in your build.xml as you have in a pom.xml. The easiest thing would be to add some code to your ant "compile" target that writes the classpath to a file when you do a compile.
You can also externalise the classpath in a separate libraries.xml file (that was done in our company when we used Ant) which allows you to edit and read the data more easily. You could even write another ant target for that.
I'm new with ANT and I'm trying to compile part of my Eclipse project. I have many classes but i need to pack only a part of it (please don't ask why). My problem is that one of these classes references to an external library placed in the <project_root>/libs folder and I did not find out how to link it. I found examples on the web but I was not able to arrange it.
<path id="classpath">
<fileset dir="libs" includes="**/*.jar"/>
</path>
<target name="compile">
<mkdir dir="client/classes"/>
<javac srcdir="src" destdir="client/classes" sourcepath="classpath">
//include needed java files
</javac>
</target>
I'm using the annotation #Remote of EJB. It's in the javax.ejb package. I get the error:
package javax.ejb does not exist
[javac] import javax.Ejb.Remote;
If I understand your project structure correctly, the issue with your ant file is that you reference the classpath as beeing the sourcepath.
sourcepath / sourcepathref point to locations where source can be found. I suppose what you want is classpathref="classpath".
<javac srcdir="src" destdir="client/classes" classpathref="classpath">
//include needed java files
</javac>
try this one
<classpath>
<fileset dir="libs">
<include name="**/*.jar"/>
</fileset>
</classpath>
I have the following file structure:
ServerCode <- src , libs, bin
I am trying to compile all the code in src. Src has a couple of .java files at the top level and sub-directories. libs contains all the .jar files which are required to build my project.
I wrote the following build.xml but when I try to compile it, the compiler throws errors cannot find symbol errors for the libraries I am including.
<project default="compile">
<target name="compile">
<mkdir dir="bin"/>
<javac srcdir="src" destdir="bin" classpath="libs/*.jar">
</target>
</project>
Define class path to include all jars like this
<target name="compile" depends="" description="compile the java source files">
<javac srcdir="." destdir="${build}">
<classpath>
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${test_lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
I don't think you can use a pattern in the classpath attribute. I could be wrong about this. You should run ant in verbose mode (the -v option) to see how it's using your classpath attribute. I suspect it's passing it to javac as a literal string.
Here's what I do:
<javac target="${javac.target}" source="${javac.source}" destdir="${validator.output.dir}" debug="on"
nowarn="off"
memorymaximumsize="128m" fork="true">
<classpath refid="validator.module.production.classpath"/>
<src>
<dirset dir="Validator">
<include name="src"/>
</dirset>
</src>
</javac>
...
<path id="validator.module.production.classpath">
<fileset dir="Validator/lib/-validator" includes="**/*.jar"/>
</path>
Some of this code is generated by my IDE so it's a little verbose, but you get the idea.
Try this as mentioned at http://ant.apache.org/manual/using.html instead of giving classPath attribute alongwith javac
<classpath>
<pathelement location="libs/*.jar"/>
</classpath>
There are other ways also which you can glance thru the link mentioned above
I'm having a problem getting the javac used by Ant to find and use certain packages. When I invoke javac directly from the command line the packages are found and used.
The .jar files are located in my home directory under lib/java. This is my classpath:
/home/bliskovs/lib/java/*:/home/bliskovs/vendor/cytoscape-v2.7.0/cytoscape.jar
This is the relevant section in my build.xml:
<target name="compile">
<javac srcdir="." debug="true"/>
<javac srcdir="tools/" debug="true"/>
<javac srcdir="core/" debug="true"/>
</target>
How can I get Ant to recognize these packages?
Check out this.
<property name="build.classes.dir" location="build/classes"/>
<path id="compile.classpath">
<fileset dir="lib"/>
<pathelement location="/home/bliskovs/vendor/cytoscape-v2.7.0"/>
</path>
<target name="compile" description="Compile src dir">
<javac destdir="${build.classes.dir}" debug="true" includeantruntime="true">
<src location="src"/>
<classpath refid="compile.classpath"/>
</javac>
</target>
Define a classpath for the javac task. Relying on the CLASSPATH environment variable is a bad practice. It's even more true for the build process of a project, which should work without having to setup a whole lot of environment variables. If you start developing three or four projects at once, you'll understand why using a single CLASSPATH env variable is a bad idea.
See http://ant.apache.org/manual/Tasks/javac.html to know how to define a classpath inside the build.xml and use it in the javac task.