[英]How to add classpath to maven
I want to create an executable jar with dependencies that are going to be outside the archive in folder lib.我想创建一个可执行 jar,其依赖项将位于 lib 文件夹中的存档之外。 I found out that i can ask maven to copy dependencies into the folder and modify manifest file in my executable to know where to look for needed classes.
我发现我可以要求 maven 将依赖项复制到文件夹中并修改我的可执行文件中的清单文件,以知道在哪里寻找所需的类。 The problem is that it does not add any classpaths to my manifest.
问题是它没有向我的清单添加任何类路径。 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>org.sav</groupId>
<artifactId>console-app-01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>org.sav.app.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.kenai.nbpwr</groupId>
<artifactId>joda-time</artifactId>
<version>1.6.2-201110292322</version>
</dependency>
</dependencies>
</project>
After running mvn install my manifest looks like this:运行 mvn install 后,我的清单如下所示:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.3
Built-By: abigo
Build-Jdk: 11.0.12
Main-Class: org.sav.app.App
But it won't run.但它不会运行。 It tells me that it cannot find needed classes.
它告诉我它找不到需要的类。 As soon as i add class-path manually it runs with no problems
一旦我手动添加类路径,它就可以毫无问题地运行
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven 3.8.3
Built-By: abigo
Build-Jdk: 11.0.12
Main-Class: org.sav.app.App
Class-Path: lib/joda-time-1.6.2.jar lib/joda-time-1.6.2-201110292322.jar
Application:应用:
package org.sav.app;
import org.joda.time.LocalTime;
public class App {
public static void main(String[] args) {
LocalTime currentTime = new LocalTime();
System.out.printf("Current time is: %s%n", currentTime);
}
}
To avoid that other people waste as much time as I did... I had a similar problem, where the jar that was built with openjdk 11 (worked fine with 8) and was included in a Docker image.为了避免其他人像我一样浪费时间......我遇到了类似的问题,使用 openjdk 11 构建的 jar(与 8 一起工作正常)并包含在 Docker 映像中。 The resulting image had the above issue with the embeded jar (outputting
ClassNotFoundException
for classes in jars that should have been on the classpath).生成的图像在嵌入的 jar 中存在上述问题(为应该在类路径上的 jar 中的类输出
ClassNotFoundException
)。 The solution included two steps:该解决方案包括两个步骤:
verify
phase [2]. verify
阶段执行 [2]。 The reason behind this was that the image was being built before the jar manifest was updated somehow.... After these 2 changes, the resulting image had a jar with the correct manifest in it, that included the jars in the lib
folder.在这 2 次更改之后,生成的图像有一个包含正确清单的 jar,其中包括
lib
文件夹中的 jar。
Code snippets:代码片段:
[1] [1]
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>3.2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>someclass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
[2] [2]
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<phase>verify</phase>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
...
</configuration>
</plugin>
I'n in the same trouble now, and I can't find other workaround than downgrading maven version.我现在遇到了同样的麻烦,除了降级 maven 版本之外,我找不到其他解决方法。 I'm using maven 3.8.3, and I will download 3.6
我正在使用maven 3.8.3,我将下载3.6
使用旧版本的 Maven,一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.