This question already has answers here:
How can I create an executable JAR with dependencies using Maven?
(30个答案)
3年前关闭。
我们和一个朋友一起为我们的学校从事一个项目,我们到了最后。
问题是,使用Eclipse,我们的java程序可以正常工作,并且也可以使用eclipse生成的JAR。但是,当您执行命令mvn包时,我们需要生成JAR。这个JAR找不到外部JAR的类,这是我们第一次使用Maven,所以您能帮我制作一个好的pom.xml吗,当我们执行mvn打包时,它会使用外部JAR生成一个JAR吗?
有pom:
这将产生两个罐子。其中一个将是普通罐子,第二个将是一个胖罐子,并在其名称后附加
如果这样做,我认为您不必像在pom.xml中那样配置maven-dependency插件以及maven jar-plugin。您可以忽略它。
(30个答案)
3年前关闭。
我们和一个朋友一起为我们的学校从事一个项目,我们到了最后。
问题是,使用Eclipse,我们的java程序可以正常工作,并且也可以使用eclipse生成的JAR。但是,当您执行命令mvn包时,我们需要生成JAR。这个JAR找不到外部JAR的类,这是我们第一次使用Maven,所以您能帮我制作一个好的pom.xml吗,当我们执行mvn打包时,它会使用外部JAR生成一个JAR吗?
有pom:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>coo.project</groupId>
<artifactId>COO-DONJON</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>COO-DONJON</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>javazoom</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>coo.project.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
最佳答案
您将必须使用Maven Assembly Plugin。
您需要一个可执行jar,其中应包含所有依赖项。检查下面的示例代码。
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>coo.project.App</mainClass> // your main class here.
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
这将产生两个罐子。其中一个将是普通罐子,第二个将是一个胖罐子,并在其名称后附加
-jar-with-dependencies
。这是您现在应该使用的罐子。这个胖子罐将包含您的代码以及代码所需的所有依赖项。如果这样做,我认为您不必像在pom.xml中那样配置maven-dependency插件以及maven jar-plugin。您可以忽略它。
关于java - 使用MAVEN的外部JAR制作JAR的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40049483/