gpt4 book ai didi

java - 如何使用包含依赖项子集的 Maven 生成可执行 jar?

转载 作者:行者123 更新时间:2023-11-30 11:19:31 25 4
gpt4 key购买 nike

我有一个具有多个依赖项的 Maven SWT 项目:依赖项 1、依赖项 2、依赖项 3、....、依赖项 n。

我想制作一个仅包含项目和依赖项 1 和依赖项 2 的可执行 jar,但其他依赖项将复制到/lib 目录中。

这是我的程序集配置:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>dependency1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.company</groupId>
<artifactId>dependency2</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<configuration>
<descriptors>
<descriptor>mvnDescript.xml</descriptor>
</descriptors>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${project.build.mainClass}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

和 mvnDescript.xml :

<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>false</unpack>
<includes>
<include>${artifact}</include>
</includes>
</dependencySet>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<unpack>false</unpack>
<excludes>
<exclude>${artifact}</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>

最佳答案

要创建这样的可执行 jar,您应该了解您将依赖项定义为 maven-assembly-plugin 的类路径的元素。这不是你想做的。您的项目应该改为定义这些依赖项。当然,您应该使用最新版本的插件。

  <dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>dependency1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.company</groupId>
<artifactId>dependency2</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${project.build.mainClass}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>jar-with-deps</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

关于java - 如何使用包含依赖项子集的 Maven 生成可执行 jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23180984/

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