从现在开始一周,我正试图在日食中创建一个带有maven的可执行jar,但我遇到了麻烦。这是我拥有的和我做的:
我的pom.xml文件的内容:
<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>B4D</groupId>
<artifactId>B4D</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>B4D</name>
<description>Bot for Dofus</description>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>Cp1252</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fr.B4D.gui.JFrame_B4D</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.28.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
<version>v4-rev493-1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sqladmin</artifactId>
<version>v1beta4-rev66-1.25.0</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-drive</artifactId>
<version>v3-rev136-1.25.0</version>
</dependency>
<dependency>
<groupId>com.github.kirviq</groupId>
<artifactId>dumbster</artifactId>
<version>1.7.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jpcap</groupId>
<artifactId>capture</artifactId>
<version>0.01.16</version>
<scope>system</scope>
<systemPath>${project.basedir}/jpcap/net.sourceforge.jpcap-0.01.16.jar</systemPath>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
我通过以下方式构建我的项目:
项目>运行方式> Maven构建>目标:包>运行
这是控制台输出:
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for B4D:B4D:jar:1.0
[WARNING] 'dependencies.dependency.systemPath' for net.sourceforge.jpcap:capture:jar should not point at files within the project directory, ${project.basedir}/jpcap/net.sourceforge.jpcap-0.01.16.jar will be unresolvable by dependent projects @ line 115, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building B4D 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ B4D ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 32 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ B4D ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 84 source files to D:\Lucas\Projets personnels\Eclipse Windows\B4D\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ B4D ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ B4D ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to D:\Lucas\Projets personnels\Eclipse Windows\B4D\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ B4D ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ B4D ---
[INFO] Building jar: D:\Lucas\Projets personnels\Eclipse Windows\B4D\target\B4D-1.0.jar
[INFO]
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (make-assembly) @ B4D ---
[INFO] META-INF/ already added, skipping
[INFO] META-INF/MANIFEST.MF already added, skipping
................
[INFO] com/google/api/ already added, skipping
[INFO] com/google/api/services/ already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:01 min
[INFO] Finished at: 2019-03-14T16:54:57+01:00
[INFO] Final Memory: 27M/307M
[INFO] ------------------------------------------------------------------------
当我尝试在控制台中运行jar文件时:
$ java -jar B4D-1.0-jar-with-dependencies.jar
Exception in thread "main" java.lang.NoClassDefFoundError: net/sourceforge/jpcap /capture/InvalidFilterException
at fr.B4D.gui.JFrame_B4D.<init>(JFrame_B4D.java:64)
at fr.B4D.gui.JFrame_B4D.main(JFrame_B4D.java:55)
Caused by: java.lang.ClassNotFoundException: net.sourceforge.jpcap.capture.Inval idFilterException
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
请注意,我的项目使用本地jar文件依赖项:net.sourceforge.jpcap-0.01.16.jar。看起来这个依赖项错误地包含在最终的jar文件中。当然,我发现了很多关于如何创建可执行jar文件以及如何包含.jar文件的主题。但他们都没有解决我的问题。我的pom.xml应该有什么问题,但是什么?
非常感谢
MrDomoo
我无法使用Top-Q Public repository因为缺少了神器。所以我必须将JAR安装到我的本地Maven存储库中:
运行>运行配置> Maven构建(双击)
目标是:
install:install-file -Dfile=${basedir}/src/main/resources/net.sourceforge.jpcap-0.01.16.jar -DgroupId=net.sourceforge.jpcap -DartifactId=local -Dversion=0.01.16 -Dpackaging=jar
我现在可以在pom.xml中,只需使用:
<dependency>
<groupId>net.sourceforge.jpcap</groupId>
<artifactId>local</artifactId>
<version>0.01.16</version>
</dependency>
感谢deHaar