Scala share resources folder to open a file - scala

This is my folder structure and I am trying to load the "grammar.txt" file from resources folder but I get not found error.
val source = Source.fromResource("grammar.txt")
Folder structure:
➜ cfg-tools tree -L 4
.
├── build.sbt
├── src
│   ├── main
│   │   └── scala
│   │   ├── Builer.scala
│   │   ├── Driver.scala
│   │   ├── tokens.scala
│   │   └── Tools.scala
│   ├── resources
│   │   └── grammar.txt
build.sbt
name := "cfg-tools"
version := "0.1"
scalaVersion := "3.0.2"
Compile / unmanagedResourceDirectories += sourceDirectory.value / "resources"

You don't need the custom SBT configuration: just use the standard place for resources which is src/main/resources (note that it's in main subfolder compared to your current structure).

Related

How to publish Test Only Objects in a sbt project

I have been developing a common library for my team, where I need to provide mock data for end users to write unit-test code. Ideally, the mock object should only be available to tests of packages referencing mine, but I am not sure how to do this.
My package structure is:
├── common
│   ├── src
│   │   ├── main
│   │   │   ├── resources
│   │   │   └── scala
│   │   └── test
│   │   ├── resources
│   │   └── scala
│   │   └── MockData.scala // <--- object defined here
├── build.sbt
In my build.sbt, I have
Test / publishArtifact := true
Test / publish := true
packageBin / publishArtifact := true
And I use sbt clean; sbt compile; sbt publishLocal to publish my library locally.
In the project referencing the above library, I added the following to the build.sbt:
ThisBuild / libraryDependencies ++= Seq(
"org.my" %% "common" % "0.0.1",
"org.my" %% "common" % "0.0.1" % Test,
)
but when writing tests, I cannot find objects defined in MockData.scala.
Please provide some hints, much appreciated.
------------------ UPDATE ------------------
After googling around, I'd decided to write a separate module for publishing test data only. So my package structure becomes:
├── common
│   ├── src
│   │   ├── main
│   │   │   ├── resources
│   │   │   └── scala
│   │   └── test
│   │   ├── resources
│   │   └── scala
├── common-testkit
│   ├── src
│   │   └── main
│   │      ├── resources
│   │      └── scala
│   │   └── MockData.scala // <--- object defined here
├── build.sbt
The issue is in the way you ask to retrieve the test code in your other project.
"org.my" %% "common" % "0.0.1" % Test means to depends on the "main" code of project common when running the tests of your other project. That's what the scope Test (after the version) means.
What you want is to depend on the "test code" of common project when running your tests. This is done by specifying what is called a "classifier" in sbt:
"org.my" %% "common" % "0.0.1" % Test classifier "tests"

Importing json resources inside .pex (Python Executable (format by Twitter))

I'm using a Twitter engineered build tool pants to manage many projects inside my monorepo. It outputs .pex files when I complete a build, this is a binary that packages the bare minimum dependencies I need for each project and makes them a "binary" (actually an archive that's decompressed at runtime), my issue is a utility that my code has used for a long time fails to detect some .json files(now that I'm using pants) I have stored under my environments library. all my other code seems to run fine. I'm pretty sure it has to do with my config, perhaps I'm not storing the resources properly so my code can find it, though when I use unzip my_app.pex the resources I desire are in the package and located in the proper location(dir). Here is the method my utility uses to load the json resources:
if test_env:
file_name = "test_env.json"
elif os.environ["ENVIRONMENT_TYPE"] == "PROD":
file_name = "prod_env.json"
else:
file_name = "dev_env.json"
try:
json_file = importlib.resources.read_text("my_apps.environments", file_name)
except FileNotFoundError:
logger.error(f"my_apps.environments->{file_name} was not found")
exit()
config = json.loads(json_file)
here is the the BUILD file I use for these resource currently:
python_library(
dependencies=[
":dev_env",
":prod_env",
":test_env"
]
)
resources(
name="dev_env",
sources=["dev_env.json"]
)
resources(
name="prod_env",
sources=["prod_env.json"]
)
resources(
name="test_env",
sources=["test_env.json"]
)
and here is the BUILD file for the utility that calls these resources of which the python code above is what you saw:
python_library(
name="environment_handler",
sources=["environment_handler.py"],
dependencies=[
"my_apps/environments:dev_env",
"my_apps/environments:prod_env",
"my_apps/environments:test_env"
]
)
I always get an FileNotFoundError exception and I'm confused because the files are available to the runtime, what's causing these files to not be accessible? and is there a different format I need to set up the JSON resources as?
Also for context here is the decompressed .pex file(actually just the source-code dir):
├── apps
│   ├── __init__.py
│   └── services
│   ├── charts
│   │   ├── crud
│   │   │   ├── __init__.py
│   │   │   └── patch.py
│   │   ├── __init__.py
│   │   └── main.py
│   └── __init__.py
├── environments
│   ├── dev_env.json
│   ├── prod_env.json
│   └── test_env.json
├── __init__.py
├── models
│   ├── charts
│   │   ├── base.py
│   │   └── __init__.py
│   └── __init__.py
└── utils
├── api_queries
│   ├── common
│   │   ├── connections.py
│   │   └── __init__.py
│   └── __init__.py
├── calculations
│   ├── common
│   │   ├── __init__.py
│   │   └── merged_user_management.py
│   └── __init__.py
├── environment_handler.py
├── __init__.py
├── json_response_toolset.py
└── security_toolset.py
I figured it out: I changed the way I access the files within the library and it works perfectly before and after the build to .pex format. I used:
import pkgutil
#json_file = importlib.resources.read_text("my_apps.environments", file_name)
json_file = pkgutil.get_data("my_apps.environments", file_name).decode("utf-8")

sbt 0.13 multi project build, import packages across projects

I have a play project that I'd like to build with a common library that handles things like clients, services, utilities, etc...
My dir structure is like so:
.
├── build.sbt
├── myapp
│   ├── app
│   │   ├── controllers
│   │   │   └── HomeController.scala
│   │   └── views
│   │   ├── index.scala.html
│   │   └── main.scala.html
│   ├── conf
│   │   ├── application.conf
│   │   ├── logback.xml
│   │   ├── messages
│   │   └── routes
│   ├── public
│   │   ├── images
│   │   │   └── favicon.png
│   │   ├── javascripts
│   │   │   └── main.js
│   │   └── stylesheets
│   │   └── main.css
│   └── test
│   └── controllers
│   └── HomeControllerSpec.scala
├── myapp-common
│   └── src
│   └── main
│   └── TesterObject.scala
└── project
├── build.properties
├── plugins.sbt
└── scaffold.sbt
My build.sbt is configured as follows:
lazy val commonSettings = Seq(
version := "1.0-SNAPSHOT",
scalaVersion := "2.12.3"
)
lazy val root = (project in file("."))
.settings(commonSettings)
.aggregate(myapp, `myapp-common`)
.dependsOn(myapp, `myapp-common`)
lazy val `myapp-common` = project
.settings(commonSettings)
lazy val myapp = project
.enablePlugins(PlayScala)
.settings(commonSettings)
.settings(libraryDependencies += guice)
.settings(libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "3.1.2" % Test)
.dependsOn(`myapp-common`)
While I am able to successfully run the play project with sbt myapp/run, it fails when I try to import a package defined in myapp-common.
For instance, if I have myapp-common/src/main/TesterObject.scala:
package tester
Object TesterObject {
val testMe = 3
}
If I try to import tester._ and then use TesterObject.testMe in myapp/app/controllers/HomeController.scala, the project fails during compilation since it can't find the package.
Can anyone point me in the right direction here? The sbt guide on multi-projects is a bit tough for me to parse for this particular problem. It seems like my dependencies are appropriately set up.

Where does groovy look for properties files by default?

For this Netbeans project:
.
├── build.xml
├── manifest.mf
├── nbproject
│   ├── build-impl.xml
│   ├── genfiles.properties
│   ├── groovy-build.xml
│   ├── private
│   │   └── private.properties
│   ├── project.properties
│   └── project.xml
└── src
├── Config1.groovy
└── net
└── bounceme
└── mordor
└── groovy
└── file_ops
└── NewGroovyScript.groovy
8 directories, 10 files
what would the standard location, or path, for foo.properties so that it can be easily picked up? in src?
You should be able to stick it in the same folder as your NewGroovyScript.groovy class file, then just do:
Properties p = NewGroovyScript.class.getResource('foo.properties').withInputStream { s ->
Properties properties = new Properties()
properties.load(s)
properties
}

What goes where in Spring MVC Maven Eclipse Juno STS 3.0.0 project?

I'm creating a new Spring MVC webapp.
I used STS 3.0 Dashboard -> Spring Template Project -> Spring MVC Project (URL:http://dist.springsource.com/release/STS/help/org.springframework.templates.mvc-3.1.2.zip) to create the project. It created a directory structure like this:
build/
classes/
src/
main/
java/
com/
example/
web/
HomeController.java
resources/
META-INF/
log4j.xml
webapp/
resources/
WEB-INF/
classes/
spring/
appServlet/
servlet-context.xml
root-context.xml
views/
home.jsp
web.xml
test/ ***(I'll leave out what's under test)***
target/
classes/ ***(I'll leave out what's under classes)***
test-classes/ ***(I'll leave out what's under testclasses)***
WebContent/
META-INF/
MANIFEST.MF
WEB-INF/
lib/
This does not match any directory layout I can find documentation for and it certainly looks wrong. Like why is there a WebContent/WEB-INF as well as a src/webapp/WEB-INF? Why both a build/ and a target/ ?
I want to be able to build and deploy automatically from Eclipse Juno as well as from the command line using Maven, so question 1 is: how do I clean up this directory structure?
Question 2: what is the difference between src/main/resources/ and src/main/webapp/resources? How do I choose which directory to put a given static resource in?
Question 3: If I have libraries that I need to include that I cannot have Maven get, where do I put them?
I tied it in my local and following is the folder structure:
.
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │   └── test
│   │   │   └── spring
│   │   │   └── HomeController.java
│   │   ├── resources
│   │   │   ├── log4j.xml
│   │   │   └── META-INF
│   │   └── webapp
│   │   ├── resources
│   │   └── WEB-INF
│   │   ├── classes
│   │   ├── spring
│   │   │   ├── appServlet
│   │   │   │   └── servlet-context.xml
│   │   │   └── root-context.xml
│   │   ├── views
│   │   │   └── home.jsp
│   │   └── web.xml
│   └── test
│   ├── java
│   │   └── com
│   │   └── test
│   │   └── spring
│   └── resources
│   └── log4j.xml
└── target
├── classes
│   ├── com
│   │   └── test
│   │   └── spring
│   │   └── HomeController.class
│   └── log4j.xml
├── m2e-wtp
│   └── web-resources
│   └── META-INF
│   ├── MANIFEST.MF
│   └── maven
│   └── com.exigen
│   └── spring
│   ├── pom.properties
│   └── pom.xml
└── test-classes
├── com
│   └── test
│   └── spring
└── log4j.xml
(I removed the .XXX files/folder which just the metadata for eclipse)
Basically, for you question:
Typical src/webapp/WEB-INF is the maven way and WebContent/WEB-INF is the WTP way. And the maven way is suggested.
Also build/ is generate by eclipse and target/ is the maven way, you can just ignore this.
for Question2, src/mian/resources is the maven way which will be compiled to the target/classes folder. and 'src/main/webapp/resources' is used for some static resources.
for Question3, actually all the 3rd-party libraries are house in the WEB-INF/lib after package, so you can put your libraries into this folder, no matter using maven or not.