Simply Ian

The personal blog of Ian Macalinao

Configuring Gradle to Create an Uber JAR (Shade/Shadow)

| Comments

As a former Maven user, I was looking for a Maven Shade plugin-like software for Gradle. After searching for a bit, I found the Gradle shadow plugin.

Using this plugin is relatively easy. Add the following to your build.gradle:

1
2
3
4
5
6
7
8
9
10
11
12
13
apply plugin: 'shadow'

buildscript {
    repositories {
        ivy {
            url 'http://dl.bintray.com/content/johnrengelman/gradle-plugins'
        }
    }

    dependencies {
        classpath 'org.gradle.plugins:shadow:0.7'
    }
}

This gives you a shadow task in Gradle that creates an uber-JAR for you. To get behavior similar to the Maven Shade plugin, add the following:

1
2
3
4
5
6
7
shadow {
    artifactAttached = false
    outputFile = jar.archivePath
    artifactSet {
        exclude 'org.bukkit:bukkit'
    }
}

I’ll now explain this by section: * artifactAttached just asks if you want to not delete the original JAR. I set this to false, because I don’t care about the other JAR. * outputFile sets the path to the file that will be created. I used jar.archivePath so the created JAR has the same name as what the original would have had. * artifactSet declares what you want to exclude (or include) in your JAR. I specified org.bukkit:bukkit because I didn’t want Bukkit included in my created JAR.

So that’s it; it’s pretty similar to the Maven Shade plugin, except it’s Gradle.

How to Add MongoDB to a Gradle Project

| Comments

This is really simple. First, add the following dependency to your build.gradle:

1
compile group: 'org.mongodb', name: 'mongo-java-driver', version: '2.11.4'

Then, connect to the database like this:

1
MongoClient client = new MongoClient("localhost");

This client object is what lets you interact with the database.

Setting Up Virtual Hosts in Apache

| Comments

Today, I wanted to put my downloads on a different domain from my screenshots. Not wanting to manage multiple servers for no reason, I set up virtual hosts, also known as vhosts. Basically, depending on what domain you visit my web server from, you will get a different website. This is actually very simple to set up.

First, navigate to your apache2 directory and go to the sites-available directory within it. On my Debian system, this is at /etc/apache2/sites-available/. In this directory, you’ll see a bunch of files. Each one of these files is a config file that can be enabled or disabled individually; this is called a site.

To set up vhosting, you should first disable the default website. Use the command sudo a2dissite default to do this.

Next, add the rest of your websites. Here is the very simple config file I use:

1
2
3
4
5
6
7
8
9
<VirtualHost *:80>
    ServerName domain.you.want.to.use.com
    DocumentRoot /var/www/sitefiles/
    <Directory /var/www/sitefiles/>
        Options Indexes FollowSymLinks MultiViews
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Save this to a file with the name of the site, no extension. I named my site screenshots for example.

Obviously, replace domain.you.want.to.use.com with the domain you want to use for the website. (For my screenshot website, this is s.giza.us.) The document root is the folder that contains the files at the root of your website. For me that’s /var/www/screenshots.

Lastly, type sudo a2ensite sitename where sitename is whatever you named that file. Then, restart apache with sudo service apache2 restart, and all is well. For any additional domains, create more config files with that information, replacing all of the relevant stuff. Enjoy your awesome new vhosted website!

Including Dependencies in Your Gradle Build Script’s Classpath

| Comments

In a recent project, I wanted to use SnakeYAML in my Gradle build script. This is pretty easy to do; all you have to do is add the following to your script:

1
2
3
4
5
6
7
8
9
10
11
buildscript {
    repositories {
        mavenCentral()
        // ...etc
    }

    dependencies {
        classpath group: 'org.yaml', name: 'snakeyaml', version: '1.5'
        // ..etc
    }
}

This is the same as the repositories {} and dependencies {} sections of the build script. After doing this, feel free to use your libraries anywhere in your build script. Don’t forget to import the classes you use!

Adding a Provided JAR as a Dependency in a Gradle Project

| Comments

Adding a JAR as a dependency is simple in Gradle. In your dependencies {}, add the following line:

1
compile files('file.jar')

Where file.jar is the path to the JAR from the root directory of the repository. For example, if I had Dependency.jar at ./libs/Dependency.jar, I would use compile files('libs/Dependency.jar').

Up - a Useful File Sharing Tool Using SCP

| Comments

I created a tool today called up. Although it’s really simple, it gets the job done and it’s really useful.

Basically, to share a file, you just type up MyFile and it will upload that file to your remote server, returning the URL it is hosted at.

How to Run a Command on Startup on Linux

| Comments

I recently purchased a VPS to run my IRC client in. I wanted to start my IRC client in tmux on startup. The answer is simple: crontabs.

Run the following command on the user you want to run the command on:

1
crontab -e

This will open up the crontab of your current user. A crontab is basically a file stating a bunch of tasks that you want to run on some sort of schedule.

Next, add the following text to the file:

1
@reboot <command>

where <command> is, of course, the command you want to run. For example, on boot, I run a shell script that starts up my tmux stuff, so I have @reboot /usr/ian/tmux_start.sh.

Pretty simple, right? Crontabs have a lot more uses that you can probably find on Google.

Source: man cron and man crontab.

How to Change the Name of the JAR Produced in Gradle

| Comments

I don’t like the traditional JAR name assigned in Gradle. In Bukkit development, you usually make the JAR name the same as the plugin name. Here’s how you set the name of the JAR:

1
jar.baseName = 'JarName'

Where JarName would be the name of the jar generated in build/libs/.

Source: StackOverflow

How to Run a Specific Class Inside of a JAR

| Comments

My friend wanted me to test out his program, but it didn’t have a main class specified in the manifest. Here’s the solution:

1
java  -cp yourjar.jar com.yourpackage.YourClass

This will run the specified class as the main class.

Source: StackOverflow

Using Tmux Remotely Within a Local Tmux Session

| Comments

I often SSH to remote servers, and those servers usually have tmux installed. (tmux is better than Screen in every way) However, conflicts arise when you want to manipulate a remote tmux session within a local one. Ctrl-B refers to the local tmux session, not the remote one, and you have to press Ctrl-B twice to manipulate the remote one. This is pretty annoying. Fortunately, there is a solution to this.

There is one line you need to add to your ~/.tmux.conf (if this file doesn’t exist, create it):

1
bind-key -n C-a send-prefix

This binds the command send-prefix to Ctrl-A. Basically, you are sending a Ctrl-B (assuming you’ve left tmux at its defaults) directly to the server when you press Ctrl-A. This will let you manipulate the remote session with Ctrl-A and still use your local session with Ctrl-B. Pretty nice, eh?

Source: StackOverflow