sbt 0.13 multi project build, import packages across projects - scala

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.

Related

Scala share resources folder to open a file

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).

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"

Game_ cannot be resolved to a variable in arquillian persistance example

I was asking on #arquillian Freenode IRC channel about question
Arquillian JPA tutorial: Could not create new instance of class org.jboss.arquillian.test.impl.EventTestRunnerAdaptor
when a user told me that he successfully runned
https://github.com/arquillian/arquillian-examples/tree/master/arquillian-persistence-tutorial
so I did
git clone https://github.com/arquillian/arquillian-examples.git
then in Eclipse, I clicked File->Import->Existing Maven Projects and selecte the subdirectory
arquillian-examples/arquillian-persistence-tutorial
Once Eclipse finished importing the project, I obtain in file
/src/test/java/org/arquillian/example/GamePersistenceTest.java
line 146, the error message
Game_ cannot be resolved to a variable
What can I do? In my attempt at the link at beginning of the page, this did not happpen.
I did not change any file downloaded from git repo.
Folder tree
.
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── org
│   │   │   └── arquillian
│   │   │   └── example
│   │   │   └── Game.java
│   │   └── resources
│   │   └── META-INF
│   │   └── persistence.xml
│   └── test
│   ├── java
│   │   └── org
│   │   └── arquillian
│   │   └── example
│   │   └── GamePersistenceTest.java
│   ├── resources
│   │   ├── arquillian.launch
│   │   ├── arquillian.xml
│   │   └── jbossas-ds.xml
│   ├── resources-glassfish-embedded
│   │   ├── glassfish-resources.xml
│   │   ├── logging.properties
│   │   └── test-persistence.xml
│   ├── resources-glassfish-remote
│   │   └── test-persistence.xml
│   └── resources-jbossas-managed
│   └── test-persistence.xml
└── target
├── classes
│   ├── META-INF
│   │   ├── MANIFEST.MF
│   │   ├── maven
│   │   │   └── org.arquillian.example
│   │   │   └── arquillian-persistence-tutorial
│   │   │   ├── pom.properties
│   │   │   └── pom.xml
│   │   └── persistence.xml
│   └── org
│   └── arquillian
│   └── example
│   └── Game.class
└── test-classes
├── arquillian.launch
├── arquillian.xml
├── glassfish-resources.xml
├── jbossas-ds.xml
├── logging.properties
├── org
│   └── arquillian
│   └── example
│   └── GamePersistenceTest.class
└── test-persistence.xml
30 directories, 24 files
The _Game class in that example is generated by the Hibernate JPA 2 metamodel generator (hibernate-jpamodelgen) that is defined as a dependency in the project POM. You'll now need to generate the metamodel by employing one the options outlined in the metamodel generator reference guide.
You could use Eclipse itself by configuring the annotation processing phase. Or you could modify the POM to use the maven-compiler-plugin configuration specified in the guide, to run as part of your build.

Which folders are unnecessary while deploying a play! app?

I have finally made a production worthy app with play, and use the play war -o foo command to create an exploded war. However, its size goes on to 30 MB and there are a lot of folders included. I develop in eclipse, so there are some eclipse folders too. Below is a list of the folders I have. Can anyone help me out with the unnecessary folders? I have zeroed in on the app, conf, public and lib folders. Is my assumption correct?
├── app
│   ├── controllers
│   ├── jobs
│   │   ├── daily
│   │   └── monthly
│   ├── models
│   │   ├── encrypt
│   │   ├── file
│   │   ├── login
│   │   └── mail
│   ├── playground
│   └── views
│   ├── Application
│   └── errors
├── conf
├── eclipse
│   └── classes
│   ├── controllers
│   ├── jobs
│   │   ├── daily
│   │   └── monthly
│   ├── models
│   │   ├── encrypt
│   │   ├── file
│   │   ├── login
│   │   └── mail
│   └── playground
├── lib
├── logs
├── public
│   ├── bootstrap
│   │   ├── css
│   │   ├── img
│   │   ├── js
│   │   │   └── tests
│   │   │   ├── unit
│   │   │   └── vendor
│   │   └── less
│   ├── images
│   ├── javascripts
│   └── stylesheets
├── test
└── tmp
├── bytecode
│   └── DEV
└── classes
├── controllers
├── helpers
├── jobs
│   ├── daily
│   └── monthly
├── models
│   ├── encrypt
│   ├── file
│   ├── login
│   └── mail
└── playground
You need to delete your tmp folder as well, try to use play clean on your project.
All folders in app, conf, lib and public should be kept in your war. Maybe should add precompiled

spring-security-facebook, s2-init-facebook not generating the Default Dao

I have installed spring-security-facebook to a test grail application. This was done after installing spring-security-core and running s2-quickstart.
I am just a novice and was trying to integrate the facebook login button into my test app. But, the problem is that when I run s2-init-facebook the plugin is not generating the default Dao as it was told in the documentation in http://grails.org/plugin/spring-security-facebook
Use your own authentication dao
Plugin generates an Dao, after calling s2-init-facebook#, and put it
into your #conf/spring/resources.groovy and #Config.groovy#. Actually
it's an 'dumb' dao implementations, you have to rewrite it to follow
your data structures:
The shortened tree output of my app directory is given below:
.
├── application.properties
├── grails-app
│   ├── conf
│   │   ├── ApplicationResources.groovy
│   │   ├── BootStrap.groovy
│   │   ├── BuildConfig.groovy
│   │   ├── Config.groovy
│   │   ├── DataSource.groovy
│   │   ├── hibernate
│   │   ├── spring
│   │   │   └── resources.groovy
│   │   └── UrlMappings.groovy
│   ├── controllers
│   │   ├── LoginController.groovy
│   │   └── LogoutController.groovy
│   ├── domain
│   │   └── com
│   │   └── fbtest
│   │   └── webapp
│   │   └── auth
│   │   ├── FacebookUser.groovy
│   │   ├── SecRole.groovy
│   │   ├── SecUser.groovy
│   │   └── SecUserSecRole.groovy
│   ├── i18n
│   │   ├── ...
│   ├── services
│   ├── taglib
│   ├── utils
│   └── views
│   ├── error.gsp
│   ├── index.gsp
│   ├── layouts
│   │   └── main.gsp
│   └── login
│   ├── auth.gsp
│   └── denied.gsp
├── lib
├── scripts
├── src
│   ├── groovy
│   └── java
├── test
│   ├── integration
│   └── unit
└── web-app
├── ...
resource.groovy file is also empty.
// Place your Spring DSL code here
beans = {
}
Please, help me in understanding if I am doing any thing wrong.
I am using Grails version: 2.0.4 and spring-security-facebook's version is 0.8.
Thanks in advance.
Oh, it should be removed from documentation. For last versions it's not necessary to have own DAO, now it have default very flexible dao. And now it's not generated during install. Try to run your app, it should works, if everything is configured correctly.
You can still use own DAO, if you have implemented it by yourself. But for versions since 0.7 nearly everything can be extending with optional FacebookAuthService, that you can implement if you wish (it's described in documentation as well).