views:

1968

answers:

2

I'm relatively new to the maven mantra, but I'm trying to build a command-line runnable jar with maven. I've setup my dependencies, but when I run mvn install and attempt to run the jar, two things happen. First, no main class is found, which is correctable. When I've corrected this, I get errors on run stating that classes cannot be found.

Maven is not packaging my dependency libraries inside of the jar, so I am unable to run the jar as a stand-alone application. How do I correct this?

+4  A: 

Maven is not packaging your dependencies inside your jar file, because you don't usually do this with Java programs.

Instead you deliver the dependencies together with your jar file and mention them in the Class-Path header of the Manifest.

To go this route, you'll need to enable the addClasspath property (documented here) for the maven-jar-plugin as shown by Bozho.

If you really want to include all your dependencies in your jar file, then you can use the Maven Assembly plugin to create a jar-with-dependencies.

Joachim Sauer
+8  A: 

The easiest way to do this would be to create an assembly using the maven-assembly-plugin and the predefined jar-with-dependencies descriptor. You'll also need to generate a manifest with a main-class entry for this uber jar. The snippet below shows how to configure the assembly plugin to do so:

<build>
  <plugin>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

Then, to generate the assembly, just run:

mvn assembly:assembly

If you want to generate the assembly as part of your build, simply bind the assembly:single mojo to the package phase:

<build>
  <plugin>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

And simply run:

mvn package
Pascal Thivent
The maven-shade-plugin also works similarly and is dedicated to this purpose.
Dominic Mitchell
This does not work for spring, and other libraries that expect multiple resources with the same name on the class path. In the case of spring, META-INF\spring.schemas is included in several jars (beans, jdbc, etc) and is used to configure XML namespace support for the XML parser. If you try 'jar-with-dependencies', pasing application-context.xml will fail at runtime with something like "org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'."
David Roussel
@David Maven won't solve a Java "limitation". Just package your application cleanly (i.e. not in a uberjar) in that case.
Pascal Thivent