# Need dependency before compile task

**URL:** <https://discuss.gradle.org/t/need-dependency-before-compile-task/27806>\
**Category:** Help/Discuss\
**Created:** [July 19, 2018, 12:42pm UTC](https://discuss.gradle.org/t/need-dependency-before-compile-task/27806 "2018-07-19T12:42:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthewadams](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/matthewadams/32/7010_2.png) [@matthewadams](https://discuss.gradle.org/u/matthewadams)\
**Post date:** [July 19, 2018, 12:42pm UTC](https://discuss.gradle.org/t/need-dependency-before-compile-task/27806/1 "2018-07-19T12:42:09Z")

</div>

Hi all,

I have a code generation task that extends `type: Exec` and needs a maven dependency to be downloaded before it can run, and the code generation task must run before my `compileJava` task.

What would be a good way to force the dependency to be downloaded to my `~/.m2/repository` before my code generation task executes? There doesn’t appear to be a `dependencies` scope that I can find that will make that happen, despite trying `compileOnly` and `annotationProcessor`.

Thanks!

---

<div class="post-metadata">

**Author:** ![daggerok](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daggerok/32/7757_2.png) [@daggerok](https://discuss.gradle.org/u/daggerok)\
**Post date:** [July 21, 2018, 11:33am UTC](https://discuss.gradle.org/t/need-dependency-before-compile-task/27806/2 "2018-07-21T11:33:52Z")

</div>

not sure about gradle configuration you are looking for, but as workaround, try this:

```bash
# downloading dependencies excluding `yourExecTask` execution
./gradlew assemble -x yourExecTask

# after dependencies where cached in mavenLocal()
./gradlew yourExecTask

```

* * *

Regards,  
Maksim

---

<div class="post-metadata">

**Author:** ![matthewadams](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/matthewadams/32/7010_2.png) [@matthewadams](https://discuss.gradle.org/u/matthewadams)\
**Post date:** [July 21, 2018, 12:16pm UTC](https://discuss.gradle.org/t/need-dependency-before-compile-task/27806/3 "2018-07-21T12:16:08Z")

</div>

Thanks @daggerok, but I’d like for the solution to be transparent to the other developers. I suppose I could add another task that’s also of `type: Exec` that invokes `gradlew` with the commands you gave.

Still, it seems like there ought to be a way in gradle to force the download of Maven dependencies.

---

<div class="post-metadata">

**Author:** ![daggerok](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/daggerok/32/7757_2.png) [@daggerok](https://discuss.gradle.org/u/daggerok)\
**Post date:** [July 21, 2018, 12:31pm UTC](https://discuss.gradle.org/t/need-dependency-before-compile-task/27806/4 "2018-07-21T12:31:18Z")

</div>

1. create a library, where your code generation task will be bundle jar library file with all generated sources
2. in that shared-library project add dependencies closure with all needed transitive dependencies
3. in any other projects uses your library just use regular dependencies closure to depends on that project

settings.gradle

```gradle
root.projectName = 'parent'
include 'shared-library'
include 'app-1'
include 'app-2'
// include more subprojects...

```

in `shared-library/build.gradle`:

```gradle
apply plugin: 'java-library' // or apply plugin: 'java' // it depends..

dependencies {
  // add here needed dependencies...
}

task myExecTask(type Exec) {
  // generate sources...
}

jar.dependsOn myExecTask
jar.shouldRunAfter myExecTask

```

in `app-1/build.gradle` and `app-2/build.gradle`:

```gradle
dependencies {
  compile(project(':shared-library'))
  // other deps...
}

```

Also I would recommend you look at composite builds gradle feature: [https://docs.gradle.org/current/userguide/composite\_builds.html](https://docs.gradle.org/current/userguide/composite_builds.html)  
maybe it’s exact what you are looking for…

* * *

Regards,  
Maksim Kostormin
