Refactor buildfile to work with subprojects.

Additionally, move most custom functionality out into
plugins.