Very often when I'm programming in Java, I need to see the dependencies for a particular jar file. If you are working in a project using a build tool like Maven or SBT, it's quite trivial to dump out a dependency tree of all the jars in the project. Outside of the project it's a pain, generally you have to throw together a quick project and then run your tool on it. As a workaround, I threw together a script that does that for you (and cleans up afterwards) Just copy the code into a file named `mvntree`. You should tweak the included repositories to reflect ones that you may need. To run it use:

mvntree groupId artifactId version


For example:

mvntree edu.ucar netcdf4 4.6.9


mvntree

#!/usr/bin/env bash

# Usage: mvntree groupId artifactId version

CUR_DIR=$(pwd)
MY_DIR="/tmp/trashme_one"

mkdir -p "$MY_DIR" && \
  cat > "$MY_DIR/pom.xml" <<EOL
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>DEPENDENCY</groupId>
   <artifactId>TREE.FOR</artifactId>
   <version>IS</version>
   <packaging>jar</packaging>
   <name>Maven Quick Start Archetype</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>$1</groupId>
         <artifactId>$2</artifactId>
         <version>$3</version>
      </dependency>
<!-- add here other dependencies -->
   </dependencies>
   <repositories>
     <repository>
       <snapshots>
         <enabled>false</enabled>
       </snapshots>
       <id>bintray-hohonuuli-maven</id>
       <name>bintray</name>
       <url>http://dl.bintray.com/hohonuuli/maven</url>
     </repository>
     <repository>
       <snapshots>
         <enabled>false</enabled>
       </snapshots>
       <id>ucar</id>
       <name>ucar</name>
       <url>http://artifacts.unidata.ucar.edu/content/repositories/unidata-releases/</url>
     </repository>
   </repositories>
</project>
EOL

cd "$MY_DIR" && \
mvn -e dependency:tree -U

cd "$CUR_DIR"
rm -rf $MY_DIR 

I'm calculating SHA-512 checksum's for video files that will be registered with a custom video asset manager that I wrote. Checksum's are useful fingerprints for identifying a file. In my case the checksums are useful for doing reverse look ups; e.g. if I have a file, I can look up it's metadata just by using it's checksum.

SHA-512 from Command Line

For my application, I can use the linux/mac util shasum when I register the file. In this case the command looks like:
shasum -a 512 -p /path/to/videofile.mp4
For a 40GB file, this takes about 160 seconds to run. For a 4 GB file, it takes 20 seconds.

SHA-512 in Java

However, I also need to calculate the checksum in a Java/Scala application. To do that I used the following script (written in scala)
#!/usr/bin/env scala

// Output matches the shasum -a 512 -p output

import java.io._
import java.math._
import java.security._


val file = new File(args(0))
val h = hash(file)
println(new BigInteger(1, h).toString(16)) // Dump out hash as hex


def hash(file: File): Array[Byte] = {
  val in = new BufferedInputStream(new FileInputStream(file))
  val digest = MessageDigest.getInstance("SHA-512");
  val buffer = Array.ofDim[Byte](1048576) // 1 MB. I tried 4MB and it was the same
  var sizeRead = -1
  var ok = true
  while(ok) {
    sizeRead = in.read(buffer)
    if (sizeRead == -1) ok = false
    else digest.update(buffer, 0, sizeRead)
  }
  in.close()
  
  digest.digest()
}
This takes slightly longer than the command line, but it's pretty close. A 40GB file takes about 185 seconds, a 4GB file takes about 21 seconds. Of course, that also includes the time to compile the code.
If you upgrade to the latest beta of iTerm (v3-ish), you will find that the iTerm support in TextMate is broken. i.e. Trying to launch an interactive shell from TextMate will fail. I wrote up a little bit of AppleScript to fix this issue.

Step 1

Open Script Editor and create a new doc. Paste the following code into it:

(* 

    Open Terminal Here 
     
    A toolbar script for Mac OS X 10.3/10.4
     
    Written by Brian Schlining
 *)


property debug : false

-- when the toolbar script icon is clicked 
-- 
on run argv
 set this_folder to item 1 of argv
 my process_item(this_folder)
end run



-- this subroutine processes does the actual work 
-- this version can handle this weirdo case: a folder named "te'st"ö te%s`t"

on process_item(this_item)
 
 set thePath to quoted form of POSIX path of this_item
 set theCmd to "cd " & thePath & ";clear;"
 
 tell application "iTerm"
  activate
  -- just open a terminal and cd to thePath
  --set newWindow to (create window with default profile)
  
  set aWindow to current window
  
  if aWindow is equal to missing value then
   set aWindow to (create window with default profile)
  else
   tell aWindow
    set t to (create tab with default profile)
   end tell
  end if
  
  
  tell current session of aWindow
   write text "cd " & thePath & ";clear;"
  end tell
  
 end tell
 
 
end process_item
Save the script somewhere as iTermLaunch.scpt.

Step 2

In TextMate, select "Bundles -> Edit Bundles" from the menu. Then select the "iTerm" bundle. Select Menu Actions in the iTerm bundle. Select "Interactive Shell" and edit the script like so (replace "/Path/to" with the real path to your iTermLaunch.scpt

#!/bin/bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"

# Start a new shell if needed, otherwise show the one with the right name.
#export SHELL_NAME=${SHELL_NAME:="TextMate Shell"}
#"$TM_BUNDLE_SUPPORT/new.sh"

osascript "/Path/to/iTermLaunch.scpt" ${TM_PROJECT_DIRECTORY}

Remember to save the bundle ([CMD]+S)

Step 3

There is no step 3. That's it. Just run that bundle item to open an iTerm shell in your current project.
I just got a notice that iTerm2 has new major upcoming release (v. 3). I upgraded but found that the AppleScript dialect that iTerm used has changed. This change broke my handy little script that launches iTerm from a Finder window. So I took a few minutes to update it. The new code for iTerm2 v3 is below. Just copy and paste it into a new ScriptEditor window and save as iTermHere.scpt. You can then export it as an Application from ScriptEditor and drag it to your Finder toolbar. Here's the code:

(* 

    Open Terminal Here 
     
    A toolbar script for Mac OS X 10.3/10.4
     
    Written by Brian Schlining
 *)


property debug : false

-- when the toolbar script icon is clicked 
-- 
on run
 tell application "Finder"
  try
   set this_folder to (the target of the front window) as alias
  on error
   set this_folder to startup disk
  end try
  
  my process_item(this_folder)
  
 end tell
end run


-- This handler processes folders dropped onto the toolbar script icon 
-- 
on open these_items
 repeat with i from 1 to the count of these_items
  set this_item to item i of these_items
  my process_item(this_item)
 end repeat
end open


-- this subroutine processes does the actual work 
-- this version can handle this weirdo case: a folder named "te'st"ö te%s`t"

on process_item(this_item)
 
 set thePath to quoted form of POSIX path of this_item
 set theCmd to "cd " & thePath & ";clear;"
 
 tell application "iTerm"
  activate
  -- just open a terminal and cd to thePath
  --set newWindow to (create window with default profile)
  
  set aWindow to current window
  
  if aWindow is equal to missing value then
   set aWindow to (create window with default profile)
  else
   tell aWindow
    set t to (create tab with default profile)
   end tell
  end if
  
  
  tell current session of aWindow
   write text "cd " & thePath & ";clear;"
  end tell
  
 end tell
 
 
end process_item