Creating an executable jar using a groovy script

I have a groovy script Automate.groovy . The script does not have a class definition or main method. I am looking for the easiest way to create a jar containing a script. I tried compiling a groovy script with the following command:

 groovyc Automate.groovy 

I also have a manifest manifest manifest-add.txt that indicates the main class

 Main-Class: Automate 

And I created a jar using the command:

 jar cvfm Automate.jar mainfest-addition.txt *.class 

This gave me a jar with Automate.class and Automate $ 1.class and a manifest file with the main class. But when I tried to start the jar using java -jar Automate.jar , I always get an error

 cannot find or load main class Automate. 

What am I doing wrong here?

There should be an easy way to create a jar. I just have this script file that I want in my bank, and I don't want to use maven / ant / gradle to do this job. Isn't there a simple and easy way to do this?

+5
source share
2 answers

The class file has Groovy dependencies. Using an embedded can will fix the problem.

Using Groovy 2.4.5 if I have this script for Automate.groovy

 println "Hello from Automate.groovy" 

and use this manifest.txt file:

 Main-class: Automate Class-path: jar/groovy-all-2.4.5.jar 

and this simple script layout:

 rm *.class groovyc Automate.groovy jar cvfm Automate.jar manifest.txt Automate.class 

and in the control directory I do the following:

 bash$ mkdir jar bash$ cp $GROOVY_HOME/embeddable/groovy-all-2.4.5.jar jar 

then this works:

 bash$ java -jar Automate.jar Hello from Automate.groovy 

The solution will be much better if the built-in jar is included in Automate.jar itself. An example of this using Gradle is this project .

+7
source

The actual problem was @Grab Annotation, which was there in my script. I created a Gradle project and added a very simple HelloWorld.groovy script with just one println expression, but also used @Grab to just import something. When I built the jar, he will not find the main class. When I googled around, I found that the @Grab annotation is for use in stand-alone scripts, not for a precompiled script. Therefore, if you need to have @Grab in compiled groovy, you need to add an additional ivy dependency or choose not to have @Grab and use another way to include dependencies. I removed @Grab from my script and then built it. Thus, the jar performed well.

Sorry to deviate from the fact that I did not want to use Gradle / Maven. For almost two days I tried everything I could find on stackoverflow and google, and eventually tried Gradle. I'm not sure if this is the only solution to the problem, but it worked for me.

-1
source

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


All Articles