{"id":460,"date":"2015-11-02T12:52:48","date_gmt":"2015-11-02T19:52:48","guid":{"rendered":"http:\/\/www.managerjs.com\/blog\/?p=460"},"modified":"2015-11-02T12:52:48","modified_gmt":"2015-11-02T19:52:48","slug":"install-sonar-locally-on-osx-and-analyze-a-javascript-project","status":"publish","type":"post","link":"https:\/\/www.managerjs.com\/blog\/2015\/11\/install-sonar-locally-on-osx-and-analyze-a-javascript-project\/","title":{"rendered":"Install Sonar Locally on OSX and Analyze a JavaScript Project"},"content":{"rendered":"<div class=\"clarify-article-content\">\n<div class=\"clarify-article-description\">\n<p>Sonar really shines with statically typed languages like Java and has some very useful analysis it can run for JavaScript projects: critical errors in syntax, code duplication, even test coverage. You can play with all these reports by installing Sonar locally. Thanks to Homebrew it&#8217;s not that hard.<\/p>\n<\/div>\n<\/div>\n<p><!--more--><\/p>\n<div class=\"clarify-article-content\">\n<div class=\"clarify-steps-container\">\n<div id=\"clarify-step-1\" class=\"clarify-step-container\">\n<h3 class=\"clarify-step-title\">Before you Begin<\/h3>\n<div class=\"clarify-step-instructions\">\n<p>Make sure you have Homebrew installed and are on the latest version of Java. If you don&#8217;t update Java then you may run into issues when starting the Sonar server.<\/p>\n<p>To <strong>install Homebrew<\/strong> follow the instructions at <a href=\"http:\/\/brew.sh\/\" target=\"\">brew.sh<\/a>.<\/p>\n<p>To <strong>update Java<\/strong> follow the Stack Overflow instructions for <a href=\"http:\/\/stackoverflow.com\/questions\/24342886\/how-to-install-java-8-on-mac\" target=\"\">How to Install Java 8 on Mac<\/a>. (It&#8217;s pretty much <span style=\"color: #bb0000;\"><code>brew cask install java<\/code><\/span>.)<\/p>\n<p>If you can&#8217;t change versions of Java then try out the rest of the steps and see if sonar will run for you. \u00a0If not, you may need to maintain multiple versions of Java.<\/p>\n<p>If you run into problems using homebrew, then use the <span style=\"color: #bb0000;\"><code>brew doctor<\/code><\/span> command to find out what&#8217;s wrong with your system. Follow the instructions it prints out to fix it.<\/p>\n<p>Note: You may compare the steps listed in this guide with this blog article on <a href=\"http:\/\/zafercelaloglu.blogspot.com\/2014\/07\/how-to-setup-sonar-on-mac-part-1.html\" target=\"\">installing Sonar on Mac<\/a>. \u00a0I found some of the steps unnecessary and don&#8217;t list them here. See also related guides: <a href=\"http:\/\/docs.sonarqube.org\/display\/SONAR\/Get+Started+in+Two+Minutes\" target=\"\">Get Started in Two Minutes<\/a>, <a href=\"http:\/\/docs.sonarqube.org\/display\/SONAR\/Installing+a+Plugin\" target=\"\">Installing a Plugin,<\/a> <a href=\"https:\/\/github.com\/SonarSource\/sonar-examples\/tree\/master\/projects\/languages\/javascript\/javascript-sonar-runner\" target=\"\">JS Sonar Example,<\/a> and <a href=\"https:\/\/github.com\/SonarSource\/sonar-examples\/blob\/master\/projects\/languages\/javascript\/javascript-sonar-runner\/sonar-project.properties\" target=\"\">Sonar Project Properties File Example.<\/a><\/p>\n<\/div>\n<div class=\"clarify-step-image-wrapper\">\n<div class=\"clarify-step-image-container\"><img loading=\"lazy\" decoding=\"async\" class=\"clarify-step-image\" src=\"http:\/\/www.managerjs.com\/blog\/wp-content\/uploads\/2015\/11\/before-you-begin.png\" alt=\"\" width=\"386\" height=\"259\" \/><\/div>\n<\/div>\n<\/div>\n<div class=\"clarify-clear\"><\/div>\n<div id=\"clarify-step-2\" class=\"clarify-step-container\">\n<h3 class=\"clarify-step-title\">Install Sonar and the JavaScript Plugin<\/h3>\n<div class=\"clarify-step-instructions\">\n<p>Use Homebrew to install Sonar, then download and install the jar for the JavaScript plugin.<\/p>\n<ol>\n<li>run <span style=\"color: #bb0000;\"><code>brew install sonar<\/code><\/span> in a terminal window<\/li>\n<li>Find your sonar plugins directory. In my case it was <span style=\"color: #bb0000;\"><code>\/usr\/local\/Cellar\/sonar\/5.1.2\/libexec\/extensions\/plugins\/<\/code><\/span>. Yours will be somewhere ending with &#8220;<span style=\"color: #bb0000;\"><code>\/libexec\/extensions\/plugins<\/code><\/span>&#8221; and you can normally figure it out by inspecting the output of <span style=\"color: #bb0000;\"><code>brew list sonar<\/code><\/span>.<\/li>\n<li>download <a href=\"http:\/\/docs.sonarqube.org\/display\/PLUG\/JavaScript+Plugin\" target=\"\">the JavaScript Plugin<\/a> and put it into the plugins directory<\/li>\n<\/ol>\n<\/div>\n<div class=\"clarify-step-image-wrapper\">\n<div class=\"clarify-step-image-container\"><img loading=\"lazy\" decoding=\"async\" class=\"clarify-step-image\" src=\"http:\/\/www.managerjs.com\/blog\/wp-content\/uploads\/2015\/11\/install-sonar-and-the-javascript-plugin.png\" alt=\"\" width=\"411\" height=\"200\" \/><\/div>\n<\/div>\n<\/div>\n<div class=\"clarify-clear\"><\/div>\n<div id=\"clarify-step-3\" class=\"clarify-step-container\">\n<h3 class=\"clarify-step-title\">Install sonar-runner and Modify Your Environment<\/h3>\n<div class=\"clarify-step-instructions\">\n<p>Use homebrew again. This time install the <span style=\"color: #bb0000;\"><code>sonar-runner<\/code><\/span> formula. Finally, add a little to your <span style=\"color: #bb0000;\"><code>.bashrc<\/code><\/span> file.<\/p>\n<ol>\n<li>Run <span style=\"color: #bb0000;\"><code>brew install sonar-runner<\/code><\/span><\/li>\n<li>Follow the instructions at the end of the install. \u00a0(If you missed them you can always show them again with <span style=\"color: #bb0000;\"><code>brew info sonar-runner<\/code><\/span>.) NOTE: I found that I didn&#8217;t have to modify <span style=\"color: #bb0000;\"><code>sonar-runner.properties<\/code><\/span>. You do have to add something like <span style=\"color: #bb0000;\"><code>SONAR_RUNNER_HOME=\/usr\/local\/Cellar\/sonar-runner\/2.4\/libexec<\/code><\/span> to your <span style=\"color: #bb0000;\"><code>.bashrc<\/code><\/span> file.<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<div class=\"clarify-clear\"><\/div>\n<div id=\"clarify-step-5\" class=\"clarify-step-container\">\n<h3 class=\"clarify-step-title\">Configure and Analyze Your Project<\/h3>\n<div class=\"clarify-step-instructions\">\n<p>You need to add a properties file to you project, launch the Sonar server, run the analysis, and view the results.<\/p>\n<p>1. In a terminal window launch the Sonar server with <span style=\"color: #bb0000;\"><code>sonar console<\/code><\/span>. Leave it running. (You can kill it when you&#8217;re all done with &#8220;\u2318-.&#8221;.)<\/p>\n<p>2. In a separate terminal go to the root of your JavaScript project. \u00a0Add a <span style=\"color: #bb0000;\"><code>sonar-project.properties<\/code><\/span> file like this one:<\/p>\n<pre><code># Required metadata\nsonar.projectKey=org.familysearch:fs-reservations\nsonar.projectName=FamilySearch Reservations App\nsonar.projectVersion=1.680\n\n# Comma-separated paths to directories with sources (required)\nsonar.sources=assets\n\n# Language\nsonar.language=js<\/code><\/pre>\n<p>3. In the terminal run the analysis with <span style=\"color: #bb0000;\"><code>sonar-runner<\/code><\/span><\/p>\n<p>4. View the results by navigating to <a href=\"http:\/\/localhost:9000\" target=\"\">http:\/\/localhost:9000.<\/a> Your project should be listed at the top of the right-hand column.<\/p>\n<\/div>\n<div class=\"clarify-step-image-wrapper\">\n<div class=\"clarify-step-image-container\"><img loading=\"lazy\" decoding=\"async\" class=\"clarify-step-image\" src=\"http:\/\/www.managerjs.com\/blog\/wp-content\/uploads\/2015\/11\/configure-and-analyze-your-project.png\" alt=\"\" width=\"681\" height=\"463\" \/><\/div>\n<\/div>\n<\/div>\n<div class=\"clarify-clear\"><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sonar really shines with statically typed languages like Java and has some very useful analysis it can run for JavaScript projects: critical errors in syntax, code duplication, even test coverage. You can play with all these reports by installing Sonar locally. Thanks to Homebrew it&#8217;s not that hard.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[150,46,149],"class_list":["post-460","post","type-post","status-publish","format-standard","hentry","category-lab-notebook","tag-code-quality","tag-javascript","tag-static-analysis","wow fadeInUp","entry"],"_links":{"self":[{"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/posts\/460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/comments?post=460"}],"version-history":[{"count":2,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions"}],"predecessor-version":[{"id":462,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions\/462"}],"wp:attachment":[{"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/media?parent=460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/categories?post=460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/tags?post=460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}