Maven - 使用依赖构建 [英] Maven - Build with Dependencies

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

问题描述

我从互联网上下载了一个源代码,我正在尝试用 maven 构建它.这是源代码附带的 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 install 命令正在生成 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 已经正确下载了依赖项(jar 库都在本地 maven 存储库目录中)但是 mvn install 没有将这些库复制到生成的 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-compiler-plugin 部分中删除 descriptorRefs 和存档标记.

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

为了生成一个包含所有依赖项的 JAR.您需要将其放入 pom.xml 的构建插件部分:

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.xml:

<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 - 使用依赖构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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