Create multiple runnable Jars (with dependencies included) from a single Maven project

Martin Thurau picture Martin Thurau · Jan 4, 2012 · Viewed 47.1k times · Source

I have a single maven project that has multiple main classes. I want to generate runnable Jars (that include all dependencies) out of these project. I currently have the following build configuration (using maven.assembly):

<build>
<plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <mainClass>classpath.to.my.mainClass</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    </plugin>
</plugins>
</build>

Is their a way to achive this with maven-assembly? If not, what is the simplest way to achive my goal?

Answer

Donal Fellows picture Donal Fellows · Jan 4, 2012

You can do it. You'll need a separate execution for each artifact that you're building (i.e., give each its own id but you can leave the phase as default), and you'll need to specify the finalName and archive/manifest/mainClass for each.

<build>
<plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>build-a</id>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>foobar.Aclass</mainClass>
                </manifest>
              </archive>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <finalName>foobar_a.jar</finalName>
            </configuration>
          </execution>
          <execution>
            <id>build-b</id>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>foobar.Bclass</mainClass>
                </manifest>
              </archive>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <finalName>foobar_b.jar</finalName>
            </configuration>
          </execution>
        </executions>
    </plugin>
</plugins>
</build>