Apparently, log4j2 is a multi-release jar and older versions of Tomcat can’t handle that. So I needed to upgrade my Tomcat maven plugin.

Solution: Update your Tomcat

But how do you update your Tomcat? Its information page shows that it hasn’t been updated for a while, the latest version being 2.2 which runs 7.0.47 by default. Maven Central, on the other hand, shows that the latest version at the moment of this writing is 7.0.86. That’s the version we want.

Suppose we need to zip a bunch of Python files (without their compiled counterparts *.pyc). The next (admittedly contrived) example shows how we could go about doing something like this in Groovy:

FilesrcDir=newFile('/home/neftas/Downloads/scripts/')FiledestDir=newFile(srcDir,'antexample')FilezippedFile=newFile(srcDir,'antzipped.zip')defallPythonFilenames=srcDir.list().sort().findAll{it.endsWith('.py')}assert['a.py','b.py','c.py']==allPythonFilenamesdefant=newAntBuilder()ant.with{echo'begin zipping and unzipping'zip(destfile:zippedFile,basedir:srcDir,includes:'*.py')mkdir(dir:destDir)unzip(src:zippedFile,dest:destDir)echo'done zipping and unzipping'}// zip file is createdassertzippedFile.exists()// contents of zip file should match content of source directorydefcommands=['bash','-c',"zipinfo -1 ${zippedFile.name}"]defprocess=commands.execute(null,srcDir)defcontentsOfZip=process.text.split(System.lineSeparator)assertcontentsOfZip.sort()==allPythonFilenames// all files should be unpacked from the zip into the right directoryassertdestDir.list().sort()==allPythonFilenamesant.with{echo'deleting created files'// notice nested Ant taskdelete(includeEmptyDirs:true){fileset(dir:destDir)fileset(file:zippedFile)}echo'deleted created files'}

We can use all the power of Ant in our Groovy scripts. Ant task names map to Groovy methods (see the zip method above) and the attributes are passed as maps to these methods. What’s even better, there is no need to stringify the arguments of these attributes (as is required in Ant’s XML), but we can directly use the correct datatypes (e.g. in the key-value pair destfile: zippedFile, zippedFile is of type java.util.File). All attributes of Ant tasks are available to use with AntBuilder (see Ant manual for zip task).

Invoking from Gradle

AntBuilder is also available in Gradle. This will come in handy when you don’t know the "Gradle way" of doing something, but do know how to tackle it in Ant.

Dependencies

Groovy (and Gradle) come with a bundled version of AntBuilder, so you should be good shape when creating a Groovy script, but if you want to use AntBuilder in a project, you should add ant and ant-launcher to your build.gradle:

A while ago, I needed to write a program that retrieved some information from the database by using SQL queries located in files. I wanted to add a file that would contain the settings of the app, like the port, SID, login and password for the database, but also the location of those queries. The problem was I couldn’t add those settings to a file in the standard src/groovy/resources folder, because when creating a distribution with Gradle ./gradlew distZip the file would be part of the jar, and thus it would be difficult to modify the contents. I was basically after a run-control file, a settings file in the root of my distribution, that I could easily change on each run (if necessary).

Gradle provides several ways to accomplish this:

Add the file to src/dist and the file will be put in the root of the distribution after invoking ./gradlew installDist. Any folders you create will also be put in the distribution, so you can create src/dist/config, for example, to put your configuration in, and a folder config will be created in the distribution.