debug sbt sources too with eclipse - scala

I'd like to navigate to the sbt sources when I debug my sbt build.
By using the sbt eclipse plugin I can debug e.g. my Build.scala file, which is great! And I can include the sbt plugin sources too by using:
> reload plugins
> eclipse withSources=true
But the sources to sbt itself aren't in the .classpath file generated by sbt-eclipse. I see that the source jars are in the ivy cache, but the sourcepath entry is missing:
<classpathentry kind="lib"
path="/home/lee/.ivy2/cache/org.scala-sbt/main-settings/jars/main-settings-0.13.1-RC2.jar"
/>
I'd like the generated class path to have a sourcepath like the following. (This was edited manually, I'd like to have it generated by sbt-eclipse):
<classpathentry kind="lib"
path="/home/lee/.ivy2/cache/org.scala-sbt/main-settings/jars/main-settings-0.13.1-RC2.jar"
sourcepath="/home/lee/.ivy2/cache/org.scala-sbt/main-settings/srcs/main-settings-0.13.1-RC2-sources.jar"
/>
(Tested with sbt 13.1-RC2 and sbt eclipse 2.4.0)
I see this sbt-dependency-manager plugin, but that looks awkward...
Is there an easier way to view sbt sources along with my sbt build?

I am, as originator of sbt-dependency-manager, must admit that there is not only problem to get sources. You may download sources by hands and manually attach them to your debugger, but Eclipse still unable to use them.
sbt-dependency-manager not only download, but also repackage jars. So in this way you should open 2 bugs :-):
about fetch sources in sbt-eclipse
about sources layout of SBT itself
I tried to avoid this issues and this is only solution that I know. Oh, you may switch to Idea or Ensime, of course - but this is not acceptable for me. Also I don't use sbt-eclipse at all because sbt-dependency-manager makes it useless.

Related

Inside IDE(IntelliJ), could not find sbt dependies from External Libraries

I am starting a Scala project and I'm using SBT and Intellij 2020.2.3 as my IDE.
I have the following build.sbt file from the project, but I can't seem to get the dependencies in the Idea IDE "External Libraries" section to show up in "libraryDependencies" of SBT after running "sbt update".
The Idea version is that:
The Idea External Libraies are that:
The Sbt libraryDependencies are that:
In my experience , Intellij is very slow to pickup on dependencies and display them.
Bare in mind this is not a reflection on sbt , and asides for the annoyance, you should still be able to sbt compile from the console.
That is the first thing I suggest you test. If you can compile , that means the dependencies were downloaded, are available on the classpath, and its just a matter of getting Intellij to recognize that
You can try one of 2 things, hover over the dependencies in your build.sbt file and you might see a refresh project option , or you can go to module section in project settings and reimport

Intellij sbt won't resolve dependencies' dependencies

I have a Intellij Scala project, that depends on another lib, which in turn depends on some other libs (gson, log4j...), but somehow Intellij won't add those transitive dependencies to my project, so when I run my main method, jvm always complains no class found. I also tried sbt update and sbt update-classifier, they won't resolve down the dependency path as well. How can I have sbt resolve all levels of dependencies?
If all works in SBT command lines but not in Idea then remove the .idea folder and reload the SBT project in Idea.
I should warn that you will loose the project configs (such as run configs) by doing that.
I finally figure out what was wrong, it was the sbt-s3-resolver plugin I was using couldn't resolve maven style dependencies, I used an another plugin which supports maven libs and it worked. But I still couldn't figure out why it worked before.

How can I set in build.gradle the groovy nature of an Eclipse project?

After running gradle cleanEclipse Eclipse on the project it loses the Groovy nature. How can I set this nature automatically, or simply to say to the Gradle to leave it alone.
Edit:
According do doc, I can write in the build.gradle:
eclipse {
project {
natures 'some.extra.eclipse.nature', 'some.another.interesting.nature'
}
}
But what is the name of the groovy nature, or how could I get it?
I go to the .project and look:
<natures>
<nature>org.eclipse.jdt.groovy.core.groovyNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>bndtools.core.bndnature</nature>
</natures>
org.eclipse.jdt.groovy.core.groovyNature - that is the nature name
And I am adding apply plugin: "groovy", as #Peter Niederwieser advised (thanks+1)
But
After running gradle cleanEclipse Eclipse I have correct .project file, and the project has "G" on the icon, but in the .classpath two lines are not present:
<classpathentry exported="true" kind="con" path="GROOVY_SUPPORT"/>
<classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/>
That ARE present, when I am setting the Groovy nature by hand.
And it seems, that these lines are important, for in that variant the project shows errors even on compile level - it doesn't see some Groovy methods.
Thinking about this again, Gradle will add a Groovy nature for those projects that have the groovy (or groovy-base) plugin applied. So either add that plugin on the Gradle side, or don't run cleanEclipse after you have added the Groovy nature manually, or switch to the Eclipse Gradle tooling instead of generating Eclipse files.
It seems, that for a real groovy nature setting, I need not only to set
natures.add 'org.eclipse.jdt.groovy.core.groovyNature'
and
apply plugin: "groovy"
, but also to edit the classpath.
eclipse {
classpath {
file {
withXml {
Node node = it.asNode()
node.appendNode('classpathentry',[exported:"true",kind:"con",path:"GROOVY_SUPPORT"])
node.appendNode('classpathentry',[exported:"true",kind:"con",path:"GROOVY_DSL_SUPPORT"])
}
}
}
}
What is interesting, if we turn on the groovy classpath by hand, only the path "GROOVY_SUPPORT" appears in the .classpath file. It is also enough for my project. But when turning by hand the whole Groovy nature, both paths appear. So, I am better including them both, too.
The Gradle-Eclipse plugin (that is gradle tooling for Eclipse, not the Eclipse plugin for gradle). Should be installable on Eclipse 3.6. (I say should be installable because we no longer test on 3.6, but there shouldn't be any reason why it is broken). If you install this plugin, you should be able to import your gradle project into Eclipse without calling gradle Eclipse.
You can install from the update site here:
http://dist.springsource.com/release/TOOLS/gradle
And more information on the project is here:
https://github.com/SpringSource/eclipse-integration-gradle

Maven SBT dependency artifacts?

I manage my project using Maven and SBT at same time. The reasons for this are:
Intellij IDEA cannot import SBT
project.(idea-sbt plugin doesn't
work very well)
I don't know how can get sources and
javadocs from SBT.(I'd like to see any answers about this)
The problem is I don't know how to let Maven download SBT dependency. I search through maven repository and couldn't find anything about sbt. I wanna use Maven or SBT to manage all the jars in my project.
If you put a pom.xml to the root of your project, it will be recognized by SBT. When you specify no managed dependencies in the project definition, SBT relies on Maven dependencies.
As it said in SBT doumentation,
sbt performs this dependency handling
when the update action is executed. By
default, sbt does not update your
dependencies before every compilation,
but only does so when you execute
update. sbt supports three ways of
specifying these dependencies:
* Declarations in your project definition
* Maven POM files
* Ivy configuration and settings files
Maven knows nothing about SBT as of now (at least, I've not heard about any plugins so far), so, the best you can do to manage your project both in Maven and SBT, is to generate POMs by SBT. See SBT to Maven Converter for more details.
idea-sbt plugin works great for me with IDEA 10 - all it's really intended to do is open an SBT shell within the IDE and it does that well enough.
A plugin you should look into if you're interested in getting the Maven out of your build is sbt-idea plugin ( https://github.com/mpeltonen/sbt-idea ). This is a great plugin that generates IDEA files from an SBT project. It couldn't be easier to use. At an SBT prompt, run the following commands:
*sbtIdeaRepo at http://mpeltonen.github.com/maven/
*idea is com.github.mpeltonen sbt-idea-processor 0.3.0
update
idea
Note the asterisks - they should be included.
At this point, you can open your project in IDEA. It won't complain about the SBT dependencies. Any time you add new dependencies to your project file, simply run the 'idea' command again to tell IDEA about it. I do that in the SBT window provided by idea-sbt.
As far as getting sources and docs with dependencies, you can do something like this (from the SBT docs):
val sc = "org.scalacheck" % "scalacheck" % "1.5" withSources()
There is a corresponding withJavadoc() method. Hope that helps.

SBT to Maven Converter

Since most IDEs are only able to import Maven projects, I'd like to generate a POM.xml from an SBT managed project, is there a better way to do it?
Did you try make-pom from sbt?
It generates basic POM for the current project at the ./target.
To customize generation, you can override pomExtra, pomIncludeRepository and pomPostProcess at the project definition.
There is a very direct way provided by SBT. You can use the below command where your SBT file exixts:
sbt makePom
This will generate the .pom file in the target folder you can search that and rename to pom.xml and keep that file in the location and run mvn clean compile install to get full out of it.
Vasil's answer is correct, but for Eclipse and IDEA you can generate IDE metadata more directly using plugins. For IDEA https://github.com/mpeltonen/sbt-idea, and for Eclipse https://github.com/musk/SbtEclipsify .