15

I have an ant script to manage out build process. For WiX I need to produce a new guid when we produce a new version of the installer. Anyone have any idea how to do this in ANT? Any answer that uses built-in tasks would be preferable. But if I have to add another file, that's fine.

Eugene M
  • 47,557
  • 14
  • 38
  • 44
  • 1
    The answer below will work, but you mention you are using Wix. According to the book I am reading "WIX: A Developer's Guide to Windows Installer XML" you can define id as * and Wix will generate a new Guid for you. So you can just do – Sam Plus Plus Mar 19 '12 at 21:48

2 Answers2

26

I'd use a scriptdef task to define simple javascript task that wraps the Java UUID class, something like this:

<scriptdef name="generateguid" language="javascript">
    <attribute name="property" />
    <![CDATA[
    importClass( java.util.UUID );

    project.setProperty( attributes.get( "property" ), UUID.randomUUID() );
    ]]>
</scriptdef>

<generateguid property="guid1" />
<echo message="${guid1}" />

Result:

[echo] 42dada5a-3c5d-4ace-9315-3df416b31084

If you have a reasonably up-to-date Ant install, this should work out of the box.

martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • Had no idea you could do this; this is great. Thanks. – duma Jul 09 '14 at 15:57
  • 4
    Note that if you are using ant with a Java 8 JDK, you might run into the following issue with the importClass statement. A simple work around is to omit it and use the fully qualified name of UUID on the next line. http://stackoverflow.com/questions/22503100/java-8-javascript-engine-backwards-compatibility – Kyle Nov 23 '15 at 17:10
3

If you are using (or would like to use) groovy this will work nicely.

<project default="main" basedir=".">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" 
         classpath="lib/groovy-all-2.1.5.jar" />
    <target name="main">
        <groovy>
            //generate uuid and place it in ants properties map
            def myguid1 = UUID.randomUUID()
            properties['guid1'] =  myguid1
            println "uuid " + properties['guid1']
        </groovy>
        <!--use the uuid from ant -->
        <echo message="uuid ${guid1}" />
    </target>
</project>

Output

Buildfile: C:\dev\anttest\build.xml
main:
      [groovy] uuid d9b4a35e-4a75-454c-9f15-16b4b83bc6d0
      [echo] uuid d9b4a35e-4a75-454c-9f15-16b4b83bc6d0
BUILD SUCCESSFUL

Using groovy 2.1.5 and ant 1.8

Haim Raman
  • 11,508
  • 6
  • 44
  • 70
  • If you copy groovy-all-x.x.x.jar to the ant lib directory you can remove the classpath on the groovy taskdef. Tested with groovy-all-2.4.6.jar and ant 1.9.4 – carl verbiest Apr 08 '16 at 12:33