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

ymzxtsji  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(261)

我有一个非常简单的程序,它只生成一个通过预定结果集填充的jtable,它在ide(intellij)中运行良好。它只有一个sqlite依赖项。
我正试图从中获得一个独立的可执行jar,它可以输出相同的表。
我在gradle上做了这个项目,因为这是查找胖jar时最常见的结果。
导游根本不起作用,但我最终还是来到了这里。
gradle fat jar不包含库
在终端上运行“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上尝试了同样的项目,但没有那么成功
如何使用intellij创建包含所有依赖项的jar文件
胖jar坏了。”没有主清单属性”。已尝试pom文件,但仍失败
有了这里(和其他地方)的答案,在命令行上运行mvnclean包可以产生

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

zvms9eto

zvms9eto1#

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

相关问题