我正在尝试从我的 Maven 项目创建一个 jar,其中包含特定文件夹中依赖项的 jar 文件。
我正在为其编写插件的框架需要将外部依赖项作为插件 jar 中的 jar 文件提供。
例如:
xxxx.jar
/myapp/myjavaclasses
/lib/externalDependencies.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>AAAAA</groupId>
<artifactId>BBBBBBB</artifactId>
<packaging>jar</packaging>
<version>1.1</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Rundeck-Plugin-Classnames>CCCCC</Rundeck-Plugin-Classnames>
<Rundeck-Plugin-Version>1.1</Rundeck-Plugin-Version>
<Rundeck-Plugin-Archive>true</Rundeck-Plugin-Archive>
<Rundeck-Plugin-File-Version>${project.version}</Rundeck-Plugin-File-Version>
<Rundeck-Plugin-Libs>lib/httpclient-4.5.8.jar lib/httpcore-4.4.11.jar</Rundeck-Plugin-Libs>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
在上面的pom文件中,我们有依赖项
org.apache.httpcomponents
,该依赖包含以下jar
httpclient-4.5.8.jar httpcore-4.4.11.jar
这些 jar 需要导入到我的最终 jar 文件夹/lib 下。
最佳答案
感谢 @ernest_k 找到了一个解决方案
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
和
<resources>
<resource>
<directory>.</directory>
<includes>
<include>lib/**/*.jar</include>
</includes>
</resource>
</resources>
关于java - Maven 包将依赖项中的 jar 添加到 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55654789/