Code RED tools for Scala - scala

I have been a Java developer and have been using code red tools like FindBugs, CheckStyle, PMD-CPD etc., I have integrated these plugin in Jenkins for automated reviews as well.
When I moved to scala, I want to continue with Maven and Jenkins. But I am not able to find code red tools supported in eclipse, maven-plugin & Jenkins plugins for Scala. Can anyone give some pointer to this to address the static code analysis, code coverage etc., ?

There are many cool tools for integrated with scala, i provide list of tools preferred by me below:
Scalastyle https://github.com/scalastyle/scalastyle
Scapegoat https://github.com/sksamuel/scalac-scapegoat-plugin
Wart remover https://github.com/typelevel/wartremover
Linter https://github.com/HairyFotr/linter
CPD https://github.com/sbt/cpd4sbt
Abide https://github.com/scala/scala-abide
UPD
Firstly, for integration between scala and jenkins you must use sbt plugin in jenkins. Secondly, there are several useful plugins for code quality in jenkins (scapegoat, see link above, it's flexible for your case wiht scala and jenkins), scoverageplugin https://wiki.jenkins-ci.org/display/JENKINS/Scoverage+Plugin for code coverage metrics and of course own commands of sbt, they are very helpful, like sbt doc, sbt test. I found nice article about jenkins and scala http://yeghishe.github.io/2015/02/28/continuous-integration-for-scala-projects.html

Related

Setting up a Scala project in Eclipse, together with JUnit & Scalatest

I have recently completed the Scala course on Coursera, and since then I have been looking forward to getting my hands dirty with Scala again. I have written code for some years but I neither educated to be nor work as a programmer, so it took me a while to get a good opportunity but now that I have some time to invest and a good project to work on it's time...
Except I can't seem to get things set up properly, which I find really frustrating. I have OpenJDK 1.7.0_25 running on my Linux machine. I have downloaded and installed the Bundle Scala IDE build for Eclipse (just like we used in the course). And I got ScalaTest both as a jar file and the Eclipse plug-in.
I have a simple project (so far) and no matter what I do I can't seem to get my builds and tests in order. First off how exactly am I supposed to set up my project so that my classes and tests are actually run properly? All the assignments we got were projects that had the same structure, so do I have to have:
project
|--src
|--main
|--scala
|--test
|--scala
structure? If so why is it not the default way the project is setup when I create a new project? Do I create these folders manually, as packages or as source folders? The whole thing gets pretty murky..
I should mention that I tried to "Mavenize" the project using the contextual menu in Eclipse, added my ScalaTest dependency. The first thing that happens is that I get compile errors, at every point of dependency in my code. So clearly the library is not visible, in other words Maven does not seem to be doing much of management. I thought the whole point of Maven was to get and maintain dependencies as the project evolves. I concluded that I do not fully understand the way Maven works and thus I eventually gave up on Maven, once again, and went back to doing things manually.
Secondly, I can't seem to run my tests; the Run As... menu item does not include ScalaTest as it's mentioned in the documentation of ScalaTest Eclipse Plug-in. I have double checked that the plugin is installed. If I instead try to run using JUnitRunner then my tests are not recognized as valid tests. I have JUnit and ScalaTest on my build path, so it's got to be something else.
I suppose my overarching question is as follows:
given the Scala IDE build of Eclipse and ScalaTest, just exactly how am I supposed to set up my project (in Eclipse) so that I can just focus on writing my code and testing it, and hopefully not have any other headaches?
I work alone, and this project is not a product I need to deliver to some client. In other words I do not need to adhere to strict professionalism here. Honestly I just want to be able to code, get better acquainted with Scala and hopefully build a small data analysis tool that I will be using from time to time.
Thanks in advance!
Try using the sbt eclipse plugin:
https://github.com/typesafehub/sbteclipse
This is of course assumes that you use sbt as you build tool. If you don't at the moment you can find instructions on installation and usage here: http://www.scala-sbt.org/
Personally I've been using typesafe giter8 template (https://github.com/typesafehub/scala-sbt.g8) to setup my Scala projects, and then I use the sbt plugin mentioned above to generate eclipse project files.
Scala is somewhat Maven-based (sometimes implicitly), that's why you use that structure.
The easiest way I think is to create a simple Sbt/Maven POM and create the Eclipse project configurations (like with sbt eclipse). There you can set the dependencies (like the actual version of JUnit, Scalatest to use), so you can use the ScalaTest plugin easily.
In case of other issues, feel free to ask at the ScalaTest mailing list, Chee Seng and Bill Venners can help you a lot there.
The Scala IDE website has a full documentation on how to run unit testing frameworks with the IDE, have a look ! If you find missing elements, the bug tracker of the scala-IDE project is here.

How to add new JVM languages e.g. Scala, Clojure, Fantom, Groovy to Eclipse IDE?

What's a prefereed way to download Scala , via scala-lang.org, can it be added directly by the Eclipse IDE or how to add Scala to Eclipse IDE? Is there a convention on how to add a JVM language?
Update
I could add Clojure and Scala from Help...install new software so now I can create those kinds of projects:
You typically install a plugin for that language using Eclipse's build in plugin language.
for instance for Clojure you can search the plugin manager for "counter clockwise" to get Clojure support. for Scala it looks like http://scala-ide.org/ is a good starting point for detailed instructions and a nice screencast on setting this up. It's worth noting that many JVM languages like Clojure don't need to be explicitly installed, they are downloaded as required by standard Java build tools like Maven. Other languages like Groovy are more easily used if you install them explicity.
If you're an IDEA user, by far the best way is to use SBT to manage your projects and use its sbt-idea plug-in (which adds the gen-idea task) to create IDEA projects files and directories.
Ideally, use Paul Phillips' SBT launcher (to get SBT version flexibility if you're ever going to build 3rd-party projects whose specified SBT versions can be all over the map) and configure non-project-specific plugins, such as sbt-idea, in your per-user shared ~/.sbt directory. That way all your projects have it automatically yet it does not intrude on the project definition itself.
Optionally, there is an IDEA plug-in that integrates SBT's interactive console with the IDEA app. The best part of that is compilation errors become hyperlinks to your code.
I don't know if there's an Eclipse counterpart to sbt-idea, but even if there's not, SBT is the way to go.
The JVM languages typically have plugins for Eclipse, e.g. Counterclockwise for Clojure.
In many cases you can also use JVM languages without a plugin in a regular Java project. For example the following approach works with Clojure:
Include clojure-1.4.0.jar as a dependency (using Maven or similar)
Include clojure source files as regular resources in your project
Write a short piece of Java code that executes the clojure code by calling the appropriate methods in clojure.lang.RT (a class provided in Clojure's jar file)

How should one start on creating a sbt plugin?

I want to create a sbt plugin for Scala project.
Please any one suggest me how we start?
I referred Plugins documentation but unable to understand steps.
The first step in becoming a sbt plugin author is understanding sbt build definition. The best resource for it is Getting Started guide. For plugins, it's essential that you understand the concept of scoping. Some of my blog posts like an unofficial guide to sbt 0.10 v2.0 and traveling through the 4th dimension with sbt 0.13 discuss the topic.
Next, try reading the source code for existing plugins:
sbt/sbt-appengine
sbt-appengine adds defines appengineSettings, which the build user can include in his or her build definition to add appengineDeploy and other appengine related tasks.
sbt/sbt-man
sbt-man on the other hand overrides settings and adds man command.
These are roughly two patterns for plugins. Once you understand them fully, try making your own plugin.
Another source of inspiration is the source for sbt itself. Whenever I'm writing a plugin, I'd consult Defaults.scala to see how sbt implements a particular task that I'm interested in. Once you're comfortable, you should also read Plugins Best Practices too.

Is there a Sonar-level code coverage equivalent for Scala?

I'm trying to set up simple code coverage reports for a team coding in mixed Scala/Java at approx. a 90/10 ratio and running into some serious roadblocks. I've previously set up & administrated Sonar to great success with a Java-only team, but it doesn't appear to be an option.
Sonar w/Scala plugin is buggy and appears to support Scala-only projects, not mixed ones.
SCCT integrates with our maven build, but fails out with false-negative test failures repeatedly.
Undercover has been my best luck so far; It's integrated with our maven build & generates reports, but they aren't archived or hosted anywhere as they would be with Sonar. There also appears to be no central index to make it simple to navigate the generated reports.
I've read the answers here on StackOverflow, but they largely date back to 2010 and suggest that no decent solution is available. Has this changed?
Is there something obvious I'm missing?
About Sonar side:
yes, the Scala Sonar Plugin development is currently stalled. It was initiated by the community, but nobody has offered to take it over yet. If there are some volunteers, we'll be glad to guide and help them.
concerning the support of several languages inside a single project, support will be coming in Sonar. I can't give you a roadmap for it, but we're currently thinking about how to add this support in Sonar in the next releases, so this is a short term issue.
You can either use SCCT or JaCoCo.
SCCT: It supports Scala up to version 2.10, but development seems to be a stalled for a about 9 months. It supports Scala natively and works with both, Maven and SBT.
JaCoCo is under sctive development. It supports any version of Scala, but not natively, but on bytecode level. So you might get some artifacts, e.g. some code gets only partial coveragege, because the generated bytecode has some theoretical code path JaCoCo sees (but which can never be executed from Scala code).
JaCoCo can be a little tricky to set up with Maven and Scala. Here a few tricks:
Use the variant with the agent launcher. Do not use the variant with preprocessing bytecode.
When using JaCoCo with Maven: There is a Maven task (jacoco:prepare-agent) which will produce the correct expression for the agent launcher and stores it into a property. You can then use this property as a command line parameter when running the Java virtual machine.
Parametrize the agent launcher, so that multiple launches (e.g. for running different tests) write to the same log file. Some IDE plugins will have problems with parsing such a file, but the JaCoCo Hudson plugin for example works fine.

Best practices for command-line builds

Item 2 of the Joel Test is "Can you make a build in one step", but what is the best (or commonly accepted) way to achieve this? What are the pros and cons of using the IDE's command-line interface to do a headless build, as opposed to maintaining a build script that is completely independent of the IDE (e.g. using Ant or Maven in the case of a Java project).
I ask this question because I am experimenting with Maven, and was a bit surprised to find that even when using the m2eclipse plugin it's not really feasible to turn off Eclipse's Java Builder and delegate the whole build process to Maven while working on the project. It seems that if I want to migrate the project to Maven, I'll end up having to maintain two equivalent but different build processes, for example the command-line build will use the DataNucleus Maven plugin for enhancing JDO classes, while the IDE build will use the DataNuclues plugin for Eclipse; the command-line build will use the Tomcat Maven plugin, while the IDE will use Eclipse's web tools platform. This redundancy seems unfortunate.
I gather that NetBeans uses Ant for its build, which sounds like it would solve this problem. But unfortunately I'm using neither NetBeans nor Ant!
EDITED TO ADD: I found that I can set up a Maven builder in Eclipse with specific goals that call some of the Maven plugins that I'm using (e.g. in my case the goals "datanucleus:enhance process-resources" take care of JDO enhancement, resource copying, and native2ascii conversion). This leaves java compilation up to Eclipse, but still achieves some degree of integration with Maven.
The eclipse builder gives you intermediate and repeated building, but the "Joel Test" is really about being able to go from source to ready-to-deploy something in a single step. If you're using Maven, there's several ways of going about it - including just invoking Maven as a command line script from within Eclipse.
mvn package
Is what I most frequently used for a quick run through the whole build setup. You can extend Maven through the POM and inject some additional mechanisms if you want to. If you get a little more complex, it's often most effective to start using multi-module POM and maven setups to get functional tests integrated and running with the rest of the code.
Basically, let eclipse "do it's thing" with it's builders and take advantage of what it provides, but when you're ready to use the end product have it invoked from the command line through a continuous integration server (Hudson is a nice easy one to get and set up - free too: http://hudson-ci.org/). Presumably if you're looking at using Maven, you'll also have an instance of Archiva or Nexus set up as your DSL for the resulting libraries. You can have hudson invoke "mvn deploy" for regular checkin builds, or "mvn release:prepare && mvn release:perform" for when you're ready to cut the release (separate builds in Hudson work best for this)
You can do a headless build in an IDE. In eclipse you can execute an arbitrary shell command as an "External Tool". This is also true in IDEA and netbeans with a little effort. At the very least, this is a convenient test of the headless build that you should run whenever you make changes to the build configuration.
Also, I'd like to add that the build should be one-step as a minimum requirement. It should also be easy to set up and easy to debug. If it takes longer than an couple of hours to set up a new developers environment, then the one-step process is likely to be less than optimal. This is with the caveat that if you add or replace team members quarterly or annually this is less of an issue.