# JavaExec - classpath from plugin?

**URL:** <https://discuss.gradle.org/t/javaexec-classpath-from-plugin/38232>\
**Category:** Help/Discuss\
**Created:** [November 13, 2020, 6:11pm UTC](https://discuss.gradle.org/t/javaexec-classpath-from-plugin/38232 "2020-11-13T18:11:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mandrachek](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mandrachek/32/8931_2.png) [@mandrachek](https://discuss.gradle.org/u/mandrachek)\
**Post date:** [November 13, 2020, 6:11pm UTC](https://discuss.gradle.org/t/javaexec-classpath-from-plugin/38232/1 "2020-11-13T18:11:46Z")

</div>

I’m trying to create a task in a kotlin plugin that extends JavaExec. The main class is inside of my plugin. I can configure everything just fine except for the classpath - I need to add my plugin jar to the classpath of the the task I’m guessing for it to pick up on my main class, here’s my task creation:

```
    project.tasks.create("startApp", JavaExec::class.java) {
        it.group = "myapp"
        it.main = MyPluginMain.Companion::class.java.name
        val configString = Gson().toJson(config)
        it.args = listOf(configString)
    }

```

Any thoughts on how set the classpath would be appreciated. I suppose I could always break the main class out into a separate module, and add it to the project dependencies via the plugin, then resolve the jar, but I’d like to avoid that if at all possible.

---

<div class="post-metadata">

**Author:** ![mandrachek](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mandrachek/32/8931_2.png) [@mandrachek](https://discuss.gradle.org/u/mandrachek)\
**Post date:** [November 14, 2020, 7:26am UTC](https://discuss.gradle.org/t/javaexec-classpath-from-plugin/38232/2 "2020-11-14T07:26:42Z")

</div>

I went with creating a library with the main class, and setting the classpath from a configuration. It works, but it would still be nice to avoid it if possible.

---

<div class="post-metadata">

**Author:** ![jjustinic](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/jjustinic/32/9005_2.png) [@jjustinic](https://discuss.gradle.org/u/jjustinic)\
**Post date:** [November 15, 2020, 5:34am UTC](https://discuss.gradle.org/t/javaexec-classpath-from-plugin/38232/3 "2020-11-15T05:34:20Z")

</div>

You should be setting the classpath from a configuration, but there’s nothing forcing you to break out your main class into a separate library. That configuration can depend on your main plugin JAR same as it can depend on the separate module. You could also just reference the buildscript classpath configuration that already contains your plugin if it’s been applied.

---

<div class="post-metadata">

**Author:** ![mandrachek](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mandrachek/32/8931_2.png) [@mandrachek](https://discuss.gradle.org/u/mandrachek)\
**Post date:** [November 27, 2020, 9:13pm UTC](https://discuss.gradle.org/t/javaexec-classpath-from-plugin/38232/4 "2020-11-27T21:13:42Z")

</div>

@jjustinic - thanks, I didn’t realize I could get project.buildScript.configurations, that’s very helpful, and I got it working all from one package. But it only works if I use the old buildscript { dependencies { classpath … } } way of loading a plugin, and not if I use the newer plugins {} style.

Is there any way to get a configuration that includes my plugin when it’s applied via plugins {}?

---

<div class="post-metadata">

**Author:** ![mandrachek](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mandrachek/32/8931_2.png) [@mandrachek](https://discuss.gradle.org/u/mandrachek)\
**Post date:** [November 27, 2020, 9:58pm UTC](https://discuss.gradle.org/t/javaexec-classpath-from-plugin/38232/5 "2020-11-27T21:58:49Z")

</div>

I used [https://github.com/mfuerstenau/gradle-buildconfig-plugin](https://github.com/mfuerstenau/gradle-buildconfig-plugin) to give me a generated BuildConfig class with the version number in it that I can access, so I can use that to create my configuration containing just my plugin and dependencies. 😉 works good.
