本文介绍了Maven的 - 建立与依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载从互联网上源$ C ​​$ C和我试图与Maven来构建它。下面是与源$ C ​​$ C附带的pom.xml文件:

I've downloaded a source code from internet and I'm trying to build it with maven. Here is the pom.xml file that came with the source code:

<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</groupId>
<artifactId>myArtifact</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myArtifact</name>
<url>http://maven.apache.org</url>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>myArtifact.Main</mainClass>
                    </manifest>
                </archive>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>8.4-701.jdbc4</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
</project>

该MVN安装命令生成jar文件,但我无法执行,因为缺少相关性(在这个例子中涉及到PostgreSQL依赖找不到类除外)的文件。

The mvn install command is generating the jar file but I'm unable to execute this file because of the missing dependencies (class not found exception related to postgresql dependency in this example).

我注意到Maven有正确下载的依赖关系(罐子库都在本地Maven仓库目录),但MVN安装不复制这些库到生成jar文件。我怎样才能做到这一点?

I've noticed that maven has downloaded the dependencies correctly (the jar libraries are all at the local maven repository directory) but mvn install is not copying these libraries into the generated jar file. How can I do this?

感谢。

推荐答案

从你的Maven的编译器插件部分取出descriptorRefs和归档变量。

Remove the descriptorRefs and archive tag from your maven-compiler-plugin section.

为了生成所有自包含的依赖关系的JAR。你需要把这个到你的pom的构建插件部分。

In order to generate a JAR with all the dependencies self contained. You need to put this into the build plugins section of your pom.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>
                            com.test.your.main.class.goes.Here
                        </mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

运行在命令提示符下创建JAR文件。

Create the JAR file by running the following at the command prompt.

mvn clean compile assembly:single

这篇关于Maven的 - 建立与依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:29