在我编译的 jar 中包含一些依赖项
Including some dependencies in my compiled jar
大家好,Whosebug 的各位,在我的工作项目中,我有 5 个依赖项。
我正在做一个不包含任何主要方法的项目。
我想做的是在最终的 jar 中包含我的 5 个依赖项中的 2 个(HikariCP 和 slf4j),但我不知道该怎么做,它总是添加所有这些。
编辑:我正在使用 eclipse
使用 Maven
您可以使用 maven-shade-plugin
生成 fat-jar(或 uber-jar)。它会将所有依赖项打包到 jar 中:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>YOUR_JAR_FINAL_NAME</finalName>
</configuration>
</plugin>
</plugins>
</build>
与 maven-shade-plugin
can be found in here
相关的文档
更新: 由于您只想在其中包含一些依赖项,因此您可以查看 Selecting contents for Uber JAR 部分:
你可以使用include或exclude标签来select将哪些内容提供给jar,你可以从以下文档中找到一些示例:
排除
<configuration>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
包括和排除
<configuration>
<filters>
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>junit/framework/**</include>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
<exclude>org/junit/runners/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
更新 2: 对于可运行的 jar 文件,您可以关注 this section of the documentation 与 可执行 Jars
在 Eclipse 中
你可以使用 Package required libraries into generated JAR 选项,它的缺点是你不能真正 select 你想要包含哪些依赖项进入 to,因为它正在为您的项目评估所有必需的库。
我相信如果你真的想删除一些东西,你需要使用 Maven 来打包,或者从生成的 jar 中手动删除你不喜欢的依赖项,生成一个 custom jar with自己动手:
大家好,Whosebug 的各位,在我的工作项目中,我有 5 个依赖项。 我正在做一个不包含任何主要方法的项目。
我想做的是在最终的 jar 中包含我的 5 个依赖项中的 2 个(HikariCP 和 slf4j),但我不知道该怎么做,它总是添加所有这些。
编辑:我正在使用 eclipse
使用 Maven
您可以使用 maven-shade-plugin
生成 fat-jar(或 uber-jar)。它会将所有依赖项打包到 jar 中:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>YOUR_JAR_FINAL_NAME</finalName>
</configuration>
</plugin>
</plugins>
</build>
与 maven-shade-plugin
can be found in here
更新: 由于您只想在其中包含一些依赖项,因此您可以查看 Selecting contents for Uber JAR 部分:
你可以使用include或exclude标签来select将哪些内容提供给jar,你可以从以下文档中找到一些示例:
排除
<configuration> <artifactSet> <excludes> <exclude>classworlds:classworlds</exclude> <exclude>junit:junit</exclude> <exclude>jmock:*</exclude> <exclude>*:xml-apis</exclude> <exclude>org.apache.maven:lib:tests</exclude> <exclude>log4j:log4j:jar:</exclude> </excludes> </artifactSet> </configuration>
包括和排除
<configuration> <filters> <filter> <artifact>junit:junit</artifact> <includes> <include>junit/framework/**</include> <include>org/junit/**</include> </includes> <excludes> <exclude>org/junit/experimental/**</exclude> <exclude>org/junit/runners/**</exclude> </excludes> </filter> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration>
更新 2: 对于可运行的 jar 文件,您可以关注 this section of the documentation 与 可执行 Jars
在 Eclipse 中
你可以使用 Package required libraries into generated JAR 选项,它的缺点是你不能真正 select 你想要包含哪些依赖项进入 to,因为它正在为您的项目评估所有必需的库。
我相信如果你真的想删除一些东西,你需要使用 Maven 来打包,或者从生成的 jar 中手动删除你不喜欢的依赖项,生成一个 custom jar with自己动手: