Scala - sbt, main.scala and custom package - scala

I use SBT 0.11.0. It requires that all sources must be placed in:
[project]/src/main/scala/
However I want a custom package such as:
[project]/src/com/test/...
I can leave things as SBT requires, then specify in every source file the custom package:
package com.test
But I got some problems with Eclipse auto-features such as import statements, generating new classes... Eclipse always adds main.scala before com.test. Is there something I can do to solve this?
Edited
For example I have this:
[project]
src
main
scala
com
test
A.scala
package com.test
...
B.scala
package com.test
...
In B, I use some functions of A, I type it, then press Ctrl+Shift+O to let Eclipse import A. Eclipse does this:
import main.scala.com.test.A
But in A I set package to com.test (not main.scala.com.test).
This is small example. But when I have many source files, I need to refactor my code, things will be harder...

You are confusing source folders and packages!
sbt by convention uses the source folder src/main/scala for Scala code. Within this folder you can create whichever packages you want, e.g. a subfolder foo/bar with a file Baz.scala that contains the following code:
package foo.bar
object Baz {
...
}

If you use the eclipse plugin for sbt then you can tell sbt to generate an Eclipse project which has all the details configured to work correctly with sbt's project structure. All the details you need to add it to your sbt project and to run it are at the link above.

Related

Import code packaged as jar into another sbt project

I want to import below code packaged as jar to another sbt intellij project how could i do this?
package yyy
object Hello extends Greeting with App {
println(greeting)
}
trait Greeting {
lazy val greeting: String = "hello"
}
If you have this code packaged as a jar, you can simply place it in the lib/ folder of the other sbt project. It should be on the classpath, so you'll be able to do import yyy._ in the code.
You can read more about unmanaged dependencies in the sbt documentation. Here's an excerpt from it:
Unmanaged dependencies work like this: add jars to lib and they will be placed on the project classpath. Not much else to it!
Dependencies in lib go on all the classpaths (for compile, test, run, and console).
There’s nothing to add to build.sbt to use unmanaged dependencies, though you could change the unmanagedBase key if you’d like to use a different directory rather than lib.

Scala REPL unable to import packge

I'm trying to import com.lambdaworks.crypto.SCryptUtil (from crypto) in the Scala REPL. I'm running the REPL from the Java directory containing com/lambdaworks/crypto.
The REPL can't find com.lambdaworks.crypto.SCryptUtil, but it can autocomplete up to com.lambdaworks.crypto but can't find anything after that.
When I used the REPL in the IntelliJ IDEA after including the package in my project, I was able to find the SCryptUtil class.
Am I missing some classpath parameters that are required for import?
The REPL won't compile the Java code for you—it's only autocompleting that far because it's aware of the directory structure, but once it gets to the crypto directory it won't find any class files.
You can see this more dramatically by moving up a directory and opening a new REPL—you'll be able to autocomplete import java.com.lambdaworks.crypto, even though that's obviously not a real package hierarchy.
In this case you can move to the project root, run mvn compile to compile the Java code, and then start the REPL like this (still in the project root):
scala -classpath target/classes
Now you can import com.lambdaworks.crypto.SCryptUtil.
This only works because the project doesn't have any runtime dependencies, though—in other cases you may need either to add other things to the classpath, to build a JAR with the dependencies baked in (e.g. with the Maven Assembly plugin), or to use the mvn scala:console goal of the Maven Scala plugin.

package with _root_ breaks the scala compiler inside eclipse

I was refactoring the package structure inside a scala project with eclipse. In the middle of this I used a package statement with _root_, like this
package _root_.com.workday.foo.bar
Now my eclipse project will not build although I can still build everything in the command line. The problem is that all my com.* imports are broken
import com.workday.some.thing.Else
The error is:
Multiple markers at this line
- object workday is not a member of package com.workday.foo.bar.com
- object workday is not a member of package com.workday.foo.bar.com
It appears that it is trying to find all imports relative to the package statement at the top of each source file.
I have removed the _root_ from my package and tried to clean my project and the workspace. I have also tried changing the scala compiler from "sbt" to "refined" and back but to no avail
Does anyone know how to kick the eclipse scala compiler back on track???
Sorry to answer my own question so quickly, but I discovered the problem.
I had a sub package defined in the middle of a source file that contains various mock classes, this sub package had been changed to com.workday.foo.bar.messaging so it made coma sub package of com.workday.foo.bar resulting in a package of com.workday.foo.bar.com.workday.foo.bar.messaging

creating and using standalone scalaz jar without sbt

I've downloaded scalaz snapshot from repository (version 6.0.4).
I want to create standalone jar file and put it into my scala lib directory to use scalaz without sbt.
I'have scala package from scala-lang.org, and stored in /opt/scala
As far I did:
go to untared scalaz directory
run sbt from scalaz project
compile scalaz project
make a package (by package command)
sbt make a jar full/target/scala-2.9.1/scalaz-full_2.9.1-6.0.4-SNAPSHOT.jar
it also produce other jar: full/lib/sxr_2.9.0-0.2.7.jar
I moved both jars to /opt/scala/lib
After this I try scala repl and I can't import scalaz. I tried to import scalaz._, Scalaz._, org.scalaz._, scalaz-core._ and don't work.
REPL code completition after typing import scalaz it suggest: scalaz_2.9.1-6.0.4-SNAPSHOT.
But import scalaz_2.9.1-6.0.4-SNAPSHOT._ don't work
Any idea?
you can download scalaz and extract the jar that contains scalaz-core_2.9.1-6.0.3.jar. Or download scalaz-core directly.
then you can use : scala -cp scalaz-core_2.9.1-6.0.3.jar to launch the REPL finally import scalaz._ as expected.
If you want to use the jar produced by sbt, you can find it in core/target/scala-2.9.1/scalaz-core_2.9.1-6.0.4-SNAPSHOT.jar (you will also find source and javadoc packages in the same directory). Just put this file in your classpath (using scala -cp for example) and you will be able to import scalaz._
I think I know the problem.
scalaz-full_2.9.1-6.0.4-SNAPSHOT.jar is not a java jar class package, it's just a zip with scalaz project - so it contains not package - like directory tree (eg: directory names contains '.').
So to use it we need to unpack scalaz-full_2.9.1-6.0.4-SNAPSHOT.jar, and copy desired jars (eg: scalaz-core_2.9.1-6.0.4-SNAPSHOT.jar, scalaz-http_2.9.1-6.0.4-SNAPSHOT.jar ...) to lib directory.

I can't use sbt.Process inside /src?

I'm currently using sbt to build and run my scala programs. I'm trying to use sbt.Process to execute system commands. I must be missing something because when I try to import sbt.Process in one of my files in src/ I get this error.
not found: value sbt
[error] import sbt.Process._
So it looks like I can't access the sbt package inside my src/ files. What do I need to do to access it? Thanks.
SBT's environment (v 0.7.x) is only available in your build file or a Plugin.
The easiest way to use sbt.Process library (until 0.9.x which will have Process as an independent library) is to copy (BSD License) Process.scala and ProcessImpl.scala into your project
There are different classpaths for running sbt and compiling your source files.
One classpath is for compilation of files in directory project/build (that one contains sbt jars and usually scala library 2.7.7) and the other one is for building source files of your project (that one contains your dependencies from lib and lib_managed and usually scala library 2.8.*). If you'd like to use sbt.Process in your source files you can do two things:
add sbt jar to lib or lib_managed for it to be available on your project's classpath
use snapshot version of scala 2.9, it would have sbt Process built-in as sys.process package
Wait for Scala 2.9, and then just use it out of scala.sys.process.
sbt package has became an integral part of the Scala standard library since version 2.9
...this API has been included in the Scala standard library for version 2.9.
quoted from sbt wiki
Here's the link (scroll down)
well, in order to use it, all you have to do (assuming you are using sbt for build), is to add in build.sbt file the following line of code: sbtPlugin := true it will add the needed dependencies to your project.
of course, this solution is only to get your imports with sbt package to work. you should refactor your code to use the new package scala.sys.process like Daniel C. Sobral suggested.