user1332148
user1332148

Reputation: 1346

How to create multiple executable JARs from one module using Eclipse/Maven?

We use Maven to build an Eclipse Java module. Several of the classes in the module have main functions and we would like to create an executable JAR file for each of them. Is this possible? If so how?

Upvotes: 2

Views: 665

Answers (1)

Tunaki
Tunaki

Reputation: 137084

Yes, this is possible. You just need to define multiple executions of the plugin you're using to make an executable JAR.

One good approach would be to use the maven-shade-plugin to make the executable jar. All the common configuration is placed in the execution-independent section, which in this case, just specifies to attach the shaded JAR to the build. Then each execution only defines the main class to use and the classifier of the resulting Maven artifact.

In the following example configuration, there are 2 executable JARs created, the first with Class1 as main class and the second with Class2 as main class.

<plugin>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.4.3</version>
  <executions>
    <execution>
      <id>class1</id>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>my.main.Class1</mainClass>
          </transformer>
        </transformers>
        <shadedClassifierName>class1</shadedClassifierName>
      </configuration>
    </execution>
    <execution>
      <id>class2</id>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <transformers>
          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>my.main.Class2</mainClass>
          </transformer>
        </transformers>
        <shadedClassifierName>class2</shadedClassifierName>
      </configuration>
    </execution>
  </executions>
  <configuration>
    <shadedArtifactAttached>true</shadedArtifactAttached>
    <createDependencyReducedPom>false</createDependencyReducedPom>
  </configuration>
</plugin>

Upvotes: 3

Related Questions