user3772960 user3772960 - 6 months ago 19
Java Question

Maven not installing dependency

I have the following (short)

pom.xml
for a Maven project. I added the
com.google.collections
dependency, yet I do not see any of the classes for this dependency on in the
/target/classes
directory when I do a
maven clean package
. Furthermore, when I execute the JAR, I get an error (
java.lang.NoClassDefFoundError: com/google/common/collect/Iterables
). What am I forgetting to do?

<project>
<groupId>edu.berkeley</groupId>
<artifactId>java-page-rank</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<modelVersion>4.0.0</modelVersion>
<name>PageRank</name>
<packaging>jar</packaging>
<version>1.0</version>
<dependencies>
<dependency> <!-- Spark dependency -->
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>1.6.0</version>
</dependency>
<!-- http://mvnrepository.com/artifact/com.google.collections/google-collections -->
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>

Answer

You won't see dependencies in target/classes, those are just used for compilation and taken from your $HOME/.m2/repository.

If you need to run the resulting jar you will need to either:

  1. Construct the full classpath with all the dependencies (jars that you use from the local maven repository $HOME/.m2/repository.
  2. Create an uberjar that will contain all the classes packaged into a single jar (you can use maven assembly plugin or shade plugin for that).

E.g. for assembly plugin you will need to add plugin to the plugins section:

  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>package.for.the.start.Main</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>

And later execute maven with assembly:single, e.g:

mvn clean package assembly:single

The resulting jar will be target/java-page-rank-1.0-SNAPSHOT-jar-with-dependencies.jar.

Comments