How to recompile with -Xlint:unchecked in Ant build task? - ant

When I run the "compile" target of my Ant "build.xml" file, then I get the following message:
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
My compile target is the following:
<target name="compile">
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" debuglevel="lines,source" includeantruntime="false">
<classpath refid="class.path" />
</javac>
<javac srcdir="${test.dir}" destdir="${classes.dir}" debug="true" debuglevel="lines,source" includeantruntime="false">
<classpath refid="class.path" />
</javac>
</target>
What do I have to change in my build.xml file so that -Xlint:unchecked is done there?

Add the following element in <javac></javac> section:
<compilerarg value="-Xlint:unchecked" />

In AndroidStudio, do this:
allprojects {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:deprecation"
}
}

Related

Ant - Generate .class files even if compiler errors

How to successfully build using Ant, if the code has compilation errors?
If I have 3 .java files and 1 has a compilation error, is there anything that can make my build successful and can give me the remaining 2 .class files?
<target name="build" description="Compiles the Source code" depends="Directory.check">
<echo>Compilation Starts</echo>
<javac failonerror="false" includeantruntime="false" destdir="${build.dir}"
deprecation="false" optimize="true" executable="${exec.dir}">
<compilerarg value="-Xlint:unchecked"/>
<compilerarg value="-Xlint:deprecation"/>
<src path="${src.dir}"/>
<classpath refid="master-classpath"/>
</javac>
<echo>Compilation Ends</echo>
</target>
Iterate though src files using for from ant-contrib.
Pass the file name to javac in includes attribute with the src basedir as srcdir.
<target name="compile" depends="init" description="compile the source ">
<for param="file">
<path>
<fileset dir="${src}" includes="**/*.java" excludes="**/Sanity/*.java"/>
</path>
<sequential>
<local name="program"/>
<basename property="program" file="#{file}" suffix=".java"/>
<javac includeantruntime="false" srcdir="${src}" debug="on" includes="**/${program}.java"
excludes="**/Sanity/*.java" destdir="${build}" failonerror="false"
verbose="true">
<compilerarg value="-Xbootclasspath/p:${toString:lib.path.ref} -Xlint:deprecation -Xlint:unchecked"/>
</javac>
</sequential>
</for>
</target>

jdtCompileLogPublisher javac

I have ant build script that compiles and generates the compilation logs(in file compileLog.xml) of my project. I use jdtCompileLogPublisher to publish these results the RTC.
Now the situation is when i have the sources in the project its working fine. When i don't have any sources in the project(this is a one of the requirement that there will be an empty project with out sources), compilation step is skipped as there are no sources,and jdtCompileLogPublisher is failing because, the file compileLog.xml which is an input for this step is not created as compilation itself has been skipped.
I tried creating a new compileLog.xml if it is not available. As a result of this I'm encountering new issues as file is empty.
Why javac is skipped when there are no java files to compile?
Is there any possibility to make the compiler to execute javac in build.xml
Version ant: 1.7.1.
snippet of my code related to :
<target name="compile" depends="prepare" description="Compile the source code">
<mkdir dir="${nameBuild}/${dir.build}/bin" />
<!-- Add target ${label.jdkversion} to make it possible to build with a certain jdk version-->
<javac compiler="org.eclipse.jdt.core.JDTCompilerAdapter" destdir="${nameBuild}/${dir.build}/bin" debug="true" deprecation="on" encoding="ISO-8859-1" source="${buildInfo.jdkVersion}" target="${buildInfo.jdkVersion}" debuglevel="lines,source" failonerror="false" errorProperty="buildFailed">
<compilerarg line="-warn:+raw" />
<compilerarg line="-warn:-serial" />
<compilerarg line="-enableJavadoc" />
<compilerarg line="-log source/${name}/compileLog.xml" />
<src path="${name}/${dir.src}" />
<classpath refid="application.classpath" />
</javac>
<jdtCompileLogPublisher buildResultUUID="${buildInfo.buildResultUUID}" repositoryAddress="${repositoryAddress}" userId="${userId}" passwordFile="${psFile}" filePath="compileLog.xml" />
<fail if="buildFailed" message="Compile ${name} failed." />
</target>
I modified the tag
<jdtCompileLogPublisher buildResultUUID="${buildInfo.buildResultUUID}" repositoryAddress="${repositoryAddress}" userId="${userId}" passwordFile="${psFile}" filePath="compileLog.xml" />
to
<jdtCompileLogPublisher buildResultUUID="${buildInfo.buildResultUUID}" repositoryAddress="${repositoryAddress}" userId="${userId}" passwordFile="${psFile}" filePath="compileLog.xml" failonerror="false" />
Which solved my problem

ant javac can't find other project compiled classes

The following is a simplified version of my ant script (it's got the project element etc).
I'm new to ant and unable to figure out why 'compileTests' doesn't compile, whereas 'compileFoo' does.
The error I get is 'package does not exist' as the class in the compileTests project can't find the compiled classes in the compileFoo project, even though they've compiled fine, i can see them on the file system and the path to them is listed in the classpath (i assume this is necessary?)
Clearly there is something basic I don't understand. Can someone please help by explaining?
<path id="build_classpath">
<fileset dir="${other_required_jars}" includes="**/*.jar" />
<fileset dir="${foo_build_location}" includes="**/*.class" />
</path>
<target name="compileFoo" description="compile">
<javac srcdir="${foo_source_directory}\test-src" includeantruntime="false" destdir="${foo_build_location}" includes="**/*.java" excludes="" debug="on" optimize="off" deprecation="on" verbose="on">
<classpath refid="build_classpath" />
</javac>
</target>
<target name="compileTests" description="compile">
<javac srcdir="${test_source_directory}\test-src" includeantruntime="false" destdir="${test_build_location}" includes="**/*.java" excludes="" debug="on" optimize="off" deprecation="on" verbose="on">
<classpath refid="build_classpath" />
</javac>
</target>
Your classpath is wrong. A classpath doesn't contain a set of .class files. It cntains a set of jar or directories, each containing the root of a package tree. So the classpath should simply contain one element : ${foo_build_location}:
<path id="build_classpath">
<fileset dir="${other_required_jars}" includes="**/*.jar" />
<pathelement location="${foo_build_location}"/>
</path>

Ant optional classpath element

I have some modules and a main runnable project. I have common build file, and
build.common.xml
<target name="build" >
<path id="libraries.classpath">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath refid="libraries.classpath" />
<classpath refid="modules.classpath" />
</javac>
</target>
..and every module declares its own dependencies in their build.xml:
<path id="modules.classpath">
<pathelement path="../ModuleA/${build.dir}" />
...
</path>
The problem is if there is no internal dependency I get the following exception:
"Reference modules.classpath not found."
What is the solution for that? How could I declare an optional classpath element?
Note: If anybody want to suggest to create jars out of my modules, please justify this. I'm going to have 5-10 rapidly changing modules, and I don't want to do unnecesary steps in the build process.
Update: I extracted the build into two different targets and created a condition for them, but did not help (it echoes the 'false' and builds with module-dependencies):
<target name="build">
<condition property="modules.classpath.set" else="false">
<isset property="modules.classpath"/>
</condition>
<echo message="modules.classpath is set: ${modules.classpath.set} " />
<antcall target="build-with-modules" />
<antcall target="build-without-modules" />
</target>
<target name="build-with-modules" if="modules.classpath.set">
<echo message="Building with module-dependencies" />
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath refid="libraries.classpath" />
<classpath refid="modules.classpath" />
</javac>
</target>
<target name="build-without-modules" unless="modules.classpath.set">
<echo message="Building with no dependent modules" />
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" source="1.6">
<classpath refid="libraries.classpath" />
</javac>
</target>
Condition isreference:
Test whether a given reference has been defined in this project and - optionally - is of an expected type.
So, try
<condition property="modules.classpath.set" else="false">
<isreference refid="modules.classpath"/>
</condition>
Also on that page, there is a link to a page that describes custom conditions. If none of the provided conditions meets your requirement, then just write one.
Update:
The logic of if and unless in <target> is to check if the property has been set -- for if, the target runs when the property has been set; for unless, the target runs when the property has NOT been set -- not the value of the property.
I have never checked the code of the condition isreference, but I think maybe the else="false" should be removed.
If removing that part still doesn't help, then you may need to use some embedded groovy or beanshell script, or write your own condition.

ant + javac + properties

In an ant script, I would like to compile only certain packages e.g.
com.example.some_package.foo
com.example.some_package.bar
This is what I want to do, but it doesn't seem to work, because property substitution doesn't seem to work in the <include> tag:
<property name="ROOT_PKG_PATH" location="com/example/some_package"/>
...
<target name="compile-client" depends="init">
<javac srcdir="${srcDir}"
destdir="${buildDir}"
debug="on"
target="1.5"
classpathref="build.classpath">
<include name="${ROOT_PKG_PATH}/foo/**" />
<include name="${ROOT_PKG_PATH}/bar/**" />
</javac>
</target>
How can I get around this without having to retype the entire package path of each package?
Use the value attribute on the property, instead of location:
<property name="ROOT_PKG_PATH" value="com/example/some_package"/>
Example
I'm able to conditionally compile one of my java classes:
./src/some_package/demo1/Demo.java
./src/some_package/demo2/Demo.java
./build/classes/somepackage/demo1/Demo.class
./build.xml
Using the following ANT file:
<project name="demo" default="compile">
<property name="prop" value="some_package/demo1"/>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes">
<include name="${prop}/**"/>
</javac>
</target>
</project>

Resources