简体   繁体   English

Maven没有安装依赖项

[英]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,则需要执行以下任一操作:

  1. Construct the full classpath with all the dependencies (jars that you use from the local maven repository $HOME/.m2/repository . 构造具有所有依赖项的完整类路径(您从本地maven存储库$HOME/.m2/repository使用的jar。
  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). 创建一个uberjar,它将包含打包到单个jar中的所有类(您可以使用maven程序集插件或shade插件)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM