How to create a vertx fat-jar with maven

H

If you use Vertx, It could be helpful to create a jar or war where Vertx is included. If you use maven. You can add the following code in your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
 implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>io.vertx.core.Starter</Main-Class>
                                    <Main-Verticle>com.laurenthinoul....ApplicationMainClass</Main-Verticle>
				</manifestEntries>
			    </transformer>
			</transformers>
			<artifactSet />
			<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
		    </configuration>
		</execution>
	    </executions>
        </plugin>
    </plugins>
</build>

If you integrated Spring, you need to add extra transformers. You can use the following code in your pom.xml for that:

<build>
    <plugins>
        <plugin>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-shade-plugin</artifactId>
		<version>2.4.1</version>
		<executions>
		    <execution>
		        <phase>package</phase>
		        <goals>
			    <goal>shade</goal>
			</goals>
			<configuration>
			    <transformers>
			        <transformer
 implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
			           <manifestEntries>
			             <Main-Class>io.vertx.core.Starter</Main-Class>
			             <Main-Verticle>com.laurenthinoul....ApplicationMainClass</Main-Verticle>
			         </manifestEntries>
			     </transformer>
			     <transformer
 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
			         <resource>META-INF/spring.handlers</resource>
			     </transformer>
			     <transformer
 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
			         <resource>META-INF/spring.schemas</resource>
			     </transformer>
			     <transformer
 implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
			        <resource>META-INF/spring.tooling</resource>
			    </transformer>
			</transformers>
			<artifactSet />
		        <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
		    </configuration>
	        </execution>
	    </executions>
        </plugin>
    </plugins>
</build>

Don’t forget to change ‘com.laurenthinoul….ApplicationMainClass’ to your own Main class!

Now if you perform a maven install, maven will create a jar named: applicationname-version-fat.jar. So for example something like testapp-1.0.0-fat.jar.

Add comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Tag Cloud

Categories