Search code examples
javamavenmaven-assembly-plugin

With maven how to bundle all dependencies AND my Java project filesin the output jar when using `maven-assembly-plugin`?


I'm using maven-assembly-plugin to create a output jar from my maven project. The relevant protions of my pom.xml look like this:

.
.
.
<dependencies>
    <dependency>
      .
      .
      .
    </dependency>
    .
    .
    .
</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        .
        .
        .
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>my.package.TheApp</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
        .
        .
        .
    </plugins>
</build>
.
.
.

When I run `mvn clean install assembly:single`` the resulting jar file does contain the dependencies, but it does not contain my project files. Running the jar with

java -jar TheApp-0.1.2-SNAPSHOT-jar-with-dependencies.jar

I get the message Error: Could not find or load main class my.package.TheApp. So how can I include my project files? (I thought of setting useProjectArtifact, but according to the maven-assembly-plugin doco this property is by default true.)


Solution

  • By poking around on Stack Overflow I found the solution:

    Run mvn clean compile assembly:single instead of mvn clean install assembly:single. :-)

    Or even better bind in the pom.xml the assembly to the package phase (thanks @ khmarbaise!):

    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      .
      .
      .
      <executions>
        <execution>
          <id>make-assembly</id> <!-- this is used for inheritance merges -->
          <phase>package</phase> <!-- bind to the packaging phase -->
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    

    and then run mvn clean package (or mvn clean install if you want to).