最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Grade not able to create FAT jar but maven works - Stack Overflow

programmeradmin0浏览0评论

I used gradle to try to create a fat jar from this answer

How do I create an executable fat JAR with Gradle with implementation dependencies?

tasks.jar {
manifest.attributes["Main-Class"] = "com.example.MyMainClass"
val dependencies = configurations
    .runtimeClasspath
    .get()
    .map(::zipTree) // OR .map { zipTree(it) }
from(dependencies)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

and getting this error

Execution failed for task ':customFatJar'.

Cannot expand ZIP '/Users/user1/.gradle/caches/modules-2/files-2.1/org.apache.curator/apache-curator/2.12.0/b1ff6ce0741facb15217ae9254ee7167bc7b15e/apache-curator-2.12.0.pom'.

  • Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. Get more help at . BUILD FAILED in 31s 2 actionable tasks: 1 executed, 1 up-to-date

I then tried the same far jar but with gradle groovy script from and getting the same error

the full gradle groovy code is

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.10.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    implementation("org.neo4j.driver:neo4j-java-driver:5.27.0")
    implementation("org.mongodb:mongodb-driver-sync:4.8.2")
    implementation("org.mongodb:mongodb-driver:3.12.7")
    implementation( "co.elastic.clients:elasticsearch-java:8.12.0")
    implementation( "org.elasticsearch.client:elasticsearch-rest-high-level-client:7.17.16")
    implementation( "org.elasticsearch.client:transport:7.17.17")
    implementation( "com.datastax.oss:java-driver-core:4.15.0")
    implementation( "commons-logging:commons-logging:1.1.3")
    implementation( "org.apache.hive:hive-jdbc:3.1.2")
    implementation( "com.fasterxml.jackson.core:jackson-annotations:2.18.1")
    implementation( "com.fasterxml.jackson.core:jackson-core:2.18.1")
}

jar {
    manifest {
        attributes "Main-Class": "com.baeldung.fatjar.Application"
    }

    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

task customFatJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'com.baeldung.fatjar.Application'
    }
    archiveBaseName = 'all-in-one-jar'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

same gradle kotlin code that failed

plugins {
    id("java")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.10.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    implementation("org.neo4j.driver:neo4j-java-driver:5.27.0")
    implementation("org.mongodb:mongodb-driver-sync:4.8.2")
    implementation("org.mongodb:mongodb-driver:3.12.7")
    implementation( "co.elastic.clients:elasticsearch-java:8.12.0")
    implementation( "org.elasticsearch.client:elasticsearch-rest-high-level-client:7.17.16")
    implementation( "org.elasticsearch.client:transport:7.17.17")
    implementation( "com.datastax.oss:java-driver-core:4.15.0")
    implementation( "commons-logging:commons-logging:1.1.3")
    implementation( "org.apache.hive:hive-jdbc:3.1.2")
    implementation( "com.fasterxml.jackson.core:jackson-annotations:2.18.1")
    implementation( "com.fasterxml.jackson.core:jackson-core:2.18.1")
}

tasks.jar {
    manifest.attributes["Main-Class"] = "com.example.MyMainClass"
    val dependencies = configurations
        .runtimeClasspath
        .get()
//        .map(::zipTree) // OR .map { zipTree(it) }
        .map { zipTree(it)}
            from(dependencies)
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

tasks.test {
    useJUnitPlatform()
}

the same maven plugin does work for creating the far jar

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest> <mainClass>org.example</mainClass></manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
发布评论

评论列表(0)

  1. 暂无评论