Scala: Read data from MANIFEST.MF - scala

I'm building my Java/Scala project using maven-assembly-plugin,
I have a function in my project that prints my version number and I want to take it from MANIFEST.MF which is in the META-INF directory.
I found solutions for Java code but not for scala, the only thing I could find is scala.reflect.Manifest but I couldn't understand if it is relevant to my case.

Related

Accessing values in project/Dependencies.scala file in scala code in subproject

I have a multimodule scala project with the following structure -
-A/
-B/
-project/ (root project)
-build.sbt (this has build definition for all the subprojects)
I have an object declared in the project/ folder (lets call this object Dependencies) which contains various constants . Is it possible to access a variable declared in project/Dependencies.scala in scala code inside a subproject(A or B) without creating a dependency of any of the subprojects on the root project.
Please let me know if I need to clarify further.
If you want to make some code from your build definition available for the code in the project, you can use sbt-buildinfo plugin. It's mostly adapted for setting/task keys, but you can use it for any other values defined in your build too.
The way it works is very simple: it uses sourceGenerators to generate a piece of Scala code (with the values from the build) that will be available to the rest of your project sources. So if you don't want to use sbt-buildinfo, you can also generate sources directly. See sbt documentation on Generating files.

scala & intellij : difficulties to create a runnnable jar

I can't launch a scala jar; when I launch it I get the error "Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/immutable/List" which seems to mean the scala library is not loaded...
this is a screenshot showing a lot of informations on the artifact window.
here is the manifest:
Manifest-Version: 1.0
Class-Path: libs/scala-library-2.10.0.jar libs/commons-logging-1.1.1.j
ar libs/jcip-annotations-1.0.jar libs/jwnl-1.4_rc3.jar libs/laf-plugi
n-7.2.1.jar libs/laf-widget-7.2.1.jar libs/miglayout-core-4.2.jar lib
s/miglayout-swing-4.2.jar libs/scala-actors.jar libs/scala-library.ja
r libs/scala-swing.jar libs/slf4j-api-1.6.4.jar libs/slick_2.10-1.0.0
.jar libs/sqlite-jdbc-3.7.2.jar libs/substance-7.2.1.jar libs/trident
-7.2.1-swing.jar
Main-Class: Fenetre
and when I enter "java xf myJar.jar", there are extracted files in the directory:
- .class files
- in the libs folder, there are the libraries INCLUDING scala-library.jar & scala-library-2.10.0.jar(I specified only one of these two files in the manifest to avoid conflicts)
can you help me?
I'm new to Scala and don't know what the problem is however I've been compiling "fat jars" which include all the required libs.
I've been using https://github.com/sbt/sbt-assembly to do this successfully.
Despite what your manifest is telling you, when you run the application you either do not have the scala-library included in your class path or there's some confusion when you attempt to import List. Scala should automatically import the immutable collection classes in your project with the root Predef implementation.
Predef provides type aliases for types which are commonly used, such as the immutable collection types scala.collection.immutable.Map, scala.collection.immutable.Set, and the scala.collection.immutable.List constructors (scala.collection.immutable.:: and scala.collection.immutable.Nil). The types Pair (a scala.Tuple2) and Triple (a scala.Tuple3), with simple constructors, are also provided.
Predef in core Scaladocs
Try printing the classpath from within your app to confirm.
Although not pertinent to your question, I would recommend using SBT for dependency management now that IntelliJ IDEA 13 has full SBT integration support. Your collaborators not using IntelliJ will also be happier because SBT gives them more options for build technologies, editors, and other tooling when working on the project.

Compile scala files from a sbt plugin

I am developing a sbt plugin. In this plugin I generate some new scala sources packaged in a sbt project. Then I need to compile these new files programaticaly so that I could add the generated class in my classLoader.
I do not find any way to compile programaticaly sources from a given sbt project path (and eventually from a classLoader) in the sbt API, something as simple as the sbt command (sbt compile) line would be very convenient, something like:
XXX.compile(path/to/sbt/project)
Thanks
I suggest you have a look at sbt-boilerplate which is an sbt plugin that generates code, works well and is really simple.
Here's a link to the file that you probably want to take a look at

Pig-Scala UDF exception- ScalaObject not found

I have a scala .class file that I convert to a jar and try to register to a pig script. It is able to find that class now BUT it throws a ClassNotFoundException for scala.ScalaObject.
I notice that there is a scala.ScalaObject.class entry in the scala-library jar in the littlepiggy/lib folder.
Question 1
Shouldn't this jar be directly accessible anyways? Or do I have to add this path to an equivalent of a CLASSPATH for Pig?
Question 2
After this, I forcefully registered that jar as well.
I got this error:
java.lang.NoSuchMethodError: scala.collection.JavaConversions$.asScalaIterator(Ljava/util/Iterator;)Lscala/collection/Iterator
This doesn't look right to me. Any ideas?
PS - This source suggests that I should include the scala-library jar but Pig should already be able to find it and anyways, its not really working for me.
(http://mehack.com/levenshtein-distance-function-for-pig-and-had-0)
The answer's here. Should have checked out a more exhaustive set of keywords.
NoSuchMethodError when attempting to implicitly convert a java to scala collection
I was using different scala versions to build the class file and then in pig.
Thanks!

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.