Dynamically add JAR to Gradle dependencies

dj_bushido

Currently, my build.gradle has a dependency on an external library built with Ant. To accomplish building the library, I followed the advice here and created a task which builds the external library, and copies it to the libs/ folder.

The task is called as part of a dependency:

build.gradle

dependencies {
    compile fileTree('libs') {
        include '*.jar'
        builtBy 'myTask'
    }
}

task myTask (type: GradleBuild) { GradleBuild antBuild ->
    antBuild.buildFile('external-stub.gradle')
    antBuild.tasks = ['clean', 'ivy.check', 'ivy.download', 'ivy.task', 'ivy',
    'init', 'mergeCode', 'compile', 'jar', 'copyJarsToProject']
}

However, when the compile actually runs, the library I just built and copied in is not included in the dependencies, as evidenced by a whole lot of compilation errors.

Am I including the library the wrong way?

The full build.gradle and associated files are over at Github, and I've linked to the specific commit I'm having issues with: Source Repository

dj_bushido

Alright, took me a while to get a build I was happy with. But, here's what was changed.

The actual build of the JAR was built using the same style, but moved to the external project (so that the main build project wasn't reaching across to it). I'll give an in-depth explanation below, but the commits are here and here. These are in order.

Basically, we export the jar as an artifact that other projects can depend on, rather than copying over the Jar ourselves. This way, the Ant build runs and other projects can see the Jar we just created. This is the end of the first commit. In the second commit, the task outputs are marked as needing to be regenerated only if the Jar does not exist. This was due to the fact that whenever I tried to build the app, it would take minutes to regen the Jar, and then have to repackage everything else as well. The code is below:

build.gradle External Project

configurations {
    buildJSword
}

task doBuildJSword (type: GradleBuild) {
buildFile = 'jsword-stub.gradle'
tasks = ['clean', 'ivy.check', 'ivy.download', 'ivy.task', 'ivy',
    'init', 'mergeCode', 'compile', 'jar'] //, 'copyJarsToMinimalBible']
    ext.outputJar = file('distribution/jsword.jar')
    outputs.upToDateWhen {
        ext.outputJar.exists()
    }
}

artifacts {
    buildJSword(doBuildJSword.ext.outputJar) {
        builtBy doBuildJSword
    }
}

Then, the main project just has to add this project as a compile-time dependency:

build.gradle Main Project

compile project(path: ':jsword-minimalbible', configuration: 'buildJSword')

Hope this is helpful for anyone with a similar issue, let me know if you have questions!

Note: The build currently does not clean itself properly, so if you change any code in the external project, you need to delete the external Jar for everything to regenerate itself correctly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically add JAR to Gradle dependencies

From Dev

Gradle: Add jar & dependencies to exporting jar

From Dev

Dependencies not showing up in jar with Gradle?

From Dev

Gradle shadow jar with conflicting dependencies

From Dev

Gradle compile dependencies not included in Jar

From Dev

Gradle - Shared project dependencies as jar

From Dev

Gradle compile dependencies not included in Jar

From Dev

Add jar to Gradle

From Dev

Add external jar in gradle

From Dev

Add to subproject to gradle jar

From Dev

Add dependencies in Eclipse using Gradle

From Dev

Unable to add new dependencies gradle

From Dev

Add dependencies in Eclipse using Gradle

From Dev

Gradle: Build 'fat jar' with Spring Boot Dependencies

From Dev

intellij build jar artifact containing gradle dependencies

From Dev

Gradle Dependencies aren't included in jar

From Dev

How to run Gradle JAR against local dependencies?

From Dev

gradle executable jar can't include local jar dependencies

From Dev

how to add a library to the dependencies gradle of LIBGDX project

From Dev

Is there a way to add classpath/directories for dependencies on Gradle?

From Dev

How to add programmatically dependencies to Gradle configuration?

From Dev

How to add Idea module to dependencies for gradle

From Dev

How to add Idea module to dependencies for gradle

From Dev

Add dependencies in Gradle file not auto import in MainActivity

From Dev

Gradle: how to add dependencies to the specific task?

From Java

Add gradle dependencies(build.gradle) in .aar file Android

From Java

How do I create an executable fat jar with Gradle with implementation dependencies

From Dev

In an Android Gradle build, how to exclude dependencies from an included jar file?

From Dev

Android Gradle Plugin: Create AAR with Jar Dependencies and Proguard

Related Related

  1. 1

    Dynamically add JAR to Gradle dependencies

  2. 2

    Gradle: Add jar & dependencies to exporting jar

  3. 3

    Dependencies not showing up in jar with Gradle?

  4. 4

    Gradle shadow jar with conflicting dependencies

  5. 5

    Gradle compile dependencies not included in Jar

  6. 6

    Gradle - Shared project dependencies as jar

  7. 7

    Gradle compile dependencies not included in Jar

  8. 8

    Add jar to Gradle

  9. 9

    Add external jar in gradle

  10. 10

    Add to subproject to gradle jar

  11. 11

    Add dependencies in Eclipse using Gradle

  12. 12

    Unable to add new dependencies gradle

  13. 13

    Add dependencies in Eclipse using Gradle

  14. 14

    Gradle: Build 'fat jar' with Spring Boot Dependencies

  15. 15

    intellij build jar artifact containing gradle dependencies

  16. 16

    Gradle Dependencies aren't included in jar

  17. 17

    How to run Gradle JAR against local dependencies?

  18. 18

    gradle executable jar can't include local jar dependencies

  19. 19

    how to add a library to the dependencies gradle of LIBGDX project

  20. 20

    Is there a way to add classpath/directories for dependencies on Gradle?

  21. 21

    How to add programmatically dependencies to Gradle configuration?

  22. 22

    How to add Idea module to dependencies for gradle

  23. 23

    How to add Idea module to dependencies for gradle

  24. 24

    Add dependencies in Gradle file not auto import in MainActivity

  25. 25

    Gradle: how to add dependencies to the specific task?

  26. 26

    Add gradle dependencies(build.gradle) in .aar file Android

  27. 27

    How do I create an executable fat jar with Gradle with implementation dependencies

  28. 28

    In an Android Gradle build, how to exclude dependencies from an included jar file?

  29. 29

    Android Gradle Plugin: Create AAR with Jar Dependencies and Proguard

HotTag

Archive