Packaging Vert.x Applications as Executable JARs for Static Content Serving

Configuring Maven for Vert.x JAR Packaging

To package a Vert.x application with static resources, begin by setting up the Maven POM with necessary dependencies and the Maven Shade plugin:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>vertx-static-content</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <dependencies>
        <!-- Vert.x core -->
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
            <version>3.9.5</version>
        </dependency>
        
        <!-- Vert.x web -->
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web</artifactId>
            <version>3.9.5</version>
        </dependency>
        
        <!-- Template engines -->
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web-templ-thymeleaf</artifactId>
            <version>3.9.5</version>
        </dependency>
        
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-web-templ-handlebars</artifactId>
            <version>3.9.5</version>
        </dependency>
        
        <!-- Database clients -->
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-mysql-postgresql-client</artifactId>
            <version>3.9.5</version>
        </dependency>
        
        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>slf4j-impl</artifactId>
            <version>2.14.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</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.example.StaticContentServer</Main-Verticle>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                            <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Creating the Verticle for Static Content Serving

Create a Vert.x verticle that configures routing for static resources:

package com.example;

import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.StaticHandler;

public class StaticContentServer extends AbstractVerticle {

    @Override
    public void start() {
        Router router = Router.router(vertx);
        router.route().handler(BodyHandler.create());
        
        // Route static content from the 'web' directory
        router.route("/static/*").handler(StaticHandler.create("web"));
        
        // Create HTTP server and start listening
        vertx.createHttpServer()
            .requestHandler(router)
            .listen(8080);
    }
}

Setting Up Static Resources

Create a directory structure for your static content:

  1. In your project, create src/main/resources/web directory
  2. Place your HTML, CSS, JavaScript, and other static files in this directory
  3. The route router.route("/static/*").handler(StaticHandler.create("web")) will serve files from this directory

Building and Running the Application

Follow these steps to package and run your Vert.x application:

  1. Clean and build the project using Maven:
    mvn clean install
  2. Locate the generated JAR file in the target directory (named vertx-static-content-1.0.0-SNAPSHOT-fat.jar)
  3. Run the application:
    java -jar vertx-static-content-1.0.0-SNAPSHOT-fat.jar
  4. Access your static content in a browser at http://localhost:8080/static/your-file.html

Tags: Vert.x Maven JAR packaging Static Content web development

Posted on Mon, 28 Sep 2026 16:52:30 +0000 by oberwil