Steve
Steve

Reputation: 756

Maven Eclipse and Dependencies

I have an Maven Eclipse project and it runs fine within Eclipse, however if I attempt to run it from the command line it fails to find the dependencies.

The pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.something.or.rather.myapp</groupId>
  <artifactId>MyApp</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>MyApp</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.cloudant</groupId>
      <artifactId>cloudant-client</artifactId> 
      <version>1.0.1</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

From the project directory (MyApp):

mvn eclipse:eclipse    // runs fine
mvn package            // runs fine

java -cp target/MyApp-1.0-SNAPSHOT.jar com.something.or.rather.myapp.MyMainClass  // fails

...
Exception in thread "main" java.lang.NoClassDefFoundError: com/cloudant/client/api/CloudantClient
...

I notice if I put the full path of the Cloudant JAR file in the java command it resolves that exception only to raise another.

java -cp /full/path/to/cloudant-client-1.0.1jar:target/MyApp-1.0-SNAPSHOT.jar com.something.or.rather.myapp.MyMainClass  // still fails

...
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
...

Any ideas? This is painful.

Upvotes: 0

Views: 314

Answers (1)

Andrew McKee
Andrew McKee

Reputation: 1377

By default maven builds thin jars without the project dependencies, only the project classes.

What you want is a fat jar. To do this have a look at:Building a fat jar using maven

Hope this helps

Upvotes: 2

Related Questions