JPAAnnotationProcessor processing files excluded by ant - ant

I'm not sure whether this is a question concerning ant or the JPAAnnotationProcessor of QueryDSL.
I have a following target in the build.xml:
<target name="querydsl" depends="-prepare">
<javac srcdir="${src.java.home}" classpathref="compile.classpath" includeantruntime="false">
<include name="de/foo/bar/database/model/**"/>
<compilerarg value="-proc:only"/>
<compilerarg value="-processor"/>
<compilerarg value="com.mysema.query.apt.jpa.JPAAnnotationProcessor"/>
<compilerarg value="-s"/>
<compilerarg value="${src.java.generated.home}"/>
</javac>
</target>
and I only want the classes located under de/foo/bar/database/model/ to be processed.
When I execute ant querydsl I get compile errors from classes that are located outside of the ant include parameter, e.g.
[javac] C:\projects\main\cxlBackend\src\main\java\de\foo\bar\database\service\company\CompanyServiceImpl.java:96: cannot find symbol
[javac] symbol : class QAccount
I tried to explicitly exclude this class with
<exclude name="de/foo/bar/database/service/company/CompanyServiceImpl.java"/>
but I'm still getting the same error.
Edit: At the beginning, ant writes out:
[javac] Compiling 267 source files
That's the exact number of classes in the de/foo/bar/database/model/ directory.

These warnings are printed by javac since the Q-classes are not yet available.
According to the javac docs you can suppress the warnings with the -nowarn flag.

The problem went away after an upgrade to Java 7 (from Java 6).

Related

First Ant build.xml file not compiling as detecting errors plus other ant questions

I am trying to write my first build.xml file for an established java project that has just been moved from Netbeans.
A. The objectives that I'm trying to meet are pretty simplistic:
Using the "dest" target below, copy all the source files (4 in all from 1 package) to src/test that I am trying to create. The source files were copied to the "src/test" directory but then a "test" directory was also getting created in the "src/test" directory, why I'm not sure.
Using the "jar" target below, create a jar that has all the class files under the package name directory - DID NOT WORK AT ALL!
Using the "compile" target to ensure that all the code is compiled successfully but I got a lot of errors. The code does CLEAN and BUILD successfully in Eclipse so I'm not sure what I did wrong in the ANT script and one thing I noticed was that it was trying to compile "8" files when there are only "4". Not sure where the other 4 are coming from though it indicates a duplication. The errors show with regard to a missing symbol seem to refer to import statements regarding required projects that are included in the build path so I'm not sure how to address the issues ANT raises in its compile.
B. Here is my first attempt at creating my first build.xml file but I"m experiencing the problems shown below:
<project name="ThalesDataGenerator" basedir="." default="clean-build">
<property name="src.dir" value="src"/>
<property name="dest.dir" value="${src.dir}/test"/>
<property name="dist.dir" value="dist"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/${ant.project.name}"/>
<property name="main-class" value="thalesdatagenerator.ThalesDataGenerator"/>
<target name="clean">
<delete dir="${build.dir}"/>
<delete dir="${dest.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="dest">
<mkdir dir="${dest.dir}"/>
<copy todir="${dest.dir}">
<fileset dir="${src.dir}" includes="**"/>
</copy>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/ThalesDataGenerator.jar" basedir="${build.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,dest,jar,run"/>
</project>
Here are the errors I got:
> Buildfile: C:\ATMSwitch\ThalesDataGenerator\build.xml clean:
> [delete] Deleting directory C:\ATMSwitch\ThalesDataGenerator\build
> dest:
> [mkdir] Created dir: C:\ATMSwitch\ThalesDataGenerator\src\test
> [copy] Copying 4 files to C:\ATMSwitch\ThalesDataGenerator\src\test
> [copy] Copied 2 empty directories to 1 empty directory under C:\ATMSwitch\ThalesDataGenerator\src\test
> compile:
> [mkdir] Created dir: C:\ATMSwitch\ThalesDataGenerator\build\classes
> [javac] C:\ATMSwitch\ThalesDataGenerator\build.xml:22: warning: 'includeantruntime' was not set, defaulting to
build.sysclasspath=last; set to false for repeatable builds
> [javac] Compiling 8 source files to C:\ATMSwitch\ThalesDataGenerator\build\classes
> [javac] C:\ATMSwitch\ThalesDataGenerator\src\thalesdatagenerator\ISOUtil.java:36:
duplicate class: thalesdatagenerator.ISOUtil
> [javac] C:\ATMSwitch\ThalesDataGenerator\src\test\thalesdatagenerator\ThalesDataGenerator.java:13:
package common.database does not exist
> [javac] import common.database.Database;
> [javac] ^
> [javac] C:\ATMSwitch\ThalesDataGenerator\src\test\thalesdatagenerator\ThalesSystem.java:13:
package com.sharpbancsystems.atmterminals.thales does not exist
> [javac] Note: C:\ATMSwitch\ThalesDataGenerator\src\test\thalesdatagenerator\ThalesDataGenerator.java
uses unchecked or unsafe operations.
> [javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 18 errors
BUILD FAILED
C:\ATMSwitch\ThalesDataGenerator\build.xml:22: Compile failed; see the compiler error output for details.
Total time: 874 milliseconds
Any help/direction would be greatly appreciated. Regards.
Let's take this one at a time:
A few hints:
Use ${basedir} when defining properties. For example, <property name="src.dir" value="${basedir}/src"/> instead of simply value="src"/>
Don't force a clean as part of your default target. The standard default target should build the jar and that's it. Doing a clean makes you duplicate work that may not be needed. You can use a target named all to clean, build, and execute.
As mentioned above, use the default target names clean to clean up your build, and all to run all targets in your build. Neither of these should be the default target.
Now back to your issue. Your <javac> target looks like this:
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
Are you saying that there are no third party jars that your source depends upon? You need to create a compile classpath that includes all of the third party jars your source depends upon. In Eclipse, there's a built in classpath you're using. In Ant, you have to specify this.
Let's assume that all jars you need for your source to compile are stored in ${basedir}/lib. Your compile target needs to look like this:
<target name="compile">
<mkdir dir="${classes.dir}"/>
<path id="compile.classpath">
<fileset dir="${basedir}/lib">
<include name="**/*.jar"/>
</fileset>
</path>
<javac srcdir="${src.dir}"
classpathref="compile.classpath"
destdir="${classes.dir}"/>
</target>
There are many ways of doing this, but this is the easiest syntactically. I use <path> to define a compile.claasspath that contains all of the jars I need. Since they all live in the lib directory, it was pretty easy.
Next, I use the classpathref parameter to specify this classpath when I compile my Java code.
This will get the classes compiled (which isn't happening now). Since compile target fails, the Ant build ends there before the <jar> task is called. Getting the compile to work should allow the rest of your build to work.
happened to me. was because i hadn't copied selenium-server-standalone-3.4.0.jar to my C:\jars dir (ws.jars)

I wonder if there is something wrong with my ant junit task

I'm writing my build.xml. However, it seems there is something go wrong with my junit task. when I run my junit task. I can build success, but the junit report only show that run 1 test with error. But I have more than 10 tests. So I wonder if there is something run with my junit task. Here is my code.
<property name="src.dir" value="src"/>
<property name="bin.dir" value="bin"/>
<property name="dest.dir" value="dest"/>
<property name="test.dir" value="test/>
<property name="lib.dir" value="lib"/>
<path id="classpath">
<pathelement location="${lib.dir}/junit-4.11.jar"/>
<pathelement location="${lib.dir}/ant-junit4.jar"/>
</path>
<target name="test" depends="compile">
<junit printsummary="yes" haltonfailure="no">
<classpath>
<pathelement location="${bin.dir}"/>
<path refid="classpath"/>
</classpath>
<formatter type="plain" usefile="false"/>
<batchtest fork="yes">
<fileset dir="${test.dir}" includes="*Test*.java"/>
</batchtest>
</junit>
</target>
I cannot figure out what is wrong so could somebody help me out?
And what is happening? Do you get any error messages?
You usually need to do the following:
Compile your normal code. The resulting *.class files should be placed inside a directory such as target/classes or build/classes. Use the destdir parameter of the <javac> task to do this.
Compile your JUnit tests.
In your classpath, you need all of the jars you needed to compile your normal classes
You need a reference to the destdir where your normal classes were compiled to.
You need the JUnit jar.
These should be compiled to a different directory from your normal jars. Normally, this is target/test-classes or build/test-classes.
Once you've compiled the JUnit tests, you may run them. You can use the <junit> task like you did.
You need to make sure that the includeantruntime parameter is set to true
You should also set fork to true.
You need the same classpath (with all three elements you had) when you compiled the test classes. More jars might be needed, but usually not.
You run the tests on the compiled JUnit test classfiles (the ones you saved to target/test-classes or build/test-classes. In your example, you're trying to run them against the source.
I use the Maven standards for my directory layout. That means my Java source is under src/main/java while my JUnit Java files are under src/test/java. Any XML or properties or other none source files needed are stored in src/main/resources. The regular source is compiled to target/classes while the Junit sources are compiled to target/test-classes.
This makes it easy to compile your code and test code separately without worrying about **/test/**, **/Test/**, **/JUnit/** exceptions in directory compiling since everything is separate.
Hope this helps.

Ant: javac with proc:only from Ant?

Is there any way to enforce javac task to invoke only annotation processing, without compilation. -proc:only javac option is supposed to enforce such behaviour, according to javac documentation.
But the following snippet of ant buildfile:
<target name="apt">
<javac srcdir="src" destdir="bin" includeantruntime="false">
<compilerarg value="-proc:only"/>
</javac>
</target>
Compiles project anyway. I experimented with other <compilerarg> tag versions (e.g. line instead of value) but nothing helped.
Avoid specifying the "destdir" attribute of the task, or use an empty directory as a destination for class files. The Ant "javac" task will then look for the class files either in the base directory (if you leave "destdir" unset) or in the empty directory. Because it will not find the classs files there, it will not exclude the potentially up-to-date sources from the compilation and execute "javac" on the sources from the directory specified in the "srcdir" attribute.
Your code would therefore look like this:
<target name="apt">
<javac srcdir="src" includeantruntime="false">
<compilerarg value="-proc:only" />
</javac>
</target>
or, if you use the empty directory approach, like this:
<target name="apt">
<mkdir dir="empty" />
<javac srcdir="src" destdir="empty" includeantruntime="false">
<compilerarg value="-proc:only" />
</javac>
</target>
The second approach is slightly more complex but a bit more clean. Usually your project will have an output directory where you put compiled classes and packaged jars, so adding an extra empty directory there would not hurt. As of Ant version 1.9.4, I did not find any other way to do annotation processing from Ant independently of compilation, even though a simple "force" attribute in the "javac" task could solve this problem.

Why does ANT javac task recompile my .java again and again

The source and destination directory will be recursively scanned for
Java source files to compile. Only Java files that have no
corresponding .class file or where the class file is older than the
.java file will be compiled.
The above is from javac Task, ANT Apache. But I really don't see why in my case .java is recompiled again and again.
My working directory is .../trunk
My source (.java) is located at trunk/src
My target (.class) is located at trunk/bin
My .java files use default package, namely, no package declaration.
The javac task is used this way in my build.xml
<javac srcdir="${src}"
destdir="${bin}"
includeantruntime="false">
<classpath>
<pathelement location="${bin}"/>
<pathelement path="${java.class.path}"/>
</classpath>
</javac>
where I have defined
<property name="src" value="src"/>
<property name="bin" value="bin/"/>
It seems everything is ok, but each time I run ANT, it recompiles the .java files. Really strange! Anyone would like to tell me why? Thanks.
Oh, I got the answer. Sorry I did not tell all the story above. Actually, I have an antlr lexer/parser generation before the "javac" part. And for that, I should have added the "-make" option so that the lexer and parser will not be generated again with a more recent timestamp. (from Use ANT for ANTLR3
The ANLTR3 command-line option "-make" only generates new files in
case they are older than the grammar. This behaviour might have an
effect on dependent tasks like "compile", which could result in
nothing to be processed because it is up to date.

Ant javadoc generation of tests code cannot find symbol from src code

In my project, I have a src folder with code source of the application and test folder with code source of the application tests.
In my Ant build, I would like to separate javadoc generation of these source codes. For the src code javadoc generation, there is no problem but for the tests code javadoc generation, I've got a problem because test code uses src code.
My Ant task to generate javadoc is like that :
<path id="classpath-test">
<pathelement path="." />
<pathelement path="${testclasses.home}" />
<pathelement path="${classes.home}" />
<fileset dir="${lib.home}" includes="*.jar" />
<fileset dir="${libtest.home}" includes="*.jar" />
</path>
<target name="compile" ... > // compiles src code of the project in ${classes.home}
<target name="compile-tests" depends="compile">
<javac srcdir="${test.home}"
destdir="${testclasses.home}"
target="1.5"
source="1.5"
debug="true"
>
<classpath refid="classpath-test" />
</javac>
<copy todir="${testclasses.home}">
<fileset dir="${test.home}">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="generate-javadoc-tests" depends="compile-tests" >
<javadoc sourcepath="${test.home}" packagenames="*"
destdir="${test-javadoc.home}" verbose="false"
linksource="true" encoding="${encoding}">
<classpath refid="classpath-test" />
</javadoc>
</target>
The ${test.home} variable is test folder. In the classpath-test, I put jar from junit to avoid error about annotation specifics to junit during javadoc generation. This jar is contained in ${libtest.home}.
When I generate javadoc, I have several warnings about code from test folder that using code from src folder which is normal. The errors are like that :
[javadoc] E:\workspace\app\test\com\app\MyClass.java:9: package com.app.SrcClass does not exist
[javadoc] symbol : class MyClass
[javadoc] location: class com.app.MyClass
So, someone knows a way to includes src classes in classpath to avoid these warnings but without having source code javadoc included in test code javadoc.
Or may be a way to disable these warnings because the verbose option of javadoc task to false doesn't disable these warnings.
So, someone knows a way to includes src classes in classpath to avoid
these warnings but without having source code javadoc included in test
code javadoc.
Make sure the classes on which your tests depend are on the classpath. You might want to make your javadoc generation target dependent on the target which compiles code from src and builds a jar file. Then make sure that the classpath, referenced by refid classpath-test includes that jar.

Resources