【问题标题】:Creating an executable fat jar with dependencies (gradle or maven)创建具有依赖项(gradle 或 maven)的可执行 fat jar
【发布时间】:2021-06-12 20:47:14
【问题描述】:

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

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

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

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

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。

【问题讨论】:

  • 你试过this plugin,我用这个来装我的肥罐。
  • @braebdeb 我做了,它产生了与 uberJar 任务相同的 ~7mb jar 文件。双击时也无法运行,从命令行运行时出现“无主清单属性”错误

标签: sqlite maven gradle intellij-idea fatjar


【解决方案1】:

您可以将清单添加到您的任务中,因为它是 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) }
    }
}

【讨论】:

  • 我正面临这个问题root@9777b290fded:/home/gradle/checkout-service# java -cp build/libs/checkout-service-1.0-SNAPSHOT-uber.jar com.noths.runner.Runner Error: Could not find or load main class com.noths.runner.Runner Caused by: java.lang.ClassNotFoundException: com.noths.runner.Runner
猜你喜欢
  • 2021-05-25
  • 2021-09-04
相关资源
最近更新 更多