Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top