gpt4 book ai didi

java - 如何使用 Maven 程序集将类路径属性添加到可执行 jar

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

我正在使用以下插件来生成可执行 jar。我在我的类路径中有一个属性文件,我想将它添加到我的 jar,但是使用程序集插件我无法做到这一点。我找到了一些答案说我们可以使用 jar 插件创建一个 jar,然后使用 assembly 插件打包它们。任何人都可以帮助我如何做到这一点。

我的插件说明

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>mainclass</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
<includes>
<include>myconfig.propeties</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>app</finalName>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>
mainclass
</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>

我的 jar-with-dependeinceis 文件

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>bin</id>
<!-- Generates a zip package containing the needed files -->
<formats>
<format>zip</format>
</formats>

<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
<dependencySet>
<!--
Project artifact is not copied under library directory since
it is added to the root directory of the zip package.
-->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>

<fileSets>
<!--
Adds startup scripts to the root directory of zip package. The startup
scripts are located to src/main/scripts directory as stated by Maven
conventions.
-->
<fileSet>
<directory>${project.build.scriptSourceDirectory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>startup.*</include>
</includes>
</fileSet>
<!-- adds jar package to the root directory of zip package -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>

提前致谢。

最佳答案

我通常发现使用 shade 插件更容易。它将运行 jar 所需的所有依赖项捆绑到一个 jar 中,因此您不必修改类路径,一切都在 jar 中:

http://maven.apache.org/plugins/maven-shade-plugin/

示例用法:

        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<configuration>
<minimizeJar>true</minimizeJar>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>your.package.Main</mainClass>
</transformer>
</transformers>
<artifactSet>
<excludes>
<!-- maybe there is stuff you don't want in the jar -->
<exclude>log4j:log4j</exclude>
<exclude>org.slf4j:slf4j-api</exclude>
<exclude>org.slf4j:slf4j-log4j12</exclude>
</excludes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

关于java - 如何使用 Maven 程序集将类路径属性添加到可执行 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25540950/

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