为什么依赖关系不伴随Maven的make软件包? [英] Why dependencies do not accompany the made package by Maven?

查看:96
本文介绍了为什么依赖关系不伴随Maven的make软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了最简单的maven示例并制作了以下pom.xml文件:

I've followed the simplest maven example and made the following pom.xml file:

<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>

然后,我编写了以下代码:

Then I wrote the following code:

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());
    }
}

之后,编译并打包项目:

After that, compiled and packaged the project:

$ mvn package

最后,我尝试运行jar文件:

Finally I tried to run the jar file:

$ 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

我在做什么错了?

推荐答案

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

The problem is, you are setting the classpath to a single .jar file, thus java command can not find the dependency. (json-simple-1.1.1.jar in your case..)

您可以告诉Maven在将类编译到的目标文件夹中包括依赖项.

You can tell maven to include the dependencies in target folder that you compiling your class to.

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

This is done by the following change in the 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全新安装.),您会在/target/lib 中看到依赖项被复制.

Now try mvn package (or mvn clean install, I am not sure..) and you will see in /target/lib the dependencies are copied.

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

Run your application like this:

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

这篇关于为什么依赖关系不伴随Maven的make软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆