Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort out situation for managed Java sources #1664

Open
jroper opened this issue Oct 12, 2014 · 6 comments
Open

Sort out situation for managed Java sources #1664

jroper opened this issue Oct 12, 2014 · 6 comments
Labels
documentation uncategorized Used for Waffle integration

Comments

@jroper
Copy link
Member

jroper commented Oct 12, 2014

The sbt documentation says:

A source generation task should generate sources in a subdirectory of sourceManaged

This works fine for generating Scala sources, but not generating Java sources. The reason is, the Java compiler needs a concept of a compilation root directory, and the one that sbt gives it for all sources in sourceManaged is sourceManaged, not sub directories of sourceManaged. Since the Java package structure must match the Java directory structure, this means in order for the Java generated source files to compile, their package structure needs to be directly under sourceManaged, not a sub directory of sourceManaged.

So this causes confusion, for example, sbt-idea follows the documentation, and treats sub directories of sourceManaged as source directories, which causes any Java files generated in sourceManaged to not compile from IntelliJ's perspective. See mpeltonen/sbt-idea#257. This causes a problem particularly with Play, see playframework/playframework#2941.

One possible solution is #753, but I think the documentation should simply be updated to say something like:

A source generation task should generate sources in sourceManaged, or if it wants to keep the source files it generates separate from other source generation tasks, it should generate sources to a sub directory in crossTarget and add that directory to managedSourceDirectories.

@jsuereth
Copy link
Member

This may be something for sbt/website, but let's look at the build itself, as I think we're weak in Java setup.

@theBlackDragon
Copy link

theBlackDragon commented Apr 14, 2016

sbt-cxf-wsdl2java has actually been following the documentation for generating Java sources and I haven't had any build problems in Sbt, things do break in IDEA when "correct" (as per the documentation) plugins get combined with ones that just generate their sources straight under sourceManaged.

The plugin does this though:
managedSourceDirectories in Compile += {(sourceManaged in Config).value}
which might not be ideal (or at least probably shouldn't need to be done by the plugin, judging from the discussions related to this subject)

@eed3si9n
Copy link
Member

eed3si9n commented Apr 20, 2016

In the hindsight, the idea that multiple plugins sharing the (sourceManaged in Compile).value to generate files in there was a bad one, given the problems we are facing with IDE integration and apparently Java compilation.

What do you guys think about adding some facility in the auto plugin that could generate sources safely?

package sbthello

import sbt._
import Keys._

object HelloPlugin extends AutoPlugin {
  override def requires = plugins.JvmPlugin
  override def managedDirectoryName = Some("hello")

  override lazy val projectSettings = Seq(
    sourceGenerators in Compile += Def.task {
      val file = pluginManagedSource(this, Compile).value / "Test.scala"
      IO.write(file, """object Test extends App { println("Hi") }""")
      Seq(file)
    }.taskValue
  )
}

@mkotsbak
Copy link

We should get a quick solution for this for short term and good solution for this in the long run. Currently the situation makes auto import of sbt project in IntelliJ almost broken.

@eed3si9n
Copy link
Member

The short-term workaround might be to recommend against using (sourceManaged in Compile).value altogether, and each plugin roll out their own directory under target, and append to managedSourceDirectories in Compile.

@benmccann
Copy link
Contributor

The solution @eed3si9n suggested seems to work well. I finally got around to trying it in sbt/sbt-xjc@4a0c67c. I've released a new version (0.10) of sbt-xjc with the fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation uncategorized Used for Waffle integration
Projects
None yet
Development

No branches or pull requests

6 participants