创建具有依赖项的可执行 fat jar(gradle 或 maven)

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

我有一个非常简单的程序,它只生成一个通过预先确定的结果集填充的 JTable,它在 ide ide、(intelliJ) 中运行良好。它只有一个 sqlite 依赖项。

我正在尝试从中获取一个独立的 executable jar,它会输出相同的 table。

我在 gradle 上做了这个项目,因为这是查找脂肪罐时最常见的结果。

guides 根本不起作用,但我最终还是在这里结束了。

运行在终端上使用“gradle uberJar”确实生成了一个 jar,但是当双击并 运行在cmd 行产生:

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

这里是 gradle 构建文本:

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

然后我在 maven 上尝试了相同的项目,但不太成功

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

使用此处(和其他地方)的答案,运行命令行上的 ning mvn clean package 产生

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

我也不能 运行 intelliJ 终端上的命令。

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

诚然,我并没有真正做到这一点,我只是想从 idea 上的代码转换为我可以 运行 的程序,无论多么简单可能是。希望我可以应用它来创建一个依赖于更大程序的胖 Jar。

您可以向您的任务添加清单,因为它是 Jar 类型。使用 Main-Class 属性指定入口点应该使您的 Jar 可执行。

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