Is there a better way to deal with juggling a source dependency and a maven artifact dependency?


(Traun Leyden) #1

I have a project called stubworld-corelib-android, which depends on stubworld-java-core, and here is the build.gradle file for stubworld-corelib-android:

dependencies {
      compile 'com.android.support:appcompat-v7:+'
      // use source dependency
    compile project(':libraries:stubworld-corelib-java')
      // uncomment this to use maven artifact dependency
    // compile 'com.stubworld.core:CorelibJava:1.0'
  }

Normally when I’m developing, I always directly depend on the source of stubworld-corelib-java, because this facilitates easier debugging and experimenting.

However, when I build a maven artifact, I can’t depend on the source of stubworld-corelib-java, because otherwise it produces an artifact that has invalid dependencies. Instead, I need to comment the source dependency and uncomment the maven dependency:

dependencies {
      compile 'com.android.support:appcompat-v7:+'
      // uncomment to use source dependency
    // compile project(':libraries:stubworld-corelib-java')
      // use maven artifact dependency
    compile 'com.stubworld.core:CorelibJava:1.0'
  }

This works, but it is really awkward with regards to build automation, because I need to have some hacky script that conditionally includes the right dependency.

Is there any way to perform this level of indirection in gradle instead?


(Peter Niederwieser) #2

What do you mean by “hacky script” vs. “in Gradle”? There isn’t currently a built-in way to easily switch between project dependencies and external dependencies, but depending on your exact needs, you may be able to script it right in the build script.


(Traun Leyden) #3

What I mean by “hacky script” is a ruby script that I call with an argument --build-with-artifacts which then goes and rewrites the build.gradle files – eg, it does the same thing as manually commenting / uncommenting the lines as mentioned above.

After that script completes, then gradle is run and it uses the desired dependencies.

Basically looking for a cleaner way to do this so I can throw away that ruby script.


(Peter Niederwieser) #4

In the simplest case, you can do something like:

def buildWithArtifacts = System.getProperty("buildWithArtifacts")
  dependencies {
    ...
    compile buildWithArtifacts == null ?
         project(':libraries:stubworld-corelib-java') :
         'com.stubworld.core:CorelibJava:1.0'
}

Then run ‘gradle build -DbuildWithArtifacts’.

On the other hand, since you’ll also need to script ‘settings.gradle’ (not) to include the ‘stubworld-corelib-java’ project, you can just check if the project exists instead.

For a more complete proof-of-concept, see https://github.com/pniederw/elastic-deps.


(Traun Leyden) #5

Thanks! This is exactly what I was looking for.