Override the compiler attribute in an Ant javac task - ant

I'm trying to override Ant compiler attributes via the command line so that all 'javac' tasks use my specified compiler.
The problem I've run into is that any target that sets its own value for compiler overrides the one that I set at the commmand line. So, even though I'm entering the following command.
ant -Dbuild.compiler=mycompiler
Any target that has the following is going to use the modern compiler instead of mycompiler because of that compiler="modern" attribute
<javac srcdir="."
destdir="${classes.dir}/core"
compiler="modern"
encoding="UTF-8">
<include name="org/**" />
<include name="com/**" />
<compilerarg line="${config.build.compilerarg}" />
</javac>
Is there any way to override this from the command line, or am I stuck editing the build file?

The Ant javac task documentation says:
It is possible to use different compilers. This can be specified by either setting the global build.compiler property, which will affect all tasks throughout the build, or by setting the compiler attribute, specific to the current task. Valid values for either the build.compiler property or the compiler attribute are:
It sounds as if you can either specify the global build.compiler property or set a specific compiler attribute.
So, it looks like you will need to modify your build file and either:
remove the compiler attribute from the javac calls and allow the
global build.compiler setting to
cascade down
change the values of the compiler
attribute from a hard-coded string
compiler="modern" to be property
compiler="${javac.compiler}"

Related

Ant global <compilerarg> property

I need to set two global properties to be used with invocation of javac in my ant script.
Currently I have this in each of my javac invocations:
<compilerarg compiler="org.eclipse.jdt.core.JDTCompilerAdapter" line="-1.6 -warn:none"/>
I tried to set a global property like this
<property name="compiler.args" value="-1.6 -warn:none" />
But only the first argument is given to javac, the second is ignored.
As per : http://ant.apache.org/manual/using.html#arg , one can have space for line attribute of compilerarg.
line a space-delimited list of command-line arguments.
Just using is below is resulting in any specific error, after property is set?
<compilerarg compiler="org.eclipse.jdt.core.JDTCompilerAdapter" line="${compiler.args}"/>

Condition Property Override

I want to use an condition property to set the property value to X if another property is defined and Y otherwise. However, I don't want the user to be able to override the condition property from the command line.
How can this be achieved?
Starting from ant 1.8 for some use cases local task may be applicable. Since a property is made local it starts with an empty value. It's scope is limited to current target, but you may pass it to subsequent targets using param argument in antcall.
Nope, you can't override a property set on the command line. At least, it's not easy to do. The whole purpose of overriding properties on the command line is to allow users to override defaults in order to make modification in the way your project builds. For example:
<property file="${basedir}/build.properties"/>
<property name="javac.debug" value="no"/>
<target name="compile">
<javac destdir="${main.destdir}"
debug="${javac.debug}">
By default, the Java code is compiled without debugging information. Maybe this is done to make jar files smaller, or faster interpretation, or maybe to make the code harder to decompile and read. Whatever reason, this build won't put debug information into the classfiles.
However, developers do want this debugging information, so they want to be able to override this setting:
$ ant -Djavac.debug=true compile
Or, they can create a build.properties file and put the value in there.
This type of issue comes up when you're not using Ant for builds. I know several sites that use Ant scripts to do deployments. I usually discourage this because Ant isn't really made for this type of thing. For example, Ant doesn't have any built in logic or loops. Once a property is set, it can't be changed. These are good ideas for a build language, but a terrible idea for a general purpose programming language.
Also, developers shouldn't be doing builds for QA or production. Those should be done by a build server that won't override defaults.
Now how to destroy this whole well thought out system and cause absolute havoc:
You can use the ant-contrib tasks in your project. Doing this will allow you to access the Ant Contrib var task to unset properties.
Download the ant-contrib.jar file (whatever the latest version is), and put it in a lib directory under your project. Then you can do this:
<project name="danger-will-robinson" default="package" basedir="."
xmlns:ac="http://ant-contrib.sourceforge.net">
<!-- Define the Ant-Contrib tasks -->
<taskdef=resource="net/sf/antcontrib/antlib.xml"
uri="http://ant-contrib.sourceforge.net">
<classpath>
<fileset dir="${basedir}/lib">
<include name="ant-contrib*.jar"/>
</fileset>
</classpath>
</taskdef>
<!-- Unset Property "foo", so you can use it -->
<ac:var name="foo" unset="true"/>
Note that the <classpath> points to the ant-contrib jar in the ${basedir}/lib directory. If you check that into your source repository, it will allow everyone who checks out your project to be able to do the build without installing the ant-contrib jar on their system.
Note that I've defined a "ac" XML namespace, so Ant-Contrib tasks won't overlap other possible third party tasks.
Properties in ant once set are immutable by design. You may overwrite an existing property with any scripting language that provides access to ant api, i.e. javascript.
JDK >= 1.6 already ships with a javascript engine, so you may use something like :
<project>
<property name="x" value="whatever"/>
<script language="javascript">
project.getProperty('x') ?
project.setProperty('foo', 'true') :
project.setProperty('foo', 'false');
</script>
<echo>$$[foo} => ${foo}</echo>
</project>
out of the box.But that won't help if someone uses ant -f yourbuild.xml -Dfoo=bla !! as userproperties (those properties defined via -Dkey=value) have a special protection.
So your requirement "..However, I don't want the user to be able to override the condition property from the command line". is not fullfilled.
But the let task from Ant addon Flaka provides the possibillity to overwrite even userproperties :
<project xmlns:fl="antlib:it.haefelinger.flaka">
<property name="x" value="whatever"/>
<!--
:= defines a new property whereas
::= overwrites any existing property
even userproperties
-->
<fl:let> foo ::= has.property['x'] ? 'true' : 'false'</fl:let>
<echo>$$[foo} => ${foo}</echo>
</project>
Run both scripts with ant -f yourbuild.xml -Dfoo=bla to see the difference.
Ant api has also method project.setUserProperty(String,String) so you may use also:
...
<script language="javascript">
project.getProperty('x') ?
project.setProperty('foo', 'true') :
project.setProperty('foo', 'false');
project.getUserProperty('x') ?
project.setUserProperty('foo', 'true') :
project.setUserProperty('foo', 'false');
</script>
...
to prevent the foo property to be set via .. -D .. and it will work even if property x is defined on commandline -Dx=whatever You have to make your choice, script task with javascript out of the box or Flaka let task
oneline solution but Flaka jar needed.

Raise an error if a variable is not defined in Ant

I need the user to define a variable at compile time, I still have to decide if it will be an environment variable or a property (ant -Dname=value).
How can I raise an error at compile-time if the variable has not been defined by the user?
Just add:
<fail unless="var1" message="var1 is not set"/>
If var1 is not set the build will fail.
For the first question:
If the variable is meant for environment-specific conditions outside of the application, then make it an environment (OS) variable. Otherwise, make it a property. Obviously, this doesn't fully answer your question since you still have to make a determination of what it means (for your app or system) to say that a variable is environment-specific.
Another guide would be to ask yourself whether you can (or will have) more than one application that depend on different values of the same variable, all possibly being deployed on the same system. In such a case, we cannot use an environment variable, with property-based variables the only way to go.
For the second question:
Use Ant's built-in Fail task to abort the build if a condition or property is not set. From Ant's documentation on the Fail task, you can get an idea on how to go about it if you use a property-based variable:
<fail>
<condition>
<not>
<isset property="thisdoesnotexist"/>
</not>
</condition>
</fail>
If you decide to use an environment variable, you simply use the environment attribute of the Property task to tap into environment (OS) variables just as if they were build properties (pls refer to the applicable documentation for examples.)
Hope it helps.

How do I get the target as a property in Ant?

Possibly one of those really terrible beginner questions where the manual will tell you everything, but, anyway, take this line below:
ant -Dfoo=bar buildme
in my build script, what is the property that holds "buildme"?
The list of targets invoked is available in the property
ant.project.invoked-targets
If there is a default target specified, then that will be the invoked target. If one or more targets are specified on the command line, these appear comma-separated in the property. Note that the property only becomes set once execution passes to a target - if you try to read the property outside of any target, it will be unset.
So, if the project has a default target 'zero':
$ ant one two
# ant.project.invoked-targets is set to:
one,two
and
$ ant
# ant.project.invoked-targets is set to (default):
zero
Seems like ant.project.invoked-targets is not available in ant 1.7.1
Not sure I understand your question, but "buildme" is the target to execute, not a property.
ant [options] [target [target2 [target3] ...]]
You "pick it" by creating the corresponding target:
<target name="buildme">
<!-- tasks that will execute here -->
</target>
As for the foo property, you "pick it" by using ${foo}.

Reasons for using Ant Properties files over "Properties Tasks"

I'm currently working with some developers who like to set up Ant tasks that define environment specific variables rather than using properties files. It seems they prefer to do this because it's easier to type:
ant <environment task> dist
Than it is to type:
ant -propertyfile <environment property file> dist
So for example:
<project name="whatever" default="dist">
<target name="local">
<property name="webXml" value="WebContent/WEB-INF/web-local.xml"/>
</target>
<target name="remote">
<property name="webXml" value="WebContent/WEB-INF/web-remote.xml"/>
</target>
<target name="build">
<!-- build tasks here --->
</target>
<target name="dist" depends="build">
<war destfile="/dist/foo.war" webxml="${webXml}">
<!-- rest of war tasks here -->
</war>
</target>
I am finding it hard to convince them that properties files are they right way to go. I believe properties files are better because:
They provides more flexibility - if you need a new environment just add a new properties file
It's clearer what's going on - You have to know about this little "trick" to realize what they're accomplishing
Doesn't provide default values and the ability to use overrides - if they used property files they could provide defaults at the top of the project but have the ability to override them with a file
Script won't break if an environment task isn't supplied on command line
Of course all they hear is that they need to change their Ant script and have to type more on the command line.
Can you provide any additional arguments in favor of properties files over "property tasks"?
Properties tasks tightly couple the build file to environments. If your fellow developers are arguing that they "have to change their ant script" with your suggestions, why aren't they arguing about changing it every time they have to deploy to a new environment? :)
Perhaps you can convince them to allow both properties file and command-line configuration. I set up my Ant builds so that if a build.properties exists in the same directory as the build.xml, it reads it in. Otherwise it uses a set of default properties hard-coded into the build. This is very flexible.
<project name="example">
<property file="build.properties"/>
<property name="foo.property" value="foo"/>
<property name="bar.property" value="bar"/>
...
</project>
I don't provide a build.properties with the project (i.e. build.properties is not versioned in SCM). This way developers aren't forced to use the property file. I do provide a build.properties.example file that developers can reference.
Since Ant properties, once set, are immutable, the build file will use properties defined in this order:
Properties provided with -D or -propertyfile via the command line
Properties loaded from build.properties
Default properties within build.xml
Advantages of this approach:
The build file is smaller and therefore more maintainable, less bug-prone
Developers that just can't get away from setting properties at the command line can still use them.
Properties files can be used, but aren't required
The arguments you have are already pretty compelling. If those arguments haven't worked, then arguing isn't going to solve the problem. In fact, nothing is going to solve the problem. Don't assume that people are rational and will do the most practical thing. Their egos are involved.
Stop arguing. Even if you win, the resentment and irritation you create will not be worth it. Winning an argument can be worse than losing.
Make your case, then let it go. It's possible that after a while they will decide to switch to your way (because it actually is better). If that happens, they will act like it was their own idea. There will be no mention of your having proposed it.
On the other hand, they may never switch.
The only solution is to work towards a position of authority, where you can say how things are to be done.
The problem with the first solution (using ant property) is basically hardcoding.
It can be convenient when you start a project for yourself but quickly you have to remove that bad habit.
I'm using a property file close to what said robhruska except that I have committed the build.properties file directly. This way you have a default one.
In other hand, I understand I could add those default values in the build.xml. (I will probably try that in the next hours/days ;-) ).
Anyway, I really don't like the first approach and I would force those guys to follow the second one ...

Resources