Search code examples
sqlitemavengradleintellij-ideauberjar

Creating an executable fat jar with dependencies (gradle or maven)


I have a very simple program that just produces a JTable that is populated via a predetermined ResultSet, it works fine inside the ide, (intelliJ). It only has the one sqlite dependency.

I'm trying to get an standalone executable jar out of it that spits out the same table.

I did the project on gradle as that was the most common result when looking up fat jars.

The guides did not work at all but i did eventually end up on here.

Gradle fat jar does not contain libraries

running "gradle uberJar" on the terminal did produce a jar but it doesn't run when double clicked and running the jar on the cmd line produces:

no main manifest attribute, in dbtest-1.0-SNAPSHOT-uber.jar

here is the gradle build text:

plugins {
    id "java"
}

version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    implementation 'org.xerial:sqlite-jdbc:3.34.0'
}

test {
    useJUnitPlatform()
}

task uberJar(type: Jar) {
    archiveClassifier = 'uber'

    from sourceSets.main.output

    dependsOn configurations.runtimeClasspath
    from {
        configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
    }
}

I then tried the same project on maven but with less success

How to make jar file with all dependencies a.k.a. Fat jar with IntelliJ

Fat JAR not working. "No Main Manifest Attribute". Tried POM file, still failing

with with the answers from here (and other places), running mvn clean package on the command line yields

> [INFO] BUILD FAILURE [INFO]
> ------------------------------------------------------------------------ [INFO] Total time:  1.814 s [INFO] Finished at:
> 2021-06-12T14:35:07-06:00 [INFO]
> ------------------------------------------------------------------------ [ERROR] Failed to execute goal
> org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
> (default-compile) on project mvDB: Fatal error compiling: error:
> invalid target release: 16 -> [Help 1] [ERROR] [ERROR] To see the full
> stack trace of the errors, re-run Maven with the -e switch. [ERROR]
> Re-run Maven using the -X switch to enable full debug logging. [ERROR]
> [ERROR] For more information about the errors and possible solutions,
> please read the following articles: [ERROR] [Help 1]
> http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

i can't run the command on the terminal in intelliJ either.

here is the pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>mvDB</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.34.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>test.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Admittedly, I don't really much of this works, I'm only trying to get from a code working on the idea to a program that i can run, however simple it may be. And hopefully I can apply it for creating a fat Jar with dependencies on a larger program.


Solution

  • You can add a manifest to your task since it is type Jar. Specifying an entrypoint with the Main-Class attribute should make your Jar executable.

    task uberJar(type: Jar) {
        manifest {
          attributes 'Main-Class': 'your.main.class.goes.here'
        }
        archiveClassifier = 'uber'
    
        from sourceSets.main.output
    
        dependsOn configurations.runtimeClasspath
        from {
            configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
        }
    }