Using Ant to ensure a properties file only contains unique entries - ant

Is there a way in Ant which can check that a specified properties file only contains unique keys? A build failure would be the required behaviour if any duplicates are found.

rscbundlecheck does the duplicate key check and more validations suitable for internationalization. Blurb from their website:
Ant Task for checking Java
Resourcebundles. It checks for
existence of all keys in each bundle,
duplicate keys, existence of forbidden
chars (e.g. special characters like
umlauts) and continuous usage of
placeholders.
Example ant script:
<taskdef name="resourceCheck" classpathref="build.classpath" classname="org.dyndns.fichtner.rsccheck.ant.RscBundleCheckTask"/>
<resourceCheck>
<fileset file="my-properties-file.properties"/>
</resourceCheck>
Where build.classpath contains the resourceCheck binary jar available here

Related

How can an ANT build file insert the version number in the generated Javadocs?

I use an ANT build file to build my project and also generate the JavaDocs with it. Therefore I use the "javadoc"-command from ANT. Until now I have the version number inserted directly in the .java-file with the "#version"-literal. So if I want to increase the version number I have to open every .java-file and change the number.
Is it possible to define the version number once in the ANT file and let ANT insert this version number into the JavaDocs itself? How can you do this?
Looking at the available options of the JavaDoc task I would use the Header atribute/element which allows to include HTML text defined in every generated file. The HTML text is defined in the the ANT task similar to the doctitle attribute:
<javadoc ...>
<header><![CDATA[<p>Version: ${myversion}</p>]]></header>
</javadoc>
http://ant.apache.org/manual/Tasks/javadoc.html

Findbugs ant task submitting several dynamically detected JAR files for analysis

I'm currently looking to run static analysis over a pre-existing project. As the project is created and supplied by an off-site company, I cannot change the build process radically.
The project is split into a lot of sub-modules, located in various places. For other analyisi tools (JDepend, Google Testability Explorer, etc.), I have dynamically detected all build JAR files into a path element as follows:
<path id="built-libs">
<fileset dir="${overall-base}">
<include name="${some-common-base}/**/lib/*.jar" />
</fileset>
</path>
<property name="built-libs-string" refid="built-libs" />
For some tools, I use the build-libs, for others I use the string (in classpath form; x.jar;y.jar).
The trouble is, FindBugs uses a completely different format to any other;
<class location="x.jar"/>
<class location="y.jar"/>
...
Now, I could list all the JAR files manually, but then run the risk of this list going out of synch with the other tool's lists, or of introducing typos.
Another complication is that I also want to run the reports in Jenkins, in this case the extract directory for individual modules will depend on the job that has previously built the module (pipeline builds, modules extracted from SCM and built in parallel, the reporting occurring at the end of the pipline).
I could make a call out to the OS to run FindBugs, passing in the JARs in a space separated list (as in Invoking FindBugs from Ant: passing a space-separated list of files to java). However, I prefer a, Ant solution to an OS <exec... hack.
Note I know I have a similar problem for the sourcepath element, however, I'm assuming that solving the class element problem also solves the sourcepath one.
Ideally, FindBugs should be taking a resource collection rather than separate class elements. I'm not familiar with FindBugs, so I can't comment on why they have chose to go the class element route instead of a resource collection, however your comment about using exec implies that using a resource collection is a valid design alternative.
I would try rolling your own Ant macro, which invokes FindBugs directly using the java task. This should give you the control you need and avoiding the redundancy that the FindBugs Ant task would introduce.
Another option (which is an ugly hack) is to use the fileset to write a mini ant file with a FindBugs target, which you then invoke using the ant task. shudders
The Findbugs Ant task allows you to specify a filelist which can be used to specify multiple files. Quoting from the Findbugs documentation
"In addition to or instead of specifying a class element, the FindBugs
task can contain one or more fileset element(s) that specify files to
be analyzed. For example, you might use a fileset to specify that all
of the jar files in a directory should be analyzed."
Example that includes all jars at ${lib.dir}:
<findbugs home="${findbugs.home}" output="xml" outputFile="findbugs.xml" >
<auxClasspath path="${basedir}/lib/Regex.jar" />
<sourcePath path="${basedir}/src/java" />
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</findbugs>

How to determine if a property value is directory path using ANT?

We have a property file containing list of key and their values. The value of a property can be directory paths or numeric values or hyperlink URLs or database URLs or database server I.P address or port numbers etc.
I would like to know if there is a way in ANT build file to detrmine if the value of a property is directory path or not. Based on the answer, for the values with directory path, we would like to execute a special logic than others.
Please advise.
You want to use the Available Ant task. This is untested but it should get you started.
<target name="check.directory">
<available file="${my.property}" type="dir" property="my.property.present"/>
</target>
<target name="do-if-directory" depends="check.directory" if="my.property.present">
<!-- custom task here -->
</target>
Alternatively you can use the Condition Ant Task.

ant script to use use two .properties files?

I want to know if it is possible to get an ant script to reference 2 different .properties files at once, and if so, how to accomplish this.
Assume that the properties contained within the two .properties files are mutually exclusive, i.e. the same property will not appear twice.
Thanks!
In addition to Ash answer.
You can use a different prefix attribute of property task, e.g
<property file="file1.properties" prefix="file1"/>
<property file="file2.properties" prefix="file2"/>
This way you can find out if both files have same properties and differentiate between them in your build script. For example if both files have property test, then after they are loaded with the above commands you will end up with properties named file1.test and file2.test.
You should be able to import any number of properties files with multiple <property file="..."> entries in your ant script (unless there's some subtlety to your question that I've missed?). Duplicate properties are OK, since in ant properties are immutable and whoever sets the property first "wins".
See http://ant.apache.org/manual/Tasks/property.html for more details.

How can I exclude a list of files (held in a file) from an Ant fileset?

Is there a way to have an Ant Fileset->IncludesFile attribute take a property file OR any other file that contains a list of Java class files to exclude?
Eg:
File A.properties OR A.java contains listing
abc.class
mno.class
xyz.class
Is there a way to say to point excludesFile to file A.properties.
<fileset dir="...">
<excludesFile file="A.properties" />
</fileset>
The behavior that I want is when Java runs, it excludes the Java files listed in this file (A.properties)
Hmm...not sure if it's case sensitive. The documentation at http://ant.apache.org/manual/Types/fileset.html shows "excludesfile", all lowercase, as with other ant directives.
I'm not sure exactly what you are asking. If you have a fileset which is including files you would like to exclude, and you want to specify that list of files to exclude in an external file, "excludesfile" is the correct way to do it.

Resources