Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dependent jar is not bundled along with project jar Gradle

Tags:

gradle

jar

I have a project corehibernate and a project coregeneral. corehibernate is dependent on coregeneral. I need the jar file of coregeneral to be bundled along with the corehibernate jar. I tried various versions of the build.gradle thing, nothing worked.

I tried compile files("../coregeneral/build/libs/coregeneral.jar")

This version of fatJar too does not work.

apply plugin: 'java'

repositories {
    jcenter()
}

dependencies {
    compile (':coregeneral')
    testCompile 'junit:junit:4.12'
}

jar {
    baseName='corehibernate'
    from ('bin')
}

task fatJar(type: Jar, dependsOn: jar) {
  baseName = project.name + '-fat'
}
like image 823
Siddharth Avatar asked Nov 03 '16 08:11

Siddharth


People also ask

Where are Gradle dependency JARs?

Gradle declares dependencies on JAR files inside your project's module_name /libs/ directory (because Gradle reads paths relative to the build.gradle file). This declares a dependency on version 12.3 of the "app-magic" library, inside the "com.example.android" namespace group.

How do you define Gradle dependencies?

Gradle build script defines a process to build projects; each project contains some dependencies and some publications. Dependencies refer to the things that supports in building your project, such as required JAR file from other projects and external JARs like JDBC JAR or Eh-cache JAR in the class path.


1 Answers

There are two basic ways how to bundle projects together. The first would be to use application plugin which creates a zip with scripts that will also execute your application and bundle all jars by default. Second way is to use distribution plugin and define the final archive yourself (zip or tar).

Here is a sample project using the application plugin:

settings.gradle

rootProject.name = 'root'
include 'partone', 'parttwo'

build.gradle

subprojects {
    apply plugin: 'java'
}

partone/build.gradle - this one is empty

parttwo/build.gradle

apply plugin: 'application'

mainClassName = 'Hello'

dependencies {
    compile project (':partone')
}

Give that both projects actually have some content (classes), when you run gradle :projecttwo:build it will generate a zip file with executable scripts and both jars bundled inside.

If you prefer to use distribution plugin, change the parttwo/build.gradle to:

apply plugin: 'distribution'

distributions {
    main {
        contents {
            from jar
            from (project.configurations.runtime)
        }
    }
}

dependencies {
    compile project (':partone')
}

And again run gradle :parttwo:build. It will create a zip file that contains both jars.

like image 80
MartinTeeVarga Avatar answered Oct 05 '22 08:10

MartinTeeVarga