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

时间:2021-06-12 20:47:14

标签: sqlite maven gradle intellij-idea fatjar

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

我正在尝试从中获取一个独立的可执行 jar,它会吐出同一个表。

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

指南根本不起作用,但我最终还是来到了这里。

Gradle fat jar does not contain libraries

在终端上运行“gradle uberJar”确实产生了一个 jar,但是当双击并在 cmd 行上运行 jar 时它不会运行:

<块引用>

无主要清单属性,在 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

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

有了这里(和其他地方)的答案,在命令行上运行 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>

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

1 个答案:

答案 0 :(得分:1)

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

depth