Groovy task in ant file without explicit taskdef? - groovy

Disclaimer: Ant Noob!
I'm trying to get the groovy task running in ant. Works so far:
<project>
<taskdef name="groovy"
classname="org.codehaus.groovy.ant.Groovy"
classpath="C:/Local/groovy-2.4.5/lib/groovy-ant-2.4.5.jar;C:/Local/groovy-2.4.5/lib/groovy-2.4.5.jar"/>
<echo message="Hello!"/>
<groovy>
println "Hello from Groovy!"
</groovy>
</project>
What I can't get behind (even reading this and related entries) is what I need to do to my Ant installation (Windows) to make the script run like so, without the taskdef or any other reference in my project specific build file:
<project>
<echo message="Hello!"/>
<groovy> <!-- Would be nice if I could treat this like a built-in -->
println "Hello from Groovy!"
</groovy>
</project>

You'll need to put all Groovy dependencies (groovy-ant.jar, groovy.jar, etc) on the classpath that is read by Ant. The easiest way is to store them under ANT_HOME/lib. Or you can pass them using the -lib option on the command line.
Then you would still have to tell Ant about the path of the antlib. You can use the antlib namespace mechanism described in the documentation page. You specify the package that contains the antlib.xml in the namespace URI, in this case org.codehaus.groovy:
<project xmlns:groovy="antlib:org.codehaus.groovy">
<echo message="Hello!"/>
<groovy:groovy>
println "Hello from Groovy!"
</groovy:groovy>
</project>
Note that you can still use taskdef without referencing the Jar file if it is placed (with its dependencies) in ANT_HOME/lib. You just reference the path of the antlib resource in the Jar. In this case you can do without the namespace URI:
<project>
<taskdef resource="org/codehaus/groovy/antlib.xml"/>
<echo message="Hello!"/>
<groovy>
println "Hello from Groovy!"
</groovy>
</project>

Related

How to hide Tomcat version from error messages when using embedded servers in Java

I have a java application where i'm using embedded Tomcat servers,
which looks like this
Tomcat tomcat = new Tomcat()
I'm creating an embedded tomcat server here.
Problem statement
whenever there's an error it displays information on which tomcat version i'm using,
how to hide this in java?
i have a little idea that i need to override ServerInfo.properties, but how do i do this?
I'm not sure how we can do this in java, but if you are using any build scripts like ant / gradle for distribution purpose, we can write a task to override / harden the jar file, and replace the ServerInfo.properties file with the customized value whatever we need.
the code for ant build scripts would look like
<target name="override.tomcat">
<jar destfile="path/to/tomcat-embed-core-9.0.62.jar" update="true">
<fileset dir="src/"> <!-- folder where you keep the directory/file to raplace-->
<include name="org/apache/catalina/util/ServerInfo.properties"/> <!-- file to replace within directory path in side the jar-->
</fileset>
</jar>
</target>
and in gradle
task overRideTomcat(type: Jar) {
from(zipTree(file("path/to/tomcat-embed-core-9.0.62.jar"))) {
exclude '**/org/apache/catalina/util/ServerInfo.properties'
}
from('src/') {
include('/org/apache/catalina/util/ServerInfo.properties')
}
archiveName "tomcat-embed-core-9.0.62.jar"
}
make sure you have the modified ServerInfo.properties file under src directory in the same path as you have mentioned in the include statement.

Overriding project properties with a custom task

Our C++ project uses MSBuild to build on Windows and GNU make on *nix. I'm trying to recreate the functionality of the following single line of GNU make in MSBuild:
GENN_PATH:=$(abspath $(dir $(shell which genn-buildmodel.sh))../userproject/include)
Essentially setting a variable to a path relative to an executable in the path. However, this is proving to be a battle to implement in MSBuild...
The following are the (hopefully) pertinent sections from my vcxproj. For testing purposes I am first setting the variable I want to override to something obvious:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
<PropertyGroup Label="Configuration">
...
<GeNNUserProject>UNDEFINED</GeNNUserProject>
</PropertyGroup>
Then, in my ClCompile item definitions, I am adding the value of this property to the additional include directories
<ItemDefinitionGroup>
<ClCompile>
...
<AdditionalIncludeDirectories>include;$(GeNNUserProject)</AdditionalIncludeDirectories>
</ClCompile>
...
</ItemDefinitionGroup>
In order to find this path, I'm using the where command and redirecting it's output to a property. Then, from this, I'm finding the include directory and printing it out - this works!
<Target Name="FindUserProjects">
<Exec Command="where genn-buildmodel.bat" ConsoleToMsBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="GeNNBuildModelPath" />
</Exec>
<PropertyGroup>
<GeNNUserProject>$([System.IO.Path]::GetFullPath($([System.IO.Path]::GetDirectoryName($(GeNNBuildModelPath)))\..\userproject\include))</GeNNUserProject>
</PropertyGroup>
<Message Text="MAGIC GENN-FINDING! $(GeNNBuildModelPath) -> $(GeNNUserProject)"/>
</Target>
I've tried a variety of ways of making this a dependency of ClCompile including setting the Target as BeforeTargets="PrepareForBuild" and the following:
<PropertyGroup>
<BeforeClCompileTargets>
FindUserProjects;
$(BeforeClCompileTargets);
</BeforeClCompileTargets>
</PropertyGroup>
</Project>
Whatever I do, my custom target runs but the property is not being overriden. Google suggests that if properties are overriden in depencies they should be visible from targets and from digging into Microsoft.CPP*.targets this is what setting BeforeClCompileTargets is doing.
The problem here was not that the target wasn't setting the property, it's that the AdditionalIncludeDirectories item metadata was being set from the original value. The solution is to set this directly from the target instead:
<ItemGroup>
<ClCompile>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$([System.IO.Path]::GetFullPath($([System.IO.Path]::GetDirectoryName($(GeNNBuildModelPath)))\..\userproject\include))</AdditionalIncludeDirectories>
</ClCompile>
</ItemGroup>

ant: string manipulation on variable

I have a question based from this question
Replacing characters in Ant property
I want to build a variable (i can't use a property because i'm in a loop) that is pretty much StringA - StringB.
(maybe this is a misunderstanding of properties on my part but they can only be assigned once correct?)
I guess I could build a script function to calculate that, but my guess is that it must be possible to do it in an already existing function, probably something i'm missing.
this would be an example of the code
<for param="file">
<path>
<fileset dir="${mydir}" >
<include name="*.war"/>
</fileset>
</path>
<sequential>
<var name="undeploy_name" value="#{file} function_here ${mydir}" />
<JBossCLI port="${jboss.port.management-native}">
<undeploy namePattern="${undeploy_name}" />
</JBossCLI>
<deployToLiferay file="#{file}" />
</sequential>
</for>
in general I want to deploy several wars. this works fine when I run it once but if I want to make it re-runnable I need to undeploy them first.
I'm just a consumer of this interfaces, Ideally deployToLiferay would auto undeploy but it does not.
thanks for an feedback
edit: if I use something similar to what is define on the linked page i get:
<loadresource property="file-to-deploy">
<propertyresource name="#{file}"/>
<filterchain>
<tokenfilter>
<filetokenizer/>
<replacestring from="${mydir}" to=""/>
</tokenfilter>
</filterchain>
</loadresource>
10:52:49.541: * /data/contribution.xml:171: The following error occurred while executing this line:
10:52:49.541: * /data/contribution.xml:178: null doesn't exist
line 178 is my loadresource part
ANT is not a programming language. Personally I'd recommend embedding a scripting language like Groovy to process a group of files:
<target name="process-files" depends="resolve">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<fileset id="wars" dir="src/wars" includes="*.war"/>
<groovy>
project.references.wars.each {
ant.echo(message: "I want to do something with this ${it} file")
}
</groovy>
</target>
Example
├── build.xml
└── src
└── wars
├── app1.war
├── app2.war
└── app3.war
Example
process-files:
[echo] I want to do something with this /../src/wars/app1.war file
[echo] I want to do something with this /../src/wars/app2.war file
[echo] I want to do something with this /../src/wars/app3.war file
Update
The following working example shows how Apache ivy can be used to manage build dependencies. This is a capability that exists in other Java build tools like Maven.
<project name="demo" default="process-files" xmlns:ivy="antlib:org.apache.ivy.ant">
<available classname="org.apache.ivy.Main" property="ivy.installed"/>
<!--
==================
Normal ANT targets
==================
-->
<target name="process-files" depends="resolve">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<fileset id="wars" dir="src/wars" includes="*.war"/>
<groovy>
project.references.wars.each {
ant.echo(message: "I want to do something with this ${it} file")
}
</groovy>
</target>
<!--
=============================
Dependency management targets
=============================
-->
<target name="resolve" depends="install-ivy">
<ivy:cachepath pathid="build.path">
<dependency org="org.codehaus.groovy" name="groovy-all" rev="2.4.7" conf="default"/>
</ivy:cachepath>
</target>
<target name="install-ivy" unless="ivy.installed">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/>
<fail message="Ivy has been installed. Run the build again"/>
</target>
</project>

Retrieve value from text file and replace a string constant in another file with that value using ant script

I have a file called versionInfo.txt. This file among other things has the following text: "Implementation-Version: 7.5.0.1".
I need to retrieve the version value and copy this version value to a Java file. The Java file will have the following variable:
version = "#version-info#";
I need to replace this #version-info# with the value I retrieved from the first file. I need to do plug in this code in an existing build.xml file written using ant script.
Create a properties file like this and name it build.properties
version.label=7.5.0.1
Then in your build.xml file
<project basedir=".">
<target name="replace-labels">
<property file="${basedir}/build.properties"/>
<replace
file="${basedir}/myClass.java"
token="#version-info#"
value="${version.label}" />
</target>
</project>
So your file structure should look like
myproject
build.properties
build.xml
myClass.java
Then you can execute your ANT build by changing to the "myproject" directory and executing
ant replace-labels
The replace tag will look for the string "#version-info#" in your myClass.java file and replace it with the value "7.5.0.1"
For the second part of your question, retrieve the version info.. :
If you need to read the Implementation-Version from the Manifest of a jar you may use a macrodef, f.e. :
<!-- Grep a keyvalue from Manifest -->
<macrodef name="mfgrep">
<attribute name="jar"/>
<attribute name="key"/>
<attribute name="catch"/>
<sequential>
<loadproperties>
<zipentry zipfile="#{jar}" name="META-INF/MANIFEST.MF"/>
</loadproperties>
<property name="#{catch}" value="${#{key}}"/>
</sequential>
</macrodef>
<mfgrep
jar="/home/rosebud/temp/ant.jar"
key="Implementation-Version"
catch="foobar"
/>
<echo>$${foobar} => ${foobar}</echo>

Cruise Control parsing "!" character in NAnt file

I have Cruise Control configured with a task to run a NAnt script, which runs an MSTest suite. MSTest allows me to specify test categories so I want to specify "!Integration" (which means "don't run Integration tests"). My Nant script successfully runs when I run it from the command line, but when Cruise runs it, the "!Integration" directive is being garbled -- the Cruise output suggests its inserting a line break after the '!' character. The result is that all my tests run, including integration tests.
Extract from ccnet.config:
<tasks>
<nant>
<executable>C:\nant\bin\nant.exe</executable>
<baseDirectory>C:\MyProject\BuildDirectory</baseDirectory>
<buildFile>MyProject.build</buildFile>
<targetList>
<target>CIServerBuild</target>
</targetList>
</nant>
</tasks>
Extract from MyProject.build:
<target name="CIServerBuild">
:
<call target="RunUnitTests" />
</target>
<target name="RunUnitTests">
<property name="TestCategories" value="!Integration" />
<call target="RunMSTest" failonerror="true"/>
</target>
<target name="RunMSTest">
<call target="BuildListOfTestContainers" failonerror="true"/>
<exec program="${MSTest.exe}"
commandline=" /category:"${TestCategories}" ${TestContainers} /resultsfile:${MSTest.ResultsFile} /nologo "
/>
</target>
Extract from Cruise output:
[exec] Starting 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe ( /category:"!
Integration" /testcontainer:C:\TaxWise\BuildDirectory\TaxWise\TaxWise.Data.Tests\bin\Debug\TaxWise.Data.Tests.dll /testcontainer:C:\TaxWise\BuildDirectory\TaxWise\TaxWise.Domain.Tests\bin\Debug\TaxWise.Domain.Tests.dll /testcontainer:C:\TaxWise\BuildDirectory\TaxWise\TaxWise.Infrastructure.Tests\bin\Debug\TaxWise.Infrastructure.Tests.dll /resultsfile:.\TestResults\UnitTests.trx /nologo )'
in 'C:\TaxWise\BuildDirectory'
I have tried replacing the '!' character with
'!'
but that made no difference.
Any ideas, anyone?
I suggest splitting the commandline attribute in the exec task into Nant arg elements.
http://nant.sourceforge.net/release/0.85/help/tasks/exec.html
You'll have more flexibility and the readability will increase.
Yes, perhaps it is not caused by CC. Try setting verbose="True" on the <exec> task and check the raw build protocol. Remember what you see on the report page is not the exact output (typically subject to line-wrap and coalescing whitespaces).
Maybe it depends on from where you run the script, a hidden dependency on a build property or different environment variables. You can check the latter using <exec program="cmd.exe" commandline="/c set" />. For the properties you can use the following script:
<script language="C#" prefix="util" verbose="true">
<code>
<![CDATA[
public static void ScriptMain(Project project)
{
foreach (DictionaryEntry entry in new System.Collections.SortedList(project.Properties) )
Console.WriteLine("{0}={1}", entry.Key, entry.Value);
}
]]>
</code>
</script>

Resources