I have an example app with a main class in Groovy and a main class in Java. maven-jar-plugin successfully builds an executable jar when the Java class is specified as the main class but not when the Groovy class is. Let me demonstrate. Here is my build:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.hello.JavaHello</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<verbose>false</verbose>
<source>${java.compiler.version}</source>
<target>${java.compiler.version}</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>${groovy-eclipse-compiler.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.4.3-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Then compiling and running:
$ mvn -q clean package && \
> jar xf target/hello-world-1.0-SNAPSHOT.jar META-INF/MANIFEST.MF && \
> cat META-INF/MANIFEST.MF && rm -r META-INF && \
> java -jar target/hello-world-1.0-SNAPSHOT.jar
Manifest-Version: 1.0
Built-By: tytk
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_66
Main-Class: com.example.hello.JavaHello
Hello from Java!
Then, changing the mainClass
to:
<mainClass>com.example.hello.GroovyHello</mainClass>
And running the same command:
$ mvn -q clean package && \
> jar xf target/hello-world-1.0-SNAPSHOT.jar META-INF/MANIFEST.MF && \
> cat META-INF/MANIFEST.MF && rm -r META-INF && \
> java -jar target/hello-world-1.0-SNAPSHOT.jar
Manifest-Version: 1.0
Built-By: tytk
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_66
Main-Class: com.example.hello.GroovyHello
Error: Could not find or load main class com.example.hello.GroovyHello
Each class just has a main method with a println statement.
So how can I get an executable jar with my groovy classes? maven-shade-plugin works but I don't want a fat jar - I want dependencies excluded.