简体   繁体   English

Maven Eclipse和依赖项

[英]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. 我有一个Maven Eclipse项目,它在Eclipse中运行良好,但是,如果我尝试从命令行运行它,它将找不到依赖项。

The pom.xml file: pom.xml文件:

<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): 从项目目录(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. 我注意到,如果我将Cloudant JAR文件的完整路径放在java命令中,它将解决该异常,仅引发另一个异常。

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. 真痛苦

By default maven builds thin jars without the project dependencies, only the project classes. 默认情况下,maven生成没有项目依赖关系的精简jar,仅生成项目类。

What you want is a fat jar. 你想要的是一个胖子罐。 To do this have a look at: Building a fat jar using maven 为此,请看以下内容: 使用Maven构建胖罐

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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