I'm trying to learn some ant for a Struts 1.x project that I was thrown onto. Mainly I'm trying to find a good referent for the inherent variables/properties of ant...beginners tutorial. Any GOOD reference really.
A couple lines of the ant file that I've been trying to figure out just for example...
<available file=${deploy.ant.docbase.dir}/WEB-INF/sun-web.xml" property="sun.web.present"/>
and
<replace file="${temp.sun.web}">
<replacetoken><![CDATA[<!DOCTYPE]]></replacetoken>
<replacevalue<![CDATA[<!-- <!DOCTYPE]]></replacevalue> //in ant is <!-- the comment out flag?
</replace>
I did do some searching and only could find ant build examples without explanation, but if it is covered and I just didn't find it a link will suffice. No reason to make someone reexplain it....I just couldn't find it.
Your first code block refers to the "available" ant task. It sets the property sun.web.present if the given file exists.
In your second code block, "<!--" starts an XML comment ("-->" closes one). This is true for all XML, not just ant build.xml files. In this case it is using the "replace" ant task to replace "<!DOCTYPE" with "<!-- <!DOCTYPE" within the file named by temp.sun.web.
In general an ant build file has targets like "build" or "clean". These depend on each other so that "test" runs "build" first. The targets are implemented by "tasks", where each XML tag refers to a task. You can read their manual and refer to the per-task docs for how each task works.
The Ant Manual is your friend. There's a link Ant tasks on the left side of the page. Click on that link, and then the List of Tasks link. That will list all of the Ant tasks on the left and their explanation on the right. There, you'll see the available task and the replace task.
Unfortunately, the Ant manual uses Frames (bad Ant Manual! Bad Ant Manual!), so I can't supply a link that will list both the
(Shameless bid for reputation)
The example doesn't use a built-in property. Most Ant targets won't, because after properties are first set they are immutable. Instead, Ant scripts usually define their own properties. The Ant manual lists the properties that Ant predefines.
If you want to get into the guts of Ant, I recommend the Manning "Ant in Action" book.
Related
I want to insert the content of a file into an xml-file (using XPath or a replacer token). This should happen during the build process using maven.
My first try was to use the maven ant task plugin and the xmltask task of ant.
<xmltask source="sourceFile.xml" dest="destinationFile.xml">
<replace path="//L7p:MapValue[2]/L7p:Mappings" withfile="xmlFileToInsert.xml" />
</xmltask>
That worked fine for a while, but now i want to insert not valid xml. This xml will be made valid in future steps - but its really required to insert invalid xml here. AFAIK, this does not work with the xmltask of ant. If you know a way to disable the validation, it would also help.
Now, I'm searching for nearly the same xmltask can do in combination with maven and ant but without the validation of xml.
What do you guys think is the best way to do this with maven?
List item
Include shell script?
Write an maven plugin?
???
Thanks for your opinion and help.
If you're already using Ant, just use the Ant replace task (https://ant.apache.org/manual/Tasks/replace.html). It replaces text by another in any file, so it doesn't care if it's not valid XML. Read the value to use as a replacement from your file, and have a marker token in your original file. You could still first use xmltask to insert the marker token via XPath to give valid XML, then do the textual replacement via replace.
I want to start findBugs on existing project (on command line there are currently 522 bugs).
How can I set in the ant file (under findbugs target) the option to fail the build if there are more than 522 bugs found?
I want to actually fail the ant build and not to understanf it manually from the output report.
Thanks.
That may not be possible. You can use data mining task and do some special handling. http://findbugs.sourceforge.net/manual/datamining.html
This may go a bit too far, but I'm adding it as an answer none the less.
You can use sonar for that. Sonar is a free platform to measure and track code quality. It has findbugs metrics included.
It has the Build Breaker Plugin, which will break your build on certain conditions.
You need to write a custom ant task, deriving from Ant's Task base class, which adds a log listener that sets a property and removes itself afterwards. The custom task will fetch the current Project and add the log listener via addLogListener(...).
The log listener needs to identify when findBugs is logging, discern which log message is the one that lists the number of bugs, parse that line, set the property and remove itself from the set of log listeners.
Once you have that task, you need to also write a new condition, as Ant doesn't ship with a numeric comparison condition. Then you can use your custom condition in the fail task, like any other condition.
<findBugCountListener prefix="findbugs.count"/>
<findBugs ...>
</findBugs>
<fail>
<condition property="allupper">
<and>
<isset property="findbugs.count"/>
<greaterThan value="${findbugs.count}" limit="522"/>
</and>
</condition>
</fail>
Ideally your listener will then set an Ant property, with the number of bugs. You can then use AntContrib's Assert task to throw a build exception if the number of bugs is too high.
Yes, it is more manual work than simply configuring a few tags, but at least it is possible with Ant due to it's ability to load custom extensions. It isn't like Ant was written with explicit knowledge of the findBugs plugin (which naturally had to be written afterwards).
Another alternative is to find the source code of the findBugs ANT plugin, and assuming it has a license that allows legal modification, modify the ANT plugin to have a new "additional" property, setCount="propName" and then capture and set the property within the existing findBugs ANT plugin.
Quite an old question, but maybe this is useful for someone else: There is a rather simple way to achieve the desired behavior by setting the warningsProperty attribute of the findbugs task and afterwards letting fail check for the property's value:
<findbugs ... warningsProperty="findbugs.warnings">
...
</findbugs>
<fail>
<condition><istrue value="${findbugs.warnings}"/></condition>
</fail>
(Needless to say that findbugs is abandoned now, but there is spotbugs as a successor.)
I'm trying to use ANTLR3 task for Ant, but I get an "Unable to determine generated class" build failure message.
A quick research shows that many people have had the same problem, with no solution provided (see links below).
Can someone suggest a solution that doesn't resort to using a regular Java Ant task?
External links:
http://www.antlr.org/pipermail/antlr-interest/2009-November/036795.html
http://www.antlr.org/pipermail/antlr-interest/2006-July/016870.html
http://palove.kadeco.sk/itblog/posts/40
The antlr task included with Ant 1.8.2 (the latest version) seems to be dependent on ANTLR 2.7.2 (defined in $ANT_HOME/lib/ant-antlr.pom and using $ANT_HOME/lib/ant-antlr.jar.
What the task is doing is scanning the target file for a line matching ^class (.*) extends .*, where the first match group will be used as the name of the generated file. This whole bit of syntax seems to have been dropped in ANTLR 3.x, or at least made optional, because I'm able to generate parsers without it using the regular java task work-around you mentioned.
On the front page of http://antlr.org/ under the "File Sharing" heading is a link to ANTLR v3 task for Ant, but unfortunately it doesn't appear to be the sort of drop-in replacement I was hoping for. Actually, it seems to be rather convoluted so I've stuck with using the plain java task.
I have the following problem. I have something like 300 Eclipse Plugins. Now, as part of an ant script I want to read all MANIFEST.MF files and then look for the execution environment string.
Bundle-RequiredExecutionEnvironment: J2SE-1.4
Now, this string has several possible values. I want to create a report that lists the execution environment for each plug-in. That part is not really a problem as I can use some kind of regexp to obtain it.
My problem is that I want also to create some kind of summary for tracking changes at a glance, something like:
JS2E-1.4: 50 Plugins
JS2E-1.5: 150 Plugins
JS2E-1.6: 74 Plugins
Anyone has some suggestions on how could I go around this?
EDIT: Reason for using ANT is that I want to integrate it with a nightly build script
I would definitively go for hard-coded Ant task and decompose the problem in two tasks:
the first task takes a jar file and outputs a plugin-info.xml file that contains various infos, like the environment
the second task parses all these xml files and creates an XML summary report
This will of course generate (n+1) XML files for n plugins and some will find this way too much.
The nice end effect with that approach is that you can generate either detail or aggregated reports very easily (with some XSLT magic.) or even graphs.
If i were to do it myself, i probably would just write a perl script.
If it has to be done from Ant, i would write an Ant Task to do it.
I would suggest just printing each executable environment on System.out and then post process with "|sort| uniq -c".
You can use the math task from the ant-contrib project
I had to do it, I'd probably go for some shell script or custom code
I would like to append the output JAR filename of a Netbeans project with some version number: something like 1.0, 2.0b or even a Subversion revision number. I can't seem to find anything on this, though. I am also not sure if this would the responsibility of the build system (Ant) or if the IDE (Netbeans) can delegate the process. Is there a centralised, clean way of doing this?
IMO, this is the responsibility of the build system, not of the IDE. Let me say it in other way: don't rely on your IDE to build your project, use a build tool. Using an IDE is fine during development but being IDE dependent to build a project is not a good thing (what if you change your IDE tomorrow, what if you want to build your project on another machine/OS without that IDE, what if you want to build your project on a headless machine, what if you want to automate your build, what if someone wants to build that project and doesn't have that IDE, etc, etc). Really, this is what build systems are for.
Now, regarding your initial request, there are plenty ways to add a version number. One of them is to use the Ant's BuildNumber task:
This is a basic task that can be used to track build numbers.
It will first attempt to read a build number from a file (by default, build.number in the current directory), then set the property build.number to the value that was read in (or to 0, if no such value). It will then increment the number by one and write it back out to the file. (See the PropertyFile task if you need finer control over things such as the property name or the number format.)
Use it for example like this:
<target name="jar" depends="compile">
<property name="version.num" value="1.00"/>
<buildnumber file="build.num"/>
<jar destfile="foo-${version.num}-b${build.number}.jar"
basedir="."
includes="**/*.class"
/>
</target>
Or you could indeed add subversion revision number. An easy way to do this seems to install the SVNAnt task and use the status task:
<target name="revisionnumber">
<!-- get the svn revision number -->
<svn>
<status path="application.cfm" revisionProperty="svn.revision" />
</svn>
<echo>Sandbox Revision: ${svn.revision}</echo>
</target>
Finally, another option would be to use Maven instead of Ant which has a built-in version management feature as pointed out by cetnar.
I'm not sure if it's the best way, but we put it in MANIFEST.MF file like this:
Implementation-Version: 2.0b
We can get this value programmatically like this:
String version_num = this.getClass().getPackage().getImplementationVersion();
If you feel like using a tool to handle your builds then there are lots about, such as CruiseControl, which is ANT based and has pretty deep integration with your source code control.
I use it to automatically increment a build number and use that as the last digit in my version number for the jar, e.g. 1.4.168, where 168 is the build number. I am just about to get it to put a label into CVS just before the fetch with the build number so I know exactly what code is in the jar.
Well is done default by Maven. Even if you want name your jar file with more detailed information you can use build number plugin.
EDIT
At begining I misunderstood your question so following part relates to adding this information inside jar files.
You can do it yourself by creating manifest file. In Maven it you can tune proces of creating manifest file by additional configuration. I suppouse (I'm sure) that in Ant should be similar functionality.
you can use maven for vesion and read it from pom.
read this article:
Embedding the maven version number
at
http://happygiraffe.net/blog/2008/10/01/embedding-the-maven-version-number/