【发布时间】:2019-10-17 20:09:19
【问题描述】:
我正在使用 Maven 构建一个小应用程序,它具有依赖项并且是一个可执行的 jar 文件。
这是我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.company</groupId>
<artifactId>site-downloader</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.company.siteripper</groupId>
<artifactId>common-interfaces</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.company.Main
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
构建完成没有问题,但是在执行它写入的 jar 时
没有主要清单属性,在 本地 maven repo / 我的 jar 中。
我寻求帮助以使 jar 包含其依赖项并同时可执行。
编辑:
我更新了我的pom 以使用 jar 和依赖项插件而不是程序集。这是更新的 pom:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.company</groupId>
<artifactId>site-downloader</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.company.siteripper</groupId>
<artifactId>common-interfaces</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>
com.company.Main
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<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}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
但我在运行它时得到一个NoClassDefFoundError:
C:\Users\myusername\Documents\Commands>调用 java -jar D:.m2\repository\com\company\site-downloader\1.0.0\site-downloader-1.0.0.jar -9gag https://9gag.com/gag/arGEvRX?ref C:\Users\myusername\Documents\sites\data\android 错误:JNI 错误 已发生,请检查您的安装并重试异常 在线程“主”java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver 在 java.lang.Class.getDeclaredMethods0(本机方法) 在 java.lang.Class.privateGetDeclaredMethods(未知来源) 在 java.lang.Class.privateGetMethodRecursive(未知来源) 在 java.lang.Class.getMethod0(未知来源) 在 java.lang.Class.getMethod(未知来源) 在 sun.launcher.LauncherHelper.validateMainClass(未知来源) 在 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) 引起:java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver 在 java.net.URLClassLoader.findClass(未知来源) 在 java.lang.ClassLoader.loadClass(未知来源) 在 sun.misc.Launcher$AppClassLoader.loadClass(未知来源) 在 java.lang.ClassLoader.loadClass(未知来源) ... 7 更多
【问题讨论】: