Is there any way to generate a guid in ANT?

前端 未结 2 584
醉酒成梦
醉酒成梦 2021-02-07 11:09

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

相关标签:
2条回答
  • 2021-02-07 11:26

    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

    0 讨论(0)
  • 2021-02-07 11:35

    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.

    0 讨论(0)
提交回复
热议问题