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

Java 在我编译的jar中包含一些依赖项,java,slf4j,hikaricp,Java,Slf4j,Hikaricp,stackoverflow的人们好,在我的工作项目中,我有5个依赖项。 我正在做一个不包含任何主方法的项目 我想做的是在最后一个jar中包含5个依赖项中的2个(HikariCP和slf4j),但我不知道怎么做,它总是添加所有依赖项 编辑:我正在使用eclipse和Maven 您可以使用maven shade插件生成一个胖罐子(或uber罐子)。它会将您的所有依赖项打包到jar中: <build> <plugins> <plugin>

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

我想做的是在最后一个jar中包含5个依赖项中的2个(HikariCP和slf4j),但我不知道怎么做,它总是添加所有依赖项

编辑:我正在使用eclipse

和Maven 您可以使用
maven shade插件
生成一个胖罐子(或uber罐子)。它会将您的所有依赖项打包到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>
  • 包括和不包括

        <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>
    
    
    与可执行JAR相关

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

    我相信如果你真的想删除一些东西,你需要使用Maven来打包,或者手动从生成的jar中删除你不喜欢的依赖项,用你自己的双手生成一个定制的jar:


    你在使用maven吗?不,我没有,但如果这是我唯一的选择,它会只包含我想要的罐子而不是全部吗?你可以使用
    过滤器
    包含
    排除
    标记相结合,编辑我的答案并提供更多细节。顺便说一下,我看到你没有使用maven,你在用什么工具?我没有用任何工具。仅eclise和导出选项。我应该使用一些吗?让我用eclipse版本编辑我的答案。给我几分钟
    
        <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>