创建一个具有依赖项(gradle或maven)的可执行胖罐子



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

我正试图从中获得一个独立的可执行jar,它吐出相同的表。

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

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

Gradle脂肪罐不包含库

运行";gradle uberJar";在终端上确实产生了一个jar,但双击时它没有运行,在cmd行上运行jar会产生:

在dbtest-1.0-SNAPSHOT-uber.jar 中没有主清单属性

这里是渐变构建文本:

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上尝试了同样的项目,但没有那么成功

如何制作具有所有依赖项的jar文件,也称为带有IntelliJ 的胖jar

胖JAR不工作"没有主清单属性";。已尝试POM文件,仍失败

有了来自这里(和其他地方(的答案,在命令行上运行mvn-clean包会产生

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

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

最新更新