Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building Distinct Executable JARs for Multiple Java Main Classes

Tech May 5 14

When a single codebase contains multiple standalone applications, each with its own entry point, the build process must generate separate executable artifacts. Consider a project containing both a command-line utility and a network service.

Define the entry points in separate packages to maintain clear bonudaries:

package com.myapp.cli;

public class DataImporter {
    public static void main(String[] arguments) {
        System.out.println("Initializing data import sequence");
    }
}
package com.myapp.api;

public class RestGateway {
    public static void main(String[] arguments) {
        System.out.println("Starting HTTP gateway on port 8080");
    }
}

Configure the maven-shade-plugin to create multiple shaded JARs during the package phase. Each execution generates an independent artifact with its own manifest pointing to the correct main class:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.5.1</version>
      <executions>
        <execution>
          <id>package-importer</id>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <shadedArtifactAttached>true</shadedArtifactAttached>
            <shadedClassifierName>importer</shadedClassifierName>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>com.myapp.cli.DataImporter</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
        <execution>
          <id>package-gateway</id>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <shadedArtifactAttached>true</shadedArtifactAttached>
            <shadedClassifierName>gateway</shadedClassifierName>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <mainClass>com.myapp.api.RestGateway</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Execute the build to generate all artifacts:

mvn clean package

This porduces three JAR files in target/: the original unshaded JAR, plus two executable varients with classifiers importer and gateway. Run either application using:

java -jar target/myapp-1.0.0-importer.jar
java -jar target/myapp-1.0.0-gateway.jar
Tags: Javamaven

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.