# How to resolve dependencies in execution phase?

**URL:** <https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725>\
**Category:** Help/Discuss\
**Created:** [August 14, 2017, 3:52pm UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725 "2017-08-14T15:52:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![johny123](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@johny123](https://discuss.gradle.org/u/johny123)\
**Post date:** [August 14, 2017, 3:52pm UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/1 "2017-08-14T15:52:35Z")

</div>

Hi, I’ve seen multiple times recommendation to avoid resolving dependencies at the configuration phase. And I understand why. It slows down my build considerably. But I struggle to be able to resolve any dependencies at execution time.

Consider this for example, in one of my tasks I do at configuration phase:

```
myTask(type: JavaExec) {
   ...
   def myClassPath = files(project.configurations.runtime.getFiles())
   ...
}

```

This of course causes dependency resolution to kick in. So I thought I can move it to execution phase with something like this:

```
myTask(type: JavaExec) { 
  ...
  def getClassPath = {files(project.configurations.runtime.getFiles())} 
  doLast {
     def myClassPath = getClassPath()
     ...
  } 
}

```

This does not trigger resolution at config time anymore but since it is task of type JavaExec it is failing to run with “no main class specified” as configuration isn’t ready at execution time.

And more generally, is there any documentation regarding recommendations around what should be done during configuration vs execution phase, and how? There’s some documentation explaining the lifecycle but would be good to have some more around good practices…

Thanks!

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [August 14, 2017, 4:16pm UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/2 "2017-08-14T16:16:43Z")

</div>

Just don’t call getFiles. A Configuration already is a file collection.

myClasspath = configurations.runtime

---

<div class="post-metadata">

**Author:** ![johny123](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@johny123](https://discuss.gradle.org/u/johny123)\
**Post date:** [August 18, 2017, 10:08am UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/3 "2017-08-18T10:08:40Z")

</div>

Thanks Stefan. Is there a similar way to resolve classpath for all subprojects in a multiproject setup? I’m looking for a way to concatenate all subproject’s classpaths as they are needed for this JavaExec task.

Tried this but getting “File collection does not allow modification”

```
myTask(type: JavaExec) { 
  ...
  mySubProjects().each{ classpath.add(it.configurations.runtime) }
  ... 
}

```

I also tried creating empty collection and adding file collections to it. Same problem…

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [August 18, 2017, 10:38am UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/4 "2017-08-18T10:38:41Z")

</div>

```gradle
configurations {
   javaExec
} 
mySubProjects().each {
   configurations.javaExec.extendsFrom it.configurations.runtime 
} 
myTask(type: JavaExec) {
   classpath = configurations.javaExec
   ... 
} 

```

---

<div class="post-metadata">

**Author:** ![johny123](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@johny123](https://discuss.gradle.org/u/johny123)\
**Post date:** [August 18, 2017, 10:53am UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/5 "2017-08-18T10:53:54Z")

</div>

Thanks Lance! I found another way, which also seems to be working, with

`mySubProjects().each{ classpath = classpath.plus(it.configurations.runtime) }`

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [August 18, 2017, 11:13am UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/6 "2017-08-18T11:13:00Z")

</div>

I was using a `Configuration` so that proper dependency resolution would take place

---

<div class="post-metadata">

**Author:** ![johny123](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@johny123](https://discuss.gradle.org/u/johny123)\
**Post date:** [August 18, 2017, 1:56pm UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/7 "2017-08-18T13:56:45Z")

</div>

How is that different to my approach?

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [August 18, 2017, 2:56pm UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/8 "2017-08-18T14:56:22Z")

</div>

You are just adding to a file collection, if for instance two projects had different versions of log4j then your classpath would have both log4j jars. Mine would use dependency resolution to choose one

---

<div class="post-metadata">

**Author:** ![johny123](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@johny123](https://discuss.gradle.org/u/johny123)\
**Post date:** [August 18, 2017, 3:40pm UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/9 "2017-08-18T15:40:56Z")

</div>

I see. That makes sense! And perhaps explains why I’m seeing some errors during runtime with my solution.

So I’m trying yours by adding it to my subprojects task

```
configure(mySubProjects()){
  ...   
  configurations {
       javaExec
    } 
    configurations.javaExec.extendsFrom it.configurations.runtime 
   ...
}

```

and then in myTask

```
task myTask(type: JavaExec) {
    ...
    classpath = configurations.javaExec
    ...
}

```

Which fails when I run it from the root project

`gradle myTask`

with  
`> Could not find property 'javaExec' on configuration container.`

As I mentioned I’m trying…

> [@johny123](#):
>
> to resolve classpath for all subprojects in a multiproject setup? I’m looking for a way to concatenate all subproject’s classpaths as they are needed for this JavaExec task

Possibly I could add it on the root project too, in build.gradle:

```
configure(project) {
    configurations {
       javaExec
    } 
    configurations.javaExec.extendsFrom project.configurations.runtime 
}

```

Although I wish I didn’t have to do that (which actually relates to my other question:  
[does-root-in-multiproject-needs-to-be-configured](https://discuss.gradle.org/t/does-root-in-multiproject-needs-to-be-configured/23783))  
it makes the error _“task not found”_ go away,

But then I’m getting _“Error: Could not find or load main class”_  
as your solution only includes external dependencies. And not my internal subprojects.

jars like log4j etc are there, but not my subprojectx.jar etc.

How could I include those too?

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [August 18, 2017, 4:02pm UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/10 "2017-08-18T16:02:57Z")

</div>

Your code is adding a “javaExec” configuration to all of the subprojects and not the root project.

Mine is creating a “javaExec” configuration in the root project.

Quite different

---

<div class="post-metadata">

**Author:** ![johny123](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@johny123](https://discuss.gradle.org/u/johny123)\
**Post date:** [August 18, 2017, 4:27pm UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/11 "2017-08-18T16:27:50Z")

</div>

If I put it the way you did above I’m getting

`> Could not find property 'runtime' on configuration container.`

If I put it only in root project like below

```
configure(project) { 
   ...
    configurations {
        javaExec
    }    
}

```

And then in mySubProjects

```
configure(mySubProjects()){
    configurations.javaExec.extendsFrom it.configurations.runtime 
}

```

I am getting:

`> Could not find property 'javaExec' on configuration container.`

The only way I made this whole thing work was resolving dependencies at configuration time, and using getFiles. Which Stefan suggested I didn’t have to…

```
task myTask(type: JavaExec) { 
    ... 
    def myClassPath = files(project.configurations.runtime.getFiles());
    mySubProjects().each{ myClassPath += it.jar.outputs.getFiles() }
    classpath = myClassPath
    ...
}

```

Then I can run this task from root project and it will work.  
But it is both ugly and slow.  
Any more ideas?

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [August 18, 2017, 5:29pm UTC](https://discuss.gradle.org/t/how-to-resolve-dependencies-in-execution-phase/23725/12 "2017-08-18T17:29:05Z")

</div>

```gradle
configure(mySubProjects()){
    configurations.javaExec.extendsFrom it.configurations.runtime 
}

```

This is wrong, `it.configurations` and `configurations` are the same thing in this closure. Getters in the closure are invoked on the subproject. This is different from my `mySubProjects().each { ... }` suggestion

You could probably just do it like this in the root project

```gradle
def mySubProjects = [':foo', ':bar'] 
configurations {
   javaExec 
} 
dependencies {
   mySubProjects.each {
      javaExec project(path: it, configuration: 'runtime') 
   } 
} 
task myTask(type: JavaExec) {
   classpath = configurations.javaExec 
} 

```
