I'm trying to learn scala and play framework. I choosed IntelliJ Idea as ide. The problem is
editor gives warning on imports like ; "unused import statements"
import play.api._
import play.api.mvc._
Also give warnings on declerations like ; "Cannot resolve symbol Controller"
object Login extends Controller
How can i remove warnings?
Steps i followed;
Create template idea project with play command;
Add scala compiler and library into external libraries.
IntelliJ Idea Version : 12.1 Community Editon
This is how I did it (I'm using Play! 2.1.0):
C:\dev>play new community
_ _
_ __ | | __ _ _ _| |
| '_ \| |/ _' | || |_|
| __/|_|\____|\__ (_)
|_| |__/
play! 2.1.0 (using Java 1.7.0_15 and Scala 2.10.0), http://www.playframework.org
The new application will be created in C:\dev\community
What is the application name? [community]
>
Which template do you want to use for this new application?
1 - Create a simple Scala application
2 - Create a simple Java application
> 1
OK, application community is created.
Have fun!
C:\dev>cd community
C:\dev\community>play idea
[info] Loading project definition from C:\dev\community\project
[info] Set current project to community (in build file:/C:/dev/community/)
[info] Trying to create an Idea module community
[info] Updating {file:/C:/dev/community/}community...
[info] Done updating.
[info] Excluding folder target
[info] Created C:\dev\community/.idea/IdeaProject.iml
[info] Created C:\dev\community\.idea
[info] Excluding folder C:\dev\community\target\scala-2.10\cache
[info] Excluding folder C:\dev\community\target\resolution-cache
[info] Excluding folder C:\dev\community\target\streams
(commons-codec_commons-codec_1.6_test,List(commons-codec_commons-codec_1.3))
(org.apache.httpcomponents_httpcore_4.1.3_test,List(org.apache.httpcomponents_httpcore_4.0.1))
(org.apache.httpcomponents_httpclient_4.1.2_test,List(org.apache.httpcomponents_httpclient_4.0.1))
[info] Created C:\dev\community\.idea_modules/community.iml
[info] Created C:\dev\community\.idea_modules/community-build.iml
C:\dev\community>play compile
[info] Loading project definition from C:\dev\community\project
[info] Set current project to community (in build file:/C:/dev/community/)
[info] Compiling 5 Scala sources and 1 Java source to C:\dev\community\target\scala-2.10\classes...
The Unused import statement is because no classes are being used in that package. This line can safely be removed by optimizing import: Ctrl + Alt + o.
In this case I didn't add any scala libraries at all. I just compiled from the command line.
Related
The problem:
I have 2 dependencies in Scala sbt project - org.slf4j:slf4j-api (direct, but used in other direct dependencies as well) and org.fusesource.hawtjni:hawtjni-generator (transitive). Both of them have the class org.slf4j.helpers.MessageFormatter. The issues arises when a third dependency tries to call MessageFormatter.someFunction(x), which should be called from slf4j-api, but instead it is called from hawtjni-generator.
What I have tried:
Cleaning the IDE (Intellij) files, as I supposed it could be the IDE not recognising them properly. Didn't work...
Adding exclusion rule for hawtjni-generator to all other dependencies, except the one that used it. Didn't work...
Edit:
A simplified dependency tree of the project looks like that:
[info] +-org.slf4j:slf4j-api:1.7.25
[info] +-ch.qos.logback:logback-classic:1.2.3
[info] | +-org.slf4j:slf4j-api:1.7.25
[info] |
[info] +-com.ltonetwork:leveldbjni.leveldbjni-osx:99-master-SNAPSHOT
[info] | +-org.fusesource.hawtjni:hawtjni-maven-plugin:1.18
[info] | +-org.fusesource.hawtjni:hawtjni-generator:1.18
The problem is with logback-classic (that's the third dependency) which wrongly calls hawtjni-generator, instead of slf4j-api.
Have you tried to use an alias for one of your imports?
import org.fusesource.hawtjni:hawtjni-generator
import org.slf4j.helpers.{MessageFormatter => MessageFormatterSlf4j}
MessageFormatterSlf4j.someFunctions(x)
MessageFormatter.someFunctions(x) // from fusesource
Scala import alias
I'm new to sbt. I'd like to know how to get dependent jar files by Scala code, not executing sbt plugin.
In Gradle, it supports to get dependent jar files by Java code like the following (project is an instance of Project class):
Configuration config = project.getRootProject().getBuildscript().getConfigurations().detachedConfiguration();
Set<File> jars = config.resolve();
I'd like to know a way to do like that in sbt and Scala. Does anyone know this? I tried to use sbt.Project#dependencies, but it seems that one doesn't meet for this purpose.
You are looking for dependencyClasspathAsJars:
sbt > inspect dependencyClasspathAsJars
[info] Task: scala.collection.Seq[sbt.internal.util.Attributed[java.io.File]]
[info] Description:
[info] The classpath consisting of internal and external, managed and unmanaged dependencies, all as JARs.
[info] Provided by:
[info] ProjectRef(uri("file:/home/claudio/foo"), "foo") / Compile / dependencyClasspathAsJars
[info] Defined at:
[info] (sbt.Classpaths.classpaths) Defaults.scala:1800
[info] Dependencies:
...
As you can see, this is a Task that returns a scala.collection.Seq[sbt.internal.util.Attributed[java.io.File]] where Attributed is just a simple wrapper around arbitrary data: https://www.scala-sbt.org/1.x/api/sbt/internal/util/Attributed.html
sbt > show dependencyClasspathAsJars
[info] List(Attributed(/home/claudio/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.13.1.jar),
Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/twirl-api_2.13/jars/twirl-api_2.13-1.5.0.jar),
Attributed(/home/claudio/.ivy2/cache/org.scala-lang.modules/scala-xml_2.13/bundles/scala-xml_2.13-1.2.0.jar),
Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/play-server_2.13/jars/play-server_2.13-2.8.1.jar),
Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/play_2.13/jars/play_2.13-2.8.1.jar),
Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/build-link/jars/build-link-2.8.1.jar),
Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/play-exceptions/jars/play-exceptions-2.8.1.jar),
Attributed(/home/claudio/.ivy2/cache/com.typesafe.play/play-streams_2.13/jars/play-streams_2.13-2.8.1.jar),
Attributed(/home/claudio/.ivy2/cache/org.reactivestreams/reactive-streams/jars/reactive-streams-1.0.3.jar),
Attributed(/home/claudio/.ivy2/cache/com.typesafe.akka/akka-stream_2.13/jars/akka-stream_2.13-2.6.3.jar),
Attributed(/home/claudio/.ivy2/cache/com.typesafe.akka/akka-actor_2.13/jars/akka-actor_2.13-2.6.3.jar),
Attributed(/home/claudio/.ivy2/cache/com.typesafe/config/bundles/config-1.4.0.jar)
...)
If you want to process the value in any way, you probably want to write a custom task: https://www.scala-sbt.org/1.x/docs/Tasks.html
SBT scalariform plugin is formatting files more than once in multi project setup. Here is example https://github.com/Seetaramayya/sbt-scalariform-example
if you compile the project you can see something like this in the console
[info] Formatting 5 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "root")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "module1")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "module2")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "main")(compile) ...
There are 5 scala files and 2 sbt files exist in the code base. Neither in SBT nor in scalariform documentation I could not find how to execute the task only once in multi module setup.
I would like to execute the task only once. Only option I see is enable plugin at root project level and disable at sub-projects but I did not like the option (I need to add disablePlugins in 15 sub-projects)
The issue seems to be that
Each subproject's scalariformFormat task in their different scopes
have the project root in their sourceDirectories setting. This just
leads to the whole project being crawled for scala sources and
formatted as many times as there are subprojects.
and has been addressed via scalariformWithBaseDirectory setting
val scalariformWithBaseDirectory = settingKey[Boolean]("Whether or not to format sources in project root (default: false)")
Thus setting withBaseDirectory=false in .scalariform.conf should give output
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "main")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module1")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module2")(compile) ...
Note, however, this will not format *.scala sources in the root project/ directory. To address that we could try keeping withBaseDirectory=false in .conf but switch it on for the root project like so
lazy val root = (project in file("."))
.aggregate(module1, module2, main)
.settings(scalariformWithBaseDirectory := true)
which outputs
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module2")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module1")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "main")(compile) ...
[info] Formatting 5 Scala sources ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "root")(compile) ...
where we still get duplication but not as much as initially.
I am new to scala. I have created a scala object containing a couple of methods. I am trying to invoke the method from Scala REPL but the import statement is not working.
This is the code I tried (It is in a default package):
object Hello extends App {
def sum(xs: List[Int]): Int = 0;
def max(xs: List[Int]): Int = 0;
}
For starting the Scala REPL, I opened the console and then did the following:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\user>sbt
[info] Set current project to user (in build file:/C:/Users/user/)
> console
[info] Updating {file:/C:/Users/user/}user...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_2
2).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import Hello._
<console>:7: error: not found: value Hello
import Hello._
^
scala>
The project workspace and the project is created in E:/Work. Even I tried navigating to the project and then followed the same steps above but it gives the same error.
Please let me know how to make this import statement work from Scala REPL.
For project structure please refer to this page
Your dir tree should be something like
E:
|
`- Work
|
`- {PROJECT}
|
+- build.sbt
|
`- src
|
`- main
|
`- scala
|
`- Hello.scala
Next navigate to E:\Work\{Project} and from there run sbt and only then console and do all the work
Remember that your REPL will refer to classes that were compiled before console were executed.
Note that your project structure could be much simpler like
E:
|
`- Work
|
`- {PROJECT}
|
`- Hello.scala
But this is not recommended
you need to compile your project
exit the scala REPL, then from the shell prompt:
> sbt clean compile package
the compile task is a dependency on the sbt, so just sbt is the same as sbt compile, but i don't see a line in your console output that shows it compiled your project (it will begin with Compiling)
alternatively if you have no dependencies (which it looks like you don't) then you can just paste that code directly in the REPL session, and let the Scala REPL compile your code for you, like so:
scala> :paste
// paste your code in the REPL window
scala> ctrl-D
I have imported the Twitter Cassovary into Eclipse form Git Hub but not sure how to run the examples.Please advise as I am new to eclipse as well as scala
I imported the project, opened a benchmark (cassovary-benchmarks/src/main/java/GraphGenerationBenchmarks) and used Run as->Scala application.
You can also do this from the command line. Start sbt in the main project directory, choose subproject cassovary-benchmarks and then use run. You will be asked which benchmark to run.
➜ twitter-cassovary git:(master) ✗ ls
README.md cassovary-examples project
cassovary-benchmarks cassovary-server sbt
cassovary-core lib_managed target
➜ twitter-cassovary git:(master) ✗ sbt
Loading /Users/szymon/bin/sbt-launch-lib.bash
[info] Loading global plugins from /Users/szymonmatejczyk/.sbt/0.13/plugins
[info] Loading project definition from /Users/szymon/oss/twitter-cassovary/project
[info] Set current project to cassovary (in build file:/Users/szymon/oss/twitter-cassovary/)
> project cassovary-benchmarks
[info] Set current project to cassovary-benchmarks (in build file:/Users/szymon/oss/twitter-cassovary/)
> run
[info] Compiling 12 Scala sources to /Users/szymon/oss/twitter-cassovary/cassovary-core/target/scala-2.11/classes...
[info] Compiling 56 Scala sources to /Users/szymon/oss/twitter-cassovary/cassovary-core/target/scala-2.11/classes...
[info] Compiling 2 Scala sources to /Users/szymon/oss/twitter-cassovary/cassovary- benchmarks/target/scala-2.11/classes...
[warn] Multiple main classes detected. Run 'show discoveredMainClasses' to see the list
Multiple main classes detected, select one to run:
[1] com.twitter.cassovary.PerformanceBenchmark
[2] com.twitter.cassovary.SubsetSamplingBenchmarks
[3] com.twitter.cassovary.GraphGenerationBenchmarks
[4] com.twitter.cassovary.utils.FastUtilsWrappingBenchmark
Enter number: