Generate UUID in Groovy

Since Groovy is a JVM-based language, we can use the same libraries and methods we would have used in Java to generate UUIDs, as long as we do so in Groovy syntax. The following code snipped produces a valid version 4 UUID:


import java.util.UUID
println UUID/randomUUID()

If you need to generate a specific UUID version, you need to use a third-party library such as uuid-creator which offers more flexible options:



import com.github.f4b6a3.uuid.UuidCreator

//generate UUID version 1
var uuid = UuidCreator.getTimeBased()

//generate UUID version 2
var uuid = UuidCreator.getDceSecurity(UuidLocalDomain.LOCAL_DOMAIN_PERSON, 1234)

//generate UUID version 3
var uuid = UuidCreator.getNameBasedMd5(UuidNamespace.NAMESPACE_URL, "https://github.com/")

//generate UUID version 4
var uuid = UuidCreator.getRandomBased()

//generate UUID version 5
var uuid = UuidCreator.getNameBasedSha1(UuidNamespace.NAMESPACE_URL, "https://github.com/")

//generate UUID version 6
var uuid = UuidCreator.getTimeOrdered()

//generate UUID version 7
var uuid = UuidCreator.getTimeOrderedEpoch()

How do I generate UUID in...

Find out how to generate UUIDs in your favorite language here: Bash, Kotlin, Python, Scala, Clojure, ClojureScript, Java, JavaScript, or return to Online UUID Generator