Setting up your Ubuntu 16.04 LTS for Java Development

It is always good to idea to automate setup to speed up your development. In case you need to setup your computer from scratch it can be very painful and a long process taking days if not weeks. Your laptop might get broken, stolen or you might just want a fresh installation.

The script I have is optimised for myself but can easily be modified for yourself as well.

It first downloads the setup files for below applications:

  • IntelliJ Idea Community Edition IDE
  • Android Studio IDE
  • Studio-3t (formerly called robomongo)
  • xmind
  • zoom
  • exodus crypto coin wallet

Below is the script to download files off internet;

#!/usr/bin/env bash

function downloadIfNotExists {
    if [ -f "$1" ]
    then
    #0 = true
        echo "Already Exists : $1"
        return 0
    else
    # 1 = false
        echo "Downloading : $1"
        wget -P /opt/setup/ -U 'Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0' "$2"
        return 1
    fi
}
#!/usr/bin/env bash

#include functions.sh for utils
. functions.sh --source-only

#idea intellij
downloadIfNotExists /opt/setup/ideaIC-2018.1.1.tar.gz https://download.jetbrains.com/idea/ideaIC-2018.1.1.tar.gz
#android studio
downloadIfNotExists /opt/setup/android-studio-ide-173.4697961-linux.zip https://dl.google.com/dl/android/studio/ide-zips/3.1.1.0/android-studio-ide-173.4697961-linux.zip
#3t studio - robomongo
downloadIfNotExists /opt/setup/studio-3t-linux-x64.tar.gz https://download.studio3t.com/studio-3t/linux/2018.2.5/studio-3t-linux-x64.tar.gz
#xmind
downloadIfNotExists /opt/setup/xmind-8-update7-linux.zip https://www.xmind.net/xmind/downloads/xmind-8-update7-linux.zip
#zoom
downloadIfNotExists /opt/setup/zoom_amd64.deb https://zoom.us/client/latest/zoom_amd64.deb
#exodus
downloadIfNotExists /opt/setup/exodus-linux-x64-1.49.0.zip https://exodusbin.azureedge.net/releases/exodus-linux-x64-1.49.0.zip

 

Then it will install below programs from ubuntu repos:

  • VirtualBox
  • docker
  • docker-compose
  • chrome browser
  • dropbox
  • open-jdk-8
  • ubuntu-restricted-extras
  • htop
  • thunderbird
  • git
  • pinta
  • gparted
  • terminator
  • yakuake
  • sl
  • vlc
  • code2html
  • maven
  • openssh-server
  • xclip
  • toilet
  • figlet
  • cowsay
  • libav-tools
#!/usr/bin/env bash

#check if the running has root privileges
if [ "$EUID" -ne 0 ]
  then echo "Please run as root"
  exit
fi

USER="fmucar"

#download program setup files first
./download_programs.sh

chown -R $USER:$USER /opt

#add java_home
echo "JAVA_HOME=\"/usr/lib/jvm/java-8-openjdk-amd64/\"" >> /etc/environment
source /etc/environment



#virtualbox installation
apt-get install -y virtualbox virtualbox-ext-pack 

#docker
apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-get -y install docker-ce
    
#docker-compose
curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version

#systemctl status docker
usermod -aG docker $USER
docker info


#install zoom
dpkg -i /opt/setup/zoom_amd64.deb
sudo apt-get -f install


#install chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
apt-get update
apt-get -y install google-chrome-stable


#dropbox
sudo -H -u $USER bash -c 'cd ~ && wget -O - "https://www.dropbox.com/download?plat=lnx.x86_64" | tar xzf -'
sudo -H -u $USER bash -c '~/.dropbox-dist/dropboxd &'


#install new programs
sudo apt-get install openjdk-8-jdk
apt-get -y install ubuntu-restricted-extras
apt-get -y install htop
apt-get -y install thunderbird
apt-get -y install git
apt-get -y install pinta
apt-get -y install gparted
apt-get -y install terminator
apt-get -y install yakuake
apt-get -y install sl
apt-get -y install vlc
apt-get -y install code2html
apt-get -y install maven
apt-get -y install openssh-server
apt-get -y install xclip
apt-get -y install toilet
apt-get -y install figlet
apt-get -y install cowsay
apt-get -y install libav-tools

#idea extract
tar -xzvf /opt/setup/ideaIC-2018.1.1.tar.gz -C /opt/

#mongodb entry for /etc/hosts for docker mongodb
echo '127.0.0.1       mongodb' >> /etc/hosts

#android studio
apt-get -y install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz2-1.0:i386
unzip /opt/setup/android-studio-ide-173.4697961-linux.zip -d /opt/

#xminf - mind mapping software
mkdir -p /opt/xmind
unzip /opt/setup/xmind-8-update7-linux.zip -d /opt/xmind/

#exodus wallet
mkdir -p /opt/exodus
unzip /opt/setup/exodus-linux-x64-1.49.0.zip -d /opt/exodus/


#robomongo
mkdir -p /opt/studio-3t
tar -xzvf /opt/setup/studio-3t-linux-x64.tar.gz -C /opt/studio-3t
chmod +x /opt/studio-3t/studio-3t-linux-x64.sh
/opt/studio-3t/studio-3t-linux-x64.sh &


#change ownership to user for those programs
chown -R $USER:$USER /opt

#update existing programs first
apt-get update
apt-get -y upgrade


#.gradle.properties - for android signing apk release files
mkdir -p /home/$USER/.gradle
cat > /home/$USER/.gradle/gradle.properties << 'EOF'
RELEASE_STORE_FILE=/home/$USER/workspace/android/prod.keystore
RELEASE_STORE_PASSWORD=[put tore password here]
RELEASE_KEY_ALIAS=[put key alias here]
RELEASE_KEY_PASSWORD=[ut key password here]
EOF

chown -R $USER:$USER /home/$USER/.gradle

#create .m2 & github token files
sudo -H -u $USER bash -c 'mkdir -p /home/$USER/.m2/'
sudo -H -u $USER bash -c 'touch /home/$USER/.m2/github-token'
sudo -H -u $USER bash -c 'touch /home/$USER/.m2/settings.xml'
sudo -H -u $USER bash -c 'mkdir -p /home/$USER/.m2/'


cat > /home/$USER/.m2/github-token << 'EOF'
[place your github token here]
EOF

cat > /home/$USER/.m2/settings.xml << 'EOF'
[place your settings.xml content here]
EOF

#Setup SSH keys
sudo -H -u $USER bash -c 'mkdir -p /home/$USER/.ssh'

cat > /home/$USER/.ssh/id_rsa << 'EOF'
[place your private key here]
EOF

# Setup SSH public key
cat > /home/$USER/.ssh/id_rsa.pub << 'EOF'
[place your public key here]
EOF


#Update access rights for SSH keys
chown -R $USER:$USER /home/$USER/.ssh
chmod 0700 /home/$USER/.ssh
chmod 0644 /home/$USER/.ssh/id_rsa.pub
chmod 0600 /home/$USER/.ssh/id_rsa

chown -R $USER:$USER /home/$USER
chown -R $USER:$USER /opt/

#setup workspace and clone repos
./cloneGitRepos.sh

apt-get update
apt-get upgrade
apt-get -y autoremove

echo "FINISHED"

lsb_release -a

And finally, update the following file to clone your own repos

#!/usr/bin/env bash


#Workspace setup
git config --global user.email "[place your email here]"
git config --global user.name "[place your name here]"
git config --global push.default simple



#Cooldatasoft github - [Replace with your own repositories]
#you should have setup your ssh keys to get access to the repos
sudo -H -u $USER bash -c 'mkdir -p /home/$USER/workspace/cooldatasoft-github'
cd /home/$USER/workspace/cooldatasoft-github
sudo -H -u $USER bash -c 'git clone git@github.com:cooldatasoft/cds-parent.git'
sudo -H -u $USER bash -c 'git clone git@github.com:cooldatasoft/cds-build-tools.git'
sudo -H -u $USER bash -c 'git clone git@github.com:cooldatasoft/wicket-menu.git'
sudo -H -u $USER bash -c 'git clone git@github.com:cooldatasoft/object-sampler.git'
sudo -H -u $USER bash -c 'git clone git@github.com:cooldatasoft/cds-util.git'



chown -R $USER:$USER /home/$USER/

 

Github Repo for scripts

 

HttpUrl Parameter Encoding in Java

Some already know that there is URLEncoder class in java but not many know that this class is actually not for URL encoding but for form encoding if you have look at the javadocs. There are several other utility libraries as well doing this correctly like apache-commons but if you just need url encoding, it might be easier to use one utility class instead of loading a whole library.

 

public class URLParamEncoder {

    public static String encode(String input) {
        StringBuilder resultStr = new StringBuilder();
        for (char ch : input.toCharArray()) {
            if (isUnsafe(ch)) {
                resultStr.append('%');
                resultStr.append(toHex(ch / 16));
                resultStr.append(toHex(ch % 16));
            } else {
                resultStr.append(ch);
            }
        }
        return resultStr.toString();
    }

    private static char toHex(int ch) {
        return (char) (ch < 10 ? '0' + ch : 'A' + ch - 10);
    }

    private static boolean isUnsafe(char ch) {
        if (ch > 128 || ch < 0)
            return true;
        return " %$&+,/:;=?@<>#%".indexOf(ch) >= 0;
    }

}

click here for source