Groovy script executable

I would like to do a portable groovy script wrapping and I inserted in the first line:

#!/usr/bin/env groovy 

The problem occurs when the script is run outside its directory, it cannot find the library. I came from the python world and imports everything into python, deciding on the path of the script. In groovy, it seems to me that I should specify -classpath, but I cannot do this in the first #! line.

Any suggestions for resolving it?

+4
source share
1 answer

If the libraries are stored in the Maven repository, accessible where you want to run it, one solution would be to use Grape to grab the libraries.

This has several advantages:

  • You do not need to worry about classpath at all
  • You do not need to distribute libraries - just make sure Groovy is available on the client
  • Libraries are downloaded only once, so even if you update the application, you only need to redistribute the .groovy file.

A simple example:

 #!/usr/bin/env groovy @Grab(group='commons-io', module='commons-io', version='2.3') import org.apache.commons.io.FileUtils ... use FileUtils like normal ... 

There are already many existing libraries at mvnrepository.com .

Even if you have non-public libraries, it is relatively easy to host your own libraries in a local / private repository .

+9
source

Source: https://habr.com/ru/post/1414051/


All Articles