【问题标题】:Why dependencies do not accompany the made package by Maven?为什么依赖项不伴随 Maven 制作的包?
【发布时间】:2016-03-09 06:22:53
【问题描述】:

我已关注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

我做错了什么?

【问题讨论】:

    标签: java maven jar


    【解决方案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 package(或者 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"}
    

    【讨论】:

    • 为了实现包目标,phase 应该是 package 然后mvn package 将复制库。
    【解决方案2】:

    根据异常ClassNotFoundException: org.json.simple.parser.JSONParserclasspath 中不存在所需的jar 文件。

    在eclipse中使用以下步骤:

        Right click on project --> properties --> deployment assembly --> add 
             --> (here select appropriate file system to add jar like)java build path entries
    

    希望有帮助。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-19
        • 2015-04-13
        • 2023-03-24
        • 1970-01-01
        • 2019-04-16
        • 1970-01-01
        • 2013-11-25
        • 1970-01-01
        相关资源
        最近更新 更多