sbtclasspath

Cannot find class in same jar during SBT build


I am trying to build an sbt plugin like swagger2markup, but I face an issue when executing this in sbt:

org.apache.commons.configuration2.ex.ConfigurationRuntimeException: java.lang.ClassNotFoundException: org.apache.commons.configuration2.PropertiesConfiguration
    at org.apache.commons.configuration2.beanutils.BeanHelper.fetchBeanClass(BeanHelper.java:549)
..snip..
Caused by: java.lang.ClassNotFoundException: org.apache.commons.configuration2.PropertiesConfiguration
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.apache.commons.lang3.ClassUtils.getClass(ClassUtils.java:909)

So, trying to lookup the PropertiesConfiguration from a class within the same jar is failing. It seems that sbt runs tasks in a classloader that does not include dependencies. This happens when I run the task from the sbt console.


Solution

  • The workaround I managed in the end, was to invoke the task in a forked jvm process:

    val runner: ScalaRun = initScoped(myTask.scopedKey, Defaults.runnerInit).value
    val log: Logger = streams.value.log
    val arguments = Seq()
    runner.run(mainClass, classpath.map(_.data), arguments, log)
    

    This worked for me when there was a main class available in the tool I was using.