Gradle:FatJar 不会包含一些罐子

时间:2021-04-10 10:01:24

标签: java gradle jar

我们有这样结构的项目

common - 模块 src -spring 引导 工人 - 模块

在工作模块中,我们有 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 丢失时。我试图在这部分 println {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 库中。所以我不确定它是否与那些罐子有关,或者我正在使用这个模块做一些关键的事情,也许有一些冲突

我尝试使用这个

 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 库

1 个答案:

答案 0 :(得分:0)

尝试包含所有 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

}
相关问题