Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Assembly Plugin is not setting the MainClass manifest setting

Tags:

I have a maven project which generates a jar via the maven assembly plugin I want to run as a console app. However, the MainClass attribute is not being set in MANIFEST.MF. Here is my plugin configuration:

        <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-assembly-plugin</artifactId>             <version>2.2.1</version>             <configuration>                 <descriptorRefs>                     <descriptorRef>jar-with-dependencies</descriptorRef>                 </descriptorRefs>                 <archive>                     <manifest>                         <mainClass>net.justaprogrammer.poi.cleanser.Cleanser</mainClass>                     </manifest>                 </archive>             </configuration>         </plugin> 

However, this does not get added to the MANIFEST.MF in the jar generated by mvn package. The manifest generated is below:

Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: zippy Build-Jdk: 1.6.0_25 

What am I doing wrong?

like image 886
Justin Dearing Avatar asked Oct 16 '11 15:10

Justin Dearing


1 Answers

I missed that you weren't generating your assembly on package. You have a jar project, so Maven will build a jar using the maven-jar-plugin. You don't have to have anything in your pom to tell it that. That's Maven's convention-over-configuration working for you. The jar it builds will have only your project classes and resources in it. If you want to add a Main-Class to the manifest in that jar, you should configure the jar plugin to do so. Basically, just move that archive configuration to the jar plugin.

However, if you actually want to assemble an executable fat jar--that is, a jar that includes all of your classes as well as the classes of all of your dependencies--then you have the setting in the right place, but you need to actually run the assembly plugin either using mvn assembly:single or by binding that goal to a lifecycle phase. To be clear, if you do this, then your project will output two jars: one that contains your project files and one that contains that plus the contents of all the libraries that your project depends on. The former is built by the jar plugin. That latter is built by the assembly plugin. Note that fat jars aren't commonly used, and you can run into unusual problems when you use them because they're rather outside the realm of normal Java stuff.

like image 50
Ryan Stewart Avatar answered Oct 07 '22 16:10

Ryan Stewart