Rather than setting up the rules for building your project or artifacts in your IDE, you might opt to use a build sytem.  There are several for Java, but Gradle is the newest and is gaining popularity.  In COMP215 (or at least the recent years) the projects are setup using Gradle as the build system.

 

I have a fully working example of Word Count on Hadoop (same code as in Lab 1) setup here using Gradle.  If you follow the directions on Github you should be able to build and run the code on any recent Hadoop installation.  You can appropriate build.gradle file and use it in your code, but we'll also walk through it below, so it makes sense.

Here's the full file:

build.gradle
plugins {
  id 'java'
  id 'application'
}

mainClassName = "edu.comp330.lab1.WordCount"

repositories {
    mavenCentral();
}

dependencies {
    compile group: 'org.apache.hadoop', name: 'hadoop-client', version: '2.7.3'
}

jar {
    manifest {
        attributes 'Main-Class': "$mainClassName"
    }
}

 

So the top bit, 

plugins {
  id 'java'
  id 'application'
}

 

 

tells Gradle to use the java and application plugins.  java is for compiling Java code and application for compiling it into an executable JAR.   

 

mainClassName = "edu.comp330.lab1.WordCount"

 

sets the main class name for the executable JAR as edu.comp330.lab1.WordCount.  Note that the directory structure for the word count file is assumed to be:

 

src/
└── main
    └── java
        └── edu
            └── comp330
                └── lab1
                    └── WordCount.java

 

and your WordCount file must be in the edu.comp330.lab1 package, e.g. 

 

WordCount.java
package edu.comp330.lab1;
 
... rest of file ... 

 

Then you specify dependencies:

repositories {
    mavenCentral();
}

dependencies {
    compile group: 'org.apache.hadoop', name: 'hadoop-client', version: '2.7.3'
}

 

So it tells Gradle we need Hadoop Client 2.7.3 to compile the project.  All the dependencies are pulled in automatically.  It also tells Gradle to look in the Maven Central Repository for our dependencies.

 

We then include a jar directive

jar {
    manifest {
        attributes 'Main-Class': "$mainClassName"
    }
}

 

Which includes an attribute in the JAR's manifest that marks the main class to run when executed.  After all this, if you have Gradle installed ( if not go here ), then you can simply run:

 

path/to/directory/with/build.gradle$ gradle build jar

 

 

  • No labels