IT, Scala, Java, Web, Ubuntu

Category: IT (Page 1 of 13)

Trying Scala Script – Script to Create a Symbolic Link to JDK on Mac OS X

I used to use Ubuntu Linux most of the time, yet I’m using Mac OS X more these days. I’m quite satisfied with it, but there’s one thing inconvenient. That’s installing JDK. On Ubuntu, installing Java 8 (technically what I want to install is JDK 8 but since Java has really terrible versioning history, I’ll just call it Java 8) is not supported by Canonical, but it can be done easily by the PPA provided by Web Upd8 (Thanks Web Upd8).

On OS X, as we all know, PPA-like solution is Homebrew (hereinafter referred to as ‘brew’). I use `brew` to install Scala and SBT but there’s no `brew formula` for Java. So I need to download the JDK from Oracle’s Java website and install it manually or there is a brew-friendly alternative that is ‘Homebrew Cask’.

So I can simply do

$ brew install caskroom/cask/brew-cask 
$ brew cask install java 

There is still an issue though. Whenever a new version of JDK is installed, `JAVA_HOME` changes. Thus I need to change the location of JDK set in my IDE which is IntelliJ IDEA in my case. This is so annoying. The reason for using brew is to make things automated, yet in terms of JDK installation, it’s still so cumbersome. I partially solved it by creating a symbolic link to the JDK so that I don’t need to change the location set in IDEA when a new version of JDK is installed. Although it got rid of unnecessary JDK setup in IDEA, I do still need to change the link to the JDK. Still annoying. So I was thinking about writing a shell script to create a symbolic link to the JDK. Then, all of a sudden, I came up with writing Scala script. I can do it using Scala as a script language. I can simply add `#!/usr/bin/env scala` to the beginning of the shell script file which actually contains Scala code. Or installing scalas using Conscript might be another more powerful way as it gives me a way to use sbt in the script file (This case, `#!/usr/bin/env scalas` should be used instead of `#!/usr/bin/env scala`). I ended up using `#!/usr/bin/env scala` to avoid any extra installation.

It might be easier and better if some changes are made on `brew cask Java formula` to create the link. However, I’ve been working as a Scala developer for about three/four months so wanted to try Scala as a script language. Besides, I’m not a Ruby programmer. 🙂 So here is the code.



See it on GitHub Gist

Click here to get the instructions to use it. <<==

I could have done a few things here using just Scala. For example, getting the list of directories, removing folder and so on. Yet, instead, I used `ProcessBuilder` as I wanted to use Scala to run OS processes. Just to try something I don’t usually do although I’ve done it before in Java in order to manipulate images using ImageMagick and to render HTML info PDF using wkhtmltopdf.

When running it, a result would be like this. (For more details, Click here!)

$ ln-s-jdk 8 

Version(s) found:

[0] jdk1.8.0_40.jdk
[1] jdk1.8.0_31.jdk
[2] jdk1.8.0_25.jdk
[c][/c] Cancel

Please enter a number on the list: 0

You chose 'jdk1.8.0_40.jdk'.
It will create a symbolic link to 'jdk1.8.0_40.jdk' (i.e. jdk8 -> jdk1.8.0_40.jdk) and may ask you to enter your password.
Would you like to proceed? (Yes / No) or (Y / N) y

/Library/Java/JavaVirtualMachines/jdk8: It is found so will be removed and recreated.

/Library/Java/JavaVirtualMachines $ sudo rm jdk8 
/Library/Java/JavaVirtualMachines $ sudo ln -s jdk1.8.0_40.jdk jdk8 
Password:

Done!

# Before
--------------------------------------
total 16
drwxr-xr-x  3 root  wheel  102  5 Nov 20:18 jdk1.7.0_71.jdk
drwxr-xr-x  2 root  wheel   68  3 Apr 22:20 jdk1.8.0_25.jdk
drwxr-xr-x  2 root  wheel   68  3 Apr 22:37 jdk1.8.0_31.jdk
drwxr-xr-x  3 root  wheel  102  7 Mar 01:30 jdk1.8.0_40.jdk
lrwxr-xr-x  1 root  wheel   15  3 Apr 18:08 jdk7 -> jdk1.7.0_71.jdk
lrwxr-xr-x  1 root  wheel   15  3 Apr 22:38 jdk8 -> jdk1.8.0_31.jdk

======================================

# After
--------------------------------------
total 16
drwxr-xr-x  3 root  wheel  102  5 Nov 20:18 jdk1.7.0_71.jdk
drwxr-xr-x  2 root  wheel   68  3 Apr 22:20 jdk1.8.0_25.jdk
drwxr-xr-x  2 root  wheel   68  3 Apr 22:37 jdk1.8.0_31.jdk
drwxr-xr-x  3 root  wheel  102  7 Mar 01:30 jdk1.8.0_40.jdk
lrwxr-xr-x  1 root  wheel   15  3 Apr 18:08 jdk7 -> jdk1.7.0_71.jdk
lrwxr-xr-x  1 root  wheel   15  3 Apr 22:44 jdk8 -> jdk1.8.0_40.jdk

======================================

Git: Push to / Pull from Both Github and Bitbucket

* Last Updated: 2013-08-26
* Fixed: Bug in the gitpullall() script. – 2013-07-23
* Refactored: gitpullall() – 2013-08-26
* Updated: gitpullall() – 2015-03-07: Now it can handle branches. So pulling on branch works. The old version always pulled from master. This version uses the name of current branch then get the same one from the remote.
(e.g. remote: github, local branch: some-fix <=pull= github/some-fix) * Added: gitpullall() for Z shell (zsh) - 2015-03-07

Both Github and Bitbucket are good SCM hosting services. For some reason, you probably want to migrate your project from Github to Bitbucket or vice versa. It’s easy because you just need to change the remote repository info. Sometimes, you want to keep your code in both places which means you want to push it to both Github and Bitbucket.

Pushing to both Github and Bitbucket is easy.

Here is a project cloned from Github. If you check out the remote repository info, it has only Github repo info.

$ git remote -v show 
origin  git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (fetch)
origin  git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (push)

The YOUR_PROJECT/.git/config file, of course, has only Github info.

[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git

To add a remote git repository on Bitbucket, use git remote set-url.

$ git remote set-url origin --add git@bitbucket.org:YOUR_USERNAME/your_project.git 

Now, it has Bitbucket info too.

$ git remote -v show 
origin  git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (fetch)
origin  git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (push)
origin  git@bitbucket.org:YOUR_USERNAME/your_project.git (push)

YOUR_PROJECT/.git/config

[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git
  url = git@bitbucket.org:YOUR_USERNAME/your_project.git

It’s enough for pushing to both Github and Bitbucket. So once it’s done, you can just do

$ git push 

Whenever you want to push to both.

If it doesn’t work, try git push -u origin master to set upstream.

$ git push -u origin master 

Next time, you can just do

$ git push 

e.g.)
Make some changes, Commit and Push

# Add all changes
$ git add -A

# Commit changes
$ git commit -m "added: .gitignore"
[master 0e649a6] added: .gitignore
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore

# Push to Github and Bitbucket
$ git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 286 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:YOUR_USERNAME/YOUR_PROJECT.git
   a24a46c..0e649a6  master -> master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 286 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: bb/acl: YOUR_USERNAME is allowed. accepted payload.
To git@bitbucket.org:YOUR_USERNAME/your_project.git
   a24a46c..0e649a6  master -> master

If what you need is only to push to Github and Bitbucket then you don’t need to read this post any further. However, if you also want to pull data from both Github and Bitbucket, it requires a little more extra steps.

Before I show the steps, I need to point out another case that you might want to know. With what I’ve done here, it fetches the data only from Github when running the git pull command. If what you want is only changing it to make it pull from Bitbucket, it’s simple. You just need to swap the positions of origin urls in the YOUR_PROJECT/.git/config file.

So change from this

[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git
  url = git@bitbucket.org:YOUR_USERNAME/your_project.git

to

[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = git@bitbucket.org:YOUR_USERNAME/your_project.git
  url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git

Now it pulls from Bitbucket instead of Github.

To pull from both Github and Bitbucket, first add the remote repository information using git remote.

$ git remote add github git@github.com:YOUR_USERNAME/YOUR_PROJECT.git 
$ git remote add bitbucket git@bitbucket.org:YOUR_USERNAME/your_project.git 

You can add both Github and Bitbucket or just the one that is not pulled when running git pull. It’s all up to you.
Now, the YOUR_PROJECT/.git/config file contains the information of the new remote repositories just added.

$ git remote -v show 
bitbucket git@bitbucket.org:YOUR_USERNAME/your_project.git (fetch)
bitbucket git@bitbucket.org:YOUR_USERNAME/your_project.git (push)
github  git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (fetch)
github  git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (push)
origin  git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (fetch)
origin  git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (push)
origin  git@bitbucket.org:YOUR_USERNAME/your_project.git (push)

YOUR_PROJECT/.git/config now has “github” and “bitbucket” as well as “origin”.

[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git
  url = git@bitbucket.org:YOUR_USERNAME/your_project.git

[remote "github"]
  url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git
  fetch = +refs/heads/*:refs/remotes/github/*
[remote "bitbucket"]
  url = git@bitbucket.org:YOUR_USERNAME/your_project.git
  fetch = +refs/heads/*:refs/remotes/bitbucket/*

If you run git pull, it still pulls from only Github so you need to also run git pull bitbucket master.

* pull from Github

$ git pull 

or

$ git pull github master 

* pull from Bitbucket

$ git pull bitbucket master 

If you want to pull it from all the remote repositories, you can use a function in shell script.

Add the following function to .bash_aliases or .bashrc or .profile depending on your OS (sorry Windows users).

~/.bash_aliases or ~/.bashrc or ~/.profile

* gitpullall() for Z shell (zsh) (Add the following function to ~/.zshrc)

Then you can simply run gitpullall to pull from all the remote repositories specified in the YOUR_PROJECT/.git/config file.

To ignore repositories when pulling, add the remote repository names to the repos_to_ignore variable in the gitpullall function above.

$ gitpullall 
running: git pull
Already up-to-date.

==================================
Remote: bitbucket
----------------------------------
running: git pull bitbucket master
From bitbucket.org:YOUR_USERNAME/your_project
 * branch            master     -> FETCH_HEAD
Already up-to-date.
==================================

==================================
Remote: github
----------------------------------
running: git pull github master
From github.com:YOUR_USERNAME/your_project
 * branch            master     -> FETCH_HEAD
Already up-to-date.
==================================

==================================
Remote: origin
----------------------------------
origin is on the ignored list so ignore it.
Ignored: origin
==================================

Pull other branch than master from the remote.

# Change branch

$ git checkout test-branch 
Switched to branch 'test-branch'
Your branch is up-to-date with 'origin/test-branch'.

# pull from all the remote repositories

$ gitpullall 
running: git pull
Already up-to-date.

==================================
Remote: bitbucket
----------------------------------
running: git pull bitbucket test-branch
From bitbucket.org:YOUR_USERNAME/your_project
 * branch            test-branch -> FETCH_HEAD
Already up-to-date.
==================================

==================================
Remote: github
----------------------------------
running: git pull github test-branch
From github.com:YOUR_USERNAME/your_project
 * branch            test-branch -> FETCH_HEAD
Already up-to-date.
==================================

==================================
Remote: origin
----------------------------------
origin is on the ignored list so ignore it.
Ignored: origin
==================================

* License for gitpullall (Opensource)

« Older posts

© 2026 Kevin's Blog

Theme by Anders Noren — Up ↑