java - How to build executable jar using maven-jar-plugin and groovy? -


i have example app main class in groovy , main class in java. maven-jar-plugin builds executable jar when java class specified main class not when groovy class is. let me demonstrate. here 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 , 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 java! 

then, changing mainclass to:

<mainclass>com.example.hello.groovyhello</mainclass> 

and running 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: not find or load main class com.example.hello.groovyhello 

each class has main method println statement.

so how can executable jar groovy classes? maven-shade-plugin works don't want fat jar - want dependencies excluded.

i see note dependencies excluded, why don't want use maven-shaded-plugin. i'm not sure how work groovy class.

(but it's worth noting explicitly myself , other java newbies: maven-jar-plugin not include dependencies)

first possibility: java barfing because can't find groovy, , wants it?

i found so answer ability load main classes extend dependent classes.

i know plain groovy script file, without explicit class declaration, compiler wraps in class extends (groovy's) script. don't know happens if write own explicit class - i'd unpackage .jar , use java decompiler @ generated code, see if gives clues.

second possibility: package name

that feature groovy creating class if haven't explicitly declared one, doesn't seem able put implicit java class inside package based on directory structure. thought worked in past: not sure if i'm seeing bug here, new feature, or if i'm mis-remembering.

so, 2 ways fix this:

  1. add package declaration groovy script.

    package com.example.hello println "i'm groovy!" 
  2. have maven mainclass tag refer class without package namespace (ie: groovyhello instead of com.example.hello.groovyhello


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -