为什么依赖项不会伴随Maven的make包?

时间:2016-03-09 06:22:54

标签: java maven jar

我已关注simplest maven example并制作了以下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.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
    </dependency>
  </dependencies>
</project>

然后我写了下面的代码:

package com.mycompany.app;

import org.json.simple.parser.JSONParser;
import org.json.simple.JSONObject;

public class App 
{
    public static void main(String[] args)
        throws Exception
    {
        JSONParser parser = new JSONParser();
        JSONObject cmd = (JSONObject) parser.parse("{ \"hello\": \"world\" }");
        System.out.println(cmd.toString());
    }
}

之后,编译并打包项目:

$ mvn package

最后我尝试运行jar文件:

$ java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/JSONParser
    at com.mycompany.app.App.main(App.java:11)
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.JSONParser
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

我做错了什么?

3 个答案:

答案 0 :(得分:1)

问题是,您将类路径设置为单个.jar文件,因此java命令无法找到依赖项。 (在你的情况下json-simple-1.1.1.jar ..)

您可以告诉maven在您编译类的目标文件夹中包含依赖项。

这是通过pom.xml中的以下更改来完成的:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <phase>install</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <outputDirectory>${project.build.directory}/lib</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

现在尝试 mvn软件包(或 mvn clean install ,我不确定..),您将在 / target / lib 中看到依赖项被复制。

像这样运行您的应用程序:

java -cp target/my-app-1.0-SNAPSHOT.jar:target/lib/json-simple-1.1.1.jar com.mycompany.app.App
{"hello":"world"}

答案 1 :(得分:0)

根据例外情况ClassNotFoundException: org.json.simple.parser.JSONParserjar中不存在所需的classpath文件。

在eclipse中使用以下步骤:

    Right click on project --> properties --> deployment assembly --> add 
         --> (here select appropriate file system to add jar like)java build path entries
希望它有所帮助。

答案 2 :(得分:0)

默认情况下,jar不包含其中的依赖项。您可能需要在运行时在类路径中添加所有依赖项,或者您可以使用依赖项构建jar。要构建具有依赖关系的jar,请参阅链接:http://maven.apache.org/plugins/maven-assembly-plugin/