在我的编译jar中包含一些依赖项

时间:2019-01-08 15:57:51

标签: java slf4j hikaricp

stackoverflow的大家好,在我的工作项目中,我有5个依赖项。 我正在一个不包含任何主要方法的项目中。

我想做的是在最终的jar中包含5个依赖项中的2个(HikariCP和slf4j),但是我不知道该怎么做,它总是将所有这些都添加了。

编辑:我正在使用Eclipse

1 个答案:

答案 0 :(得分:1)

使用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 标记选择将要提供给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文件,您可以遵循与{em>可执行jars

相关的this section of the documentation

在Eclipse中

您可以使用将所需的库打包到生成的JAR 选项中,它的缺点是您无法真正选择要包含到的依赖项,因为它正在评估所有项目所需的库。

enter image description here

我相信,如果您确实要删除某些东西,则需要使用Maven进行打包,或从生成的jar中手动删除不喜欢的依赖项,然后用自己的双手生成自定义jar < / em>:

enter image description here