Maven 没有安装依赖项
Maven not installing dependency
我有以下(简短的)pom.xml
Maven 项目。我添加了 com.google.collections
依赖项,但是当我执行 maven clean package
时,我在 /target/classes
目录中没有看到此依赖项的任何 类。此外,当我执行 JAR 时,出现错误 (java.lang.NoClassDefFoundError: com/google/common/collect/Iterables
)。我忘了做什么?
<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>
您不会在 target/classes
中看到依赖项,它们仅用于编译并取自您的 $HOME/.m2/repository
。
如果您需要 运行 生成的 jar,您将需要:
- 构建包含所有依赖项的完整类路径(您从本地 maven 存储库使用的 jar
$HOME/.m2/repository
。
- 创建一个包含所有 类 打包到一个 jar 中的 uberjar(您可以为此使用 maven assembly 插件或 shade 插件)。
例如对于 assembly plugin,您需要将插件添加到插件部分:
<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>
然后用assembly:single
执行maven,例如:
mvn clean package assembly:single
生成的 jar 将是 target/java-page-rank-1.0-SNAPSHOT-jar-with-dependencies.jar
。
如果您希望 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>
我有以下(简短的)pom.xml
Maven 项目。我添加了 com.google.collections
依赖项,但是当我执行 maven clean package
时,我在 /target/classes
目录中没有看到此依赖项的任何 类。此外,当我执行 JAR 时,出现错误 (java.lang.NoClassDefFoundError: com/google/common/collect/Iterables
)。我忘了做什么?
<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>
您不会在 target/classes
中看到依赖项,它们仅用于编译并取自您的 $HOME/.m2/repository
。
如果您需要 运行 生成的 jar,您将需要:
- 构建包含所有依赖项的完整类路径(您从本地 maven 存储库使用的 jar
$HOME/.m2/repository
。 - 创建一个包含所有 类 打包到一个 jar 中的 uberjar(您可以为此使用 maven assembly 插件或 shade 插件)。
例如对于 assembly plugin,您需要将插件添加到插件部分:
<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>
然后用assembly:single
执行maven,例如:
mvn clean package assembly:single
生成的 jar 将是 target/java-page-rank-1.0-SNAPSHOT-jar-with-dependencies.jar
。
如果您希望 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>