Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Apache BigTop Intro to Maven
Source of Material/Reference:
http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
http://wiki.apache.org/hadoop/HowToContribute
This is a brief intro into Maven before we develop the topic of building a Maven POM
for integration tests in the Bigtop convention.
Maven adopts a “convention” format for configuration. This convention is specified
in an archetype which defines a format for file structures, configuration files and
commands for building, testing, compiling; all the tasks which take source code into
production and tests releases.
There is a default archetype called the quickstart archetype which is the
“HelloWorld” example. The quickstart archetype describes a common convention
using a project name, a version, a groupID and an artifiactID.
To make sure Maven is installed correctly,
>mvn –version
Apache Bigtop requires Maven 3.X.X not 2.1.1 which is installed by default on some
of the Ubuntu AMIs.
In the steps below we will:
1) create a Maven project using the quickstart archetype
2) Configure a repository and download jars from there by adding configuration
parameters and commands to the pom.xml
3) Create a maven project for Hadoop Unit Tests
MacOS has maven installed automatically as part of the default OS configuration. If
you do not have Maven installed, install following instructions from a web search.
Create anew directory and generate a pom.xml file. A pom can be generated using a
maven command:
mvn archetype:generate
Maven will command line prompt for the following parameters:
 An archetype. maven-archetype-quickstart
 A version: default is 1.1
 groupId: com.company
 artifactID:my-app
 1.0-SNAPSHOT:
 package:
An archetype is a template which describes the project structure to maven . The
archetype defines the type of project you want to create which can do things like
create a directory structure for your code, download libraries from external urls,
integrate with other tools, etc….
The quickstart archetype needs 2 parameters, a groupId and artifactId. The groupId
by convention is the domain name of the org and the artifactId is the name of the
project you are working on. Maven will create a directory with the artifactId name.
Under the directory Maven will create a pom.xml file containing the configuration
information entered on the command line and a src subdirectory.
Note: for a Java project there are still pieces missing like a /lib directory to put in
downloaded jars in, etc…
Under the src dir are a main and test directory. Source code is placed under main
directory. Maven uses the groupId to create a subdirectory com/company
corresponding to the groupId under the main directory. Maven creates a App.java
file with a Hello World print statement in it. Maven also creates a unit test class
under test/java/com/company/AppTest.java using the names from the groupId.
Once the pom file and subdirectories with program stubs are created we are ready
to run the Java program. The quickstart archetype is defined to first create a JAR
then you run the program on the command line through the jar.
>mvn package
The package command creates a jar file under the target subdirectory.
To run the program:
> java –cp target/my-app-1.0-SNAPSHOT.jar com.company.App
where com.company.App is the path of the java source file under
src/main/java/com/company/App.java. Maven compiles the source file into a class
file and packages the class file under this class directory structure under the
target/classes subdirectory.
The online tutorial mentions running mvn site which creates subdirectories with
images and css files. Since this is not a web application we won’t cover going
through this step.
Adding JARS
Most useful Java programs require the installation of external JARS or additional
libraries the user has to download and install. Maven allows the developer to specify
the url of where to download these jars.
In the pom.xml file we created above the Junit library is listed as a dependency.
This tells Maven to download this jar into the local repository which is a
subdirectory on your local development machine. The default path for this Maven
repository is: ${user.home}/.m2/repository/
Under here you can find the Junit jars:
Once we understand the structure of where Maven puts files and the basic
configuration we can convert our Maven project into an Eclipse project and use
Eclipse to edit and add libraries to our project.
To convert our Maven project to an Eclipse project so we can edit our code inside
Eclipse and run the program without the verbose command line:
>mvn eclipse:eclipse
Maven creates a .classpath and .project files for Eclipse.
Go to Eclipse and select New Java Project>> and select the default location to be the
my-app folder created by Maven.
The project should import and look like the screen shot below in the Project
Explorer. The exclamation point is because Eclipse doesn’t know about the JUNIT
libraries needed for MyAppTest.java. Select the project and open and run the
MyApp.java program to make sure it works inside Eclipse.
To fix the JUNIT test, import the JUNIT JARS to the Eclipse project by adding a User
Library and linking the JAR to the copy under the Maven Repo. (note: not sure if this
is the best way to do this for the tutorial for BigTop. I do this for my own projects on
my local machine. Maybe a M2_REPO variable to make Eclipse search everything
under m2 is better? )
You will see the error messages disappear and running JUNIT Tests.