Friday, May 10, 2013

Maven Plugin Configuration

Configuring Maven plugins :


Maven plugins are configured broadly in two ways.
  1. Global configuration : This configuration applies to all the goals of a plugin.
  2. Execution specific configuration : Configuration is binded to a specific goals and phases.

Global Configuration :

To apply configuration for all the goals of a plugin follow the syntax shown in following XML. In this example we set compiler version to 1.6 (default is 1.5) which applies to all the goals of compiler plugin (such as compiler:compile, compiler:testCompile etc.)

Execution Specific Configuration :

In advanced build process we may require to execute a plugin with certain configurations for a certain execution. Say for the first execution we need to compile with 1.3 compiler version and for the second execution we need to compile with 1.5 compiler version. This is shown in following XML.

Another use case could be binding the configuration to specific goal and phase as shows below. In this case for antrun plugin configuration is binded to validation phase and run goal .


NOTE: Global Plugin configurations are overriden/merged by more specific execution plugin configuration

P.S. To find all the goals and default configuration of a plugin(say compiler plugin) type mvn help:describe -Dplugin=compiler -Ddetail .

Maven : The Project Management Tool

Apache Maven : 

Maven is a tool that can be used for building and managing any Java-based project. The central concept of Maven is POM, which is a revolution in itself. POM stands for Project Object Model. Maven is used to build project's, generate reports and documentations from a central piece of information.

In this tutorial we will go through a very basic example that covers dependency management using Maven.

Install Maven on Windows:

Follow this link install Maven on windows. 

http://maveninstall.blogspot.in/

Maven Coordinates : 

Most important thing in Maven are Coordinates. Maven Coordinates uniquely define any Project, dependency or plugin. These coordinates are used in a pom.xml. Below is a snippet of pom.xml that shows coordinates of a maven dependency.
 
 Every coordinate is identified by a unique set of groupId / artifactId / version .

Finding Coordinates : 

Not all but almost every library is listed on http://search.maven.org/#browse . This site has a nice search ability.

Build the project

  1. Open CLI (Command Line Interface) and type mvn archetype:generate. This will print many numbers. These numbers correspond to certain archetype. Maven provides a default number to choose. Though you are free to type in any number(in the printed range) we will choose default. For choosing default just press ENTER.
  2. Now choose version of the archetype or just press ENTER to accept default.
  3. Now enter groupID of your project, say com.mypackage, and press ENTER.
  4. Now enter artifactId, say helloMaven, and press ENTER
  5. Accept default version(1.0-SNAPSHOT), press ENTER.
  6. Accept default packge name or change it if you like, we will accept default which is com.mypackage
  7. At this point Maven will show the values of coordinates(groupID, artifactID, version, package name) and ask you to verify them. Verify and Press 'Y' . A Build success message is displayed. 
     
 By this time Maven did a couple of things for you.
  • Firstly Maven created a default project structure (using selected default archetype) for us. This is boiler plate kind of project which will print "Hello World". 
     



  • Secondly Maven created a directory under your HOME with name ".m2". This serves as a local repository of dependent Maven artifacts. By default Maven tries first to resolve the dependencies by looking into this repository, if artifact not found then it may look into maven central repository or your hosted repository as per the settings. Default POM generated in our "helloMaven" project contains dependency on junit. Checkout .m2 for this dependency under <HOME>/.m2/repository/junit/junit/3.8.1.  


Compile the project. 

  • Go to project location, i.e. path../to../helloMaven  . 
  • Type mvn compiler:compile. (use command line)
This command compiles the source code . Compilation can be done using javac also but we used maven since this a maven tutorial. But how does Maven did it. The answer is plugins. You might argue that we did not have compiler plugin in our pom.xml. You are partly right, our pom.xml does not explicitly declared any plugin but inherited the default plugin from Super pom. 

Type mvn help:effective-pom to view effective pom. From this we can confirm that Maven used maven-compiler-plugin (version 2.3.2).



NOTE: Every pom inherits from Super pom and the final pom thus generated is known as effective pom. 

Run the project:

  • Go to project location, i.e. path../to../helloMaven .
  • Type mvn exec:java -Dexec.mainClass="com.mypackage.App" .  Follow the following format <package-name>.<main-class-name>. This will run main class in our case "App". On running our main class it prints "Hello World!" on the console.






Cheers we have now built, compile and ran our first mavenized project.  



Monday, May 6, 2013

Install Apache Maven on Windows.


Installing Apache Maven on Windows.


To install maven you just need to set the environment variables after downloading and extracting maven's zip file as shown here.
  1. You need a Java Development Kit (JDK), the Java Runtime Environment (JRE) is not sufficient. Make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g. C:\Program Files\Java\jdk1.6.0 and that %JAVA_HOME%\bin is in your Path environment variable (see Fig. 1). 
  2. Download Maven zip file and extract it, e.g. D:\soffy\maven\apache-maven-3.0.4
  3. Add this Maven extracted location to the environment variables (see Fig. 1).


Fig. 1


Now test if you have configured the env. variables correct by typing mvn -version in the command prompt. If everything is ok you will see output like as in Fig. 2.


 
Fig. 2 


If you are through upto this point then Congrats you have installed Maven successfully and ready to dive into the world of Maven.

Building Android Project with Maven


This Tutorial will take you through a Step by Step approach for building an Android project using Maven. Though an IDE is not necessary but in this tutorial we will use Eclipse-Juno.

Requirements : 

Install the follwing plugins into the Eclipse-IDE.
  1. Install m2eclipse plugin. This plugin provides swift integration of Maven into the Eclipse IDE. Follow this link, which shows how to install m2e plugin into Eclipse http://eclipse.org/m2e/download/
  2. Install ADT (Android Development Toolkit) plugin. Follow this link for installation instructions. http://developer.android.com/sdk/installing/installing-adt.html 
  3. Install m2e-android plugin.  This adds maven support for Android Developer Tools and the Maven Android Plugin. It brings the power of Maven dependency management into the ADT. To install this plugin into Eclipse go to Help -> Eclipse Marketplace and search for "android m2e". 

Start a Maven based android project from scratch. 

In this approach we will start developing a maven based android project from the scratch.

  1. In eclipse start a new Maven Project.
  2. Click Next. Now a Select an Archetype dialog appears.
  3. Click on Add Archetype button. This prompts user with a Add Archetype dialog box. Fill in the Group Id as "de.akquinet.android.archetypes", Artifact Id as "android-quickstart" and Version as "1.0.8" . For now leave Repository URL blank. Click OK.
  4. Now select this Artifact from the list and click Next.
  5. In the dialog box fill in the Group Id (e.g. com.mycompany) and Artifact Id (e.g. myproject) for your project. Click Finish.
  6. Now our boilerplate mavenized android project is ready but it shows an error 'missing required src/test/java'. This is a bug in m2e-android plugin. To fix this issue, manually create folders test/java inside your src folder 
  7. Now run this project. This will show Hello <your project name>!. In our case this displays Hello myproject! 

Cheers .... We have successfully created and ran an Android App integrated with Maven. Now we can also use command line maven tools to resolve dependencies, deploy , run any android project also. 

Following above steps sets up a basic Android project integrated with Maven. Let us now examine generated pom.xml. 
  • First thing to notice is the packaging tag which is set to type apk.
  • Next important thing is to let maven compile your android app, maven must have Android SDK into its repository (local/central). In this example required android.jar (android sdk API level 10) is fetched from mavens central repository, which is set as dependency as following Maven coordinates com.google.android / android / 2.3.3 (Group Id / Artifact Id / version ).   
  • Another important tag is build plugin. Default POM generated already configured "android-maven-plugin" for you. This plugin is the heart of android maven integration. Inside this plugin, notice that platform tag is set to 10. This API level must be same as of used in dependency tag for Android SDK. (set 10 for 2.3.3 jar or set 16 for 4.1)