【问题标题】:Gradle:FatJar wont include some jarsGradle:FatJar 不会包含一些罐子
【发布时间】:2021-07-06 00:56:03
【问题描述】:

我们有具有这种结构的项目

common - 模块 src - 弹簧启动 工人 - 模块

在工作模块中,我们有这样配置的 build.gradle

plugins {
id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'


repositories {
  mavenCentral()
}



task fatJar(type: Jar) {
  manifest {
    attributes 'Implementation-Title': 'Gradle Jar File Example',
            'Implementation-Version': version,
            'Main-Class': 'com.worker.worker.EntryPoint'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
destinationDir(new File(projectDir.parent,"\\libs\\worker\\"))
with jar
}

dependencies {
  compile project(':jTransfer')
  compile project(':datamasking')
  compile project(':common')
  implementation 'com.google.code.gson:gson:2.8.6'
  implementation 'mysql:mysql-connector-java:8.0.19'
  implementation 'com.microsoft.sqlserver:mssql-jdbc'
  implementation 'com.microsoft.sqlserver:mssql-jdbc:8.4.1.jre8'
  testCompile group: 'junit', name: 'junit', version: '4.12'
}

当我运行任务 fatJar sqlserver:mssql-jdbc 时丢失。我试图在这部分打印 来自 {configuration.compile.collect { it.isDirectory() 的代码? it : zipTree(it) } } 和 "it" 从未被引用到 sqlserver jdbc jar。

由于工作模块包含公共模块“compile project(':common')”,我试图将依赖项放在公共模块中。当我这样做时,在控制台中打印出 sqlserver:mssql 并且当我进入 jar 存档 sqlserver:mssql jar 时,问题是当我运行 java -jar worker 时,输出无法加载主类: ...

同样的事情也发生在 Tika 库中。所以我不确定它是否与这些 jars 有关,或者我正在使用这些模块做一些关键的事情,也许有一些冲突

我试过用这个

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

发生的事情是我的 jar 找不到主类,但是当我进入 jar 存档时。我去我的 com.worker... 我看到有两个 EntryPoint.class 文件。

所以简而言之,当我不使用这两行代码时,我的 jar 可以启动,它有一个 EntryPoint.class 文件,但是缺少 jdbc jar 库

【问题讨论】:

    标签: java gradle jar


    【解决方案1】:

    尽量包含所有的compileClasspath和runtimeClasspath。

    这样的任务:

    task fatJar(type: Jar) {
        zip64 = true
        manifest {
            attributes 'Main-Class': 'your.main.class'
        }
        archiveBaseName.set("Name")
        archiveVersion.set(versionInt)
        from {
            configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
        with jar
    
    }
    

    【讨论】:

    • 它包含在内,但是当我尝试运行一个 jar 时,它显示错误:无法找到或加载主类 com.worker.worker.EntryPoint
    • @FilipCacic 看看这个stackoverflow.com/a/51456080/5005756也许这能解决你的问题
    猜你喜欢
    • 1970-01-01
    • 2015-06-27
    • 2018-04-12
    • 2013-05-08
    • 2012-06-06
    • 2011-07-19
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    相关资源
    最近更新 更多