[英]Maven not installing dependency
I have the following (short) pom.xml
for a Maven project. 对于Maven项目,我具有以下(简短的) pom.xml
。 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
. 我添加了com.google.collections
依赖关系,但是当我执行maven clean package
时,在/target/classes
目录中没有看到任何依赖于此依赖关系的/target/classes
。 Furthermore, when I execute the JAR, I get an error ( java.lang.NoClassDefFoundError: com/google/common/collect/Iterables
). 此外,当我执行JAR时,会收到错误消息( 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>
You won't see dependencies in target/classes
, those are just used for compilation and taken from your $HOME/.m2/repository
. 您不会在target/classes
看到依赖项,这些依赖项仅用于编译并从$HOME/.m2/repository
。
If you need to run the resulting jar you will need to either: 如果需要运行生成的jar,则需要执行以下任一操作:
$HOME/.m2/repository
. 构造具有所有依赖项的完整类路径(您从本地maven存储库$HOME/.m2/repository
使用的jar。 Eg 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
, eg: 然后使用assembly:single
执行Maven,例如:
mvn clean package assembly:single
The resulting jar will be target/java-page-rank-1.0-SNAPSHOT-jar-with-dependencies.jar
. 产生的jar将为target/java-page-rank-1.0-SNAPSHOT-jar-with-dependencies.jar
。
Use maven-assembly-plugin
instead of maven-compiler-plugin
if you want your jar to contain all the dependencies 如果您想让jar包含所有依赖项,请使用maven-assembly-plugin
代替maven-compiler-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.