Wednesday, 15 July 2015

java - Is it possible to create an "uber" jar containing the project classes and the project dependencies as jars with a custom manifest file? -


I am trying to create an executable jar (using Maven) which contains the project class and this is the manifest file Dependency is the entry for the main class and class path entry which indicates the dependency packaged in the root of the jar; Something like this:

Manifest file:

 .... Main category: com.acme.MainClass class-path: dependecy1.jar dependecy2.jar ..... 

Jar:

 Jar-Route | - .... | - com / acme /../*. Class | - Dependency 1 .jar `- Dependency .2.jor 

Maven-Shade-plugin but dependencies have been unpacked for creating a manifest file and creating" Uber "Jar and using my Jar Classes have been added in the form of

Actually, I did not check that maven-shade-plugin is absolutely (Or any other plug-in) because everything has been built to make megazar or uberjar in Maven 2. You just have to use maven-assembly-plugin with predefined jar-of-dependency descriptors.

To customize the manifest: just add this snippet to your pom.xml

  & lt; Plugin & gt; & Lt; Group & gt; Org.apache.maven.plugins & lt; / Group & gt; & Lt; ArtifactId & gt; Maven-assembly-plug-in & lt; / ArtifactId> & Lt; Configuration & gt; & Lt; Collections & gt; & Lt; Appearance & gt; & Lt; MainClass & gt; My.package.to.my.MainClass & lt; / MainClass & gt; & Lt; / Reveal & gt; & Lt; / Collection & gt; & Lt; / Configuration & gt; & Lt; / Plugin & gt;  

and the following command will generate your uberjar:

  mvn assembly: assembly-ddescriptorId = jar-of-dependence  

But, again, this is to open the default behavior dependency of the descriptor (e.g. Maven Shade-Plugin). To be honest, I do not find why this is a problem, but if you really do not want it, you can use your own custom assembly descriptor. To do this, first, create your assembly statement, let's assume that src / assembly / uberjar.xml , with the following content:

  & lt; Assembly & gt; & Lt; ID & gt; Uberjar & lt; / Id & gt; & Lt; Formats & gt; & Lt; Format & gt; Jar & lt; / Format & gt; & Lt; / Formats & gt; & Lt; IncludeBaseDirectory & gt; False & lt; / IncludeBaseDirectory & gt; & Lt; DependencySets & gt; & Lt; DependencySet & gt; & Lt; Open & gt; False & lt; / Shell> & Lt; Scope & gt; Sequence & lt; / Scope & gt; & Lt; UseProjectArtifact & gt; False & lt; / UseProjectArtifact & gt; & Lt; / DependencySet & gt; & Lt; / DependencySets & gt; & Lt; Fileset & gt; & Lt; Fileset & gt; & Lt; Directory & gt; $ {Project.build.outputDirectory} & lt; / Directory & gt; & Lt; OutputDirectory & gt; / & Lt; / OutputDirectory & gt; & Lt; / Fileset & gt; & Lt; / Fileset & gt; & Lt; / Assembly & gt; After that, configure the maven-assembly-plugin to use this scanner and add the dependency to the entry of the  class-path  manifest: 

  & lt; Plugin & gt; & Lt; Group & gt; Org.apache.maven.plugins & lt; / Group & gt; & Lt; ArtifactId & gt; Maven-assembly-plug-in & lt; / ArtifactId> & Lt; Configuration & gt; & Lt; Descriptor & gt; & Lt; Descriptor & gt; Src / assembly / uberjar.xml & lt; / Narrator & gt; & Lt; / Narrator & gt; & Lt; Collections & gt; & Lt; Appearance & gt; & Lt; MainClass & gt; My.package.to.my.MainClass & lt; / MainClass & gt; & Lt; AddClasspath & gt; True & lt; / AddClasspath & gt; & Lt; / Reveal & gt; & Lt; / Collection & gt; & Lt; / Configuration & gt; & Lt ;! - & lt; Executions & gt; & Lt; Execution & gt; & Lt; Step & gt; Package & lt; / Step & gt; & Lt; Goals & gt; & Lt; Goal & gt; A & lt; / Target & gt; & Lt; / Targets & gt; & Lt; / Execution & gt; & Lt; / Hanging & gt; - & gt; & Lt; / Plugin & gt;  

Finally create your uberjar to run mvn assembly: assembly

Alternatively, package to cancel the executions element (and the assembly has been produced as part of normal construction).


No comments:

Post a Comment