gpt4 book ai didi

java - 使用外部 JAR 和 MAVEN 制作 JAR 时出现问题

转载 作者:行者123 更新时间:2023-12-01 09:22:49 28 4
gpt4 key购买 nike

我们和一个 friend 一起为学校做一个项目,现在已经结束了。问题是,使用 Eclipse,我们的 java 程序可以正常工作,并且使用 eclipse 生成的 JAR 也可以正常工作。但是我们需要在执行命令 mvn package 时生成一个 JAR。这个JAR没有找到External JAR的类,这是我们第一次使用Maven,所以你能帮我制作一个好的pom.xml,当我们进行mvn打包时,它会使用External 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 。其中一个是常规 jar,第二个是一个 fat jar,其名称后附加了 -jar-with-dependencies。这是您现在应该使用的 jar。这个 fat jar 将包含您的代码以及您的代码所需的所有依赖项。

如果您这样做,我认为您不必像 pom.xml 中那样配置 maven-dependency 插件以及 maven jar-plugin。你可以忽略它。

关于java - 使用外部 JAR 和 MAVEN 制作 JAR 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40049483/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com