How to share .proto (protobuf) files using a shared scala library using sbt - scala

I have a few apps using shared .proto files. Each app's repo currently contains a copy of the files, which is not ideal and has recently created a problem when they accidentally diverged.
I would like to store the .proto files in a shared library which is already a common dependency for these apps. We're using sbt-protoc which has documentation for including .proto files from external libraries, but I can't find any information on how to package libraries that include them.
The .proto files are located in src/main/protobuf, but do not appear in the generated jar, which is presumably standard behaviour. I know you can tell sbt to include specific resource files, but I don't know if I've missed how to do it using sbt-protoc

To get protos included in the JAR, you can rely on standard sbt functionality, by adding a setting such as:
Compile / unmanagedResourceDirectories += sourceDirectory.value / "protobuf"

Related

outside lib in GitHub

I just recently used Github, and when I was trying to upload my java project I realized that I was using some external libraries like apahce poi in this project, and these files have to be stored in libs for my application to function, do I need to upload these files because I realize that might violate some issues(maybe ?).
If yes, then what is the correct way to upload or maybe just post a link to those dependency
Use a tool that provides a dependency management system such as Maven or Gradle (these are both common choices in the Java ecosystem). Your project will then include a configuration file that Maven or Gradle will use to download dependencies so you don't need to distribute them with your project.

What is the correct way of using static assets in a Scala SBT project?

What is the best way of using static resources in an SBT based Scala project with a packaging plugin such as sbt-assembly or sbt-native-packager.
We know that by using TypeSafe-Config with sbt-native-packager's universal plugin, we can just put the configuration file in the resources directory under sources. However, what if I wanted my application to have other static sources such as JSON files containing mappings, and models?
I understand that I can just reference the resources directory and read from the file, but would that still work after packaging the application with plugins (assuming the universal or docker plugin in this case)?
If not, what is the correct way to achieve this?
You could use
unmanagedResourceDirectories += (baseDirectory in <project>).value / some / path
to add more directories that are later mapped into the jar as static resources.
Put it into the resources directory, but don't "reference the resources directory and read from the file": use ClassLoader.getResourceAsStream() (or getResources, depending on your requirements) instead. This is the same technique TypeSafe Config and innumerable other libraries use. For this it doesn't matter if you use sbt-native-packager or not.
This approach runs into problems if you need to make these resources available specifically as files (e.g. to feed them to an external process). In this case add them to mappings as shown here:
mappings in Universal in packageBin += file("README") -> "README"
(obviously replacing "README" with the file(s) you need).

Organizing files in a SBT-based scala project

Newcomer to the Intellij IDE here, with no Java background. I've looked at Build Definition to get a brief idea on how should I organize my scala files, but their example doesn't cover the full structure of an SBT-based project shown attached.
Can you advise what each folder should be used for (e.g. where my source files should go, etc.) and also point me to sources where I can go read up more.
Thanks so much
It is described pretty well here:
http://www.scala-sbt.org/0.13.5/docs/Getting-Started/Directories.html
But to sum up.
.idea:
This contains the project files for your idea project, and has nothing directly to do with sbt itself. However idea (if auto refresh is enabled) updates its own project, each time the sbt build files change.
project:
This contains the sbt project files, except for the main build file (files ending in .sbt). Sbt build is itself based on scala, and if you need to have some scala code included in your build (e.g., code-generation/meta-programming, pre-compiler macros), then you can place scala source files in this directory. The code of these files can be used in your build system, and is not part of your project itself. To really understand how a build is made, then you will need to understand the difference in how sbt files and scala files for the build should be placed. When you run sbt, then it will search for .sbt files in the directory your are standing in, when these are found, it will search for scala files in the project directory. These files together are the source of the build system, but because these are source files, they need to be built before they can be used. To build this build system, sbt uses sbt. So a build system to build the build system is needed. It therefore looks for sbt files inside the project directory, and scala files for this build inside project/project and build these files to get a build system, that can build the build system (that can build your project). Actually it can continue recursive down to any project/project/project... directory, until it finds a project folder containing no scala files, and therefore needs no building before use.
The target folder inside project, is the target folder for the sbt build of your build definition. See below what a target folder is.
Normally you would not need to be concerned about this; just remember that build.sbt in your root directory is the build script for your project. project/plugins.sbt defines plugins activated for your build system, and project/build.properties contains special sbt properties. Currently the only sbt property I now of, is what version of sbt should be used.
src:
This is where your place the source files of your project. You should place any java sources in src/main/java, scala sources in src/main/scala. Resources are placed in src/main/resources.
The src/main/scala_2.11 folder is typically used, if you have some code that it not binary compatible with different versions of scala. In such cases you would be able to configure sbt to use different source files when building for different versions of scala. You probably do not need this, so I would advise to just delete the src/main/scala_2.11 folder.
Test sources are placed inside src/test/java and source/test/scala, and test resources are placed in src/test/resources.
target
This folder is the target folder for sbt. All compiled files, generated packages and so on are placed somewhere inside this dir.
Most things in this dir are not so interesting, as most of it is just internal sbt things. However if your build a jar file by calling sbt package, then it will be placed inside target/scala-x (where x is the scala version). There are also a lot of different plugins, that can package your application in different ways, and they will normally also place the package files somewhere inside the target dir.

How to access external libraries using Play Framework?

I'm trying to use an external library contained in a .jar file with the Play Framework.
I've added the .jar file to the lib/ directory, to no avail.
I know I could add the dependency to my project/Build.scala file, but I have no idea what the group ID, artifact ID, or version numbers are. Are those found in the .jar file?
You can go to Project Structure
Under Project Setting -> Modules ->
Go to tab Dependencies , under sbt-unmanaged-jars you can edit and add your lib manually.
groupID, artifactID and version are “Maven Coordinates”. These three identifies are needed to find exact jar file in the Maven Repository. When provided, the build system (and Play! uses SBT) can automatically find, download and include the library you want to use (assuming that that library exists in the repository).
As that is a global repository, groupID should uniquely identify the project. groupID is usually the same as the main project's package, e.g. org.apache.commons. artifactID is supposed to identify a particular jar in the project, e.g. commons-io. version, quite obviously, points to the exact version of the jar.
How to use IntelliJ with Play Framework and Scala
see this short tutorial.
but you have to add all necessary jars to lib folder before call create module command idea with-sources=yes
So, again
Create a new application
Create lib folder and copy all jars
Create the IDE module
This is only one way how I can deploy it successfully

Creating a JAR file, some classes are missing

I'm creating a JAR file in Eclipse and for some reason classes are missing. The classes that are not included are referenced in other JAR files included on my build path. What doesn't make sense is that the behavior is not consistent. Some classes on the build path get included while others do not. Any ideas?
The step I take to create my JAR file, is to export all the source folders.
JAR files are libraries, and that means - thinking object oriented:
If the classes are referenced in other JAR that included in your build, so they have to be part of the included JAR files and not part on your new JAR.
That's the whole idea of a library - If I understand your question right.
If your JAR uses those external classes, so you have to include those classes's JAR files in your project.
I hope I understood you correctly.
When I need to distribute something for internal use, I use the Maven assembly plugin: it allows you to create jars with dependencies. This is very useful if you only want to pass around one single jar: http://maven.apache.org/plugins/maven-assembly-plugin/usage.html