Search code examples
javaslf4jhikaricp

Including some dependencies in my compiled jar


Hello people of stackoverflow, on my working project I have 5 dependencies. I'm working on a project which doesn't contain any main method.

What I would like to do is including 2 (HikariCP and slf4j) out of my 5 dependencies in the final jar, but I don't figure how to do this, it's always adding all of them.

Edit: I'm using eclipse


Solution

  • Using Maven

    You can use the maven-shade-plugin to generate a fat-jar (or uber-jar). It will package all your dependencies inside the 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>
    

    Documentation related to the maven-shade-plugin can be found in here

    UPDATE: As you want to include just a few dependencies inside of it, you can check the Selecting contents for Uber JAR section:

    You can use the include or exclude tag to select which content will be provided to the jar, you can find some examples from the docs below:

    • Excluding

          <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>
      
    • Including and excluding

          <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>
      

    UPDATE 2: For a runnable jar file, you can follow this section of the documentation related to Executable Jars

    In Eclipse

    You can use the Package required libraries into generated JAR option, the downside of it is that you can't really select which dependencies do you want to include into to, since it is assessing all the required libs for you project.

    enter image description here

    I believe if you really want to remove some stuff, you would need to use Maven to package, or remove manually the dependencies you don't like from the generated jar, generating a custom jar with your own hands:

    enter image description here