0

We have project that has this structure

common - module src -spring boot worker - module

in worker module we have build.gradle configured like this

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'
}

When I run task fatJar sqlserver:mssql-jdbc is missing. I tried to println in this part of the code from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } and "it" is never referenced to the sqlserver jdbc jar.

Since worker module include common module " compile project(':common') " i tried to put the dependency in the common module. When I do that, in console print out the sqlserver:mssql and when I go to the jar archive sqlserver:mssql jar is included, but problem is then when I run java -jar worker, the output is could not load the main class:...

Same thing also happens with Tika library. So I m not sure if it has something to do with those jars or I m doing something critical with using this modules and maybe there are some conflicts

I tried to use this

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

What happend is that my jar cannot find main class, but when i go into the jar archive. I go to my com.worker... I see there are two EntryPoint.class files.

So in short when i dont use those two lines of code, my jar can start, it has one EntryPoint.class file, but then is missing jdbc jar library

Filip Cacic
  • 77
  • 1
  • 7

1 Answers1

0

Try to include all compileClasspath and runtimeClasspath.

A task like this:

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

}
Mahdi Yusefi
  • 441
  • 4
  • 13
  • it is included, but when i tried to run a jar it says Error: Could not find or load main class com.worker.worker.EntryPoint – Filip Cacic Apr 10 '21 at 11:35
  • @FilipCacic Take a look at this https://stackoverflow.com/a/51456080/5005756 maybe this solves your issue – Mahdi Yusefi Apr 10 '21 at 12:56