Pages

Thursday, August 9, 2012

Maven building and compiling the first App

In the last post I described how to set up maven in linux environment. Now let's see how to create a project structure, compile, package and install.

First open a terminal, then make a directory called TestAppDir and go to the folder. 
  • Now type in the terminal:  mvn archetype:generate Note that when you run this command for the first time it will download the dependency jars and pom files. Now this command will show you a list. The list contains various architectures (spring apps, hibernate etc...).  Lets not input anything and press enter (This will generate a folder structure with default archetype).
  • Now you will be asked the following
Choose org.apache.maven.archetypes:maven-archetype-quickstart version: 
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1

Enter 6 (Latest version). If you run this for the first time this will download the dependencies as well.

Then you will be asked to fill the following


Define value for property 'groupId': : mad.maven.test
Define value for property 'artifactId': : MadTest
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  mad.maven.test: :
Confirm properties configuration:

the values mad.maven.test is given by me and it is like the package name in a normal program. The name I have given for the artifactId is MadTest. So when you package the app, it will  be like MadTest.jar. version refers to the version of the app you are creating. in the test package, you can write all the test cases. 

Now you will see a directory called MadTest in side TestAppDir. Now go to the MadTest directory. There you will see a pom.xml open it using a text editor. You will see something like below.


<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>mad.maven.test</groupId>
  <artifactId>MadTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MadTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

The group id, artifact id, version, name are given by you. Packaging tag indicates the type of file you will get after packaging the app. If you have war, it will package app in to a .war file. All the dependencies are mentioned inside dependency tags. By default maven will include junit as a dependency to run unit tests in the test directory within your app.

Now go back to the MadTest directory.  You can enter below commands


  • mvn compile : This will compile your code.
  • mvn test : This will run the unit tests in the app
  • mvn package : This will package your app in to a file you specified in the pom.xml file. In this case it is a .jar file.
Now let's say you need you put a logging code inside your source code, like

Logger logger = LoggerFactory.getLogger(App.class);
        logger.info("Hello World !!!");

Now to work this code you need slf4j jar as a library and import it in the code. 
put,
import org.slf4j.*;

Now with maven, you do not need to download manually and put it in the libraries. Instead go to 


and put slf4j in the search box. Then go to the SLF4J Api module link and choose the version you want and click on the version number. If you choose 1.6.6, you will see something like below in that page.

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>

put this code inside dependencies tags in your pom.xml.
Then in the terminal go to the MadTest directory and type mvn compile. Now the dependent slf4j will be automatically be downloaded at the compile time. Then type mvn package. Now you will see the jar file in the target folder.

Now if you type mvn install in the terminal from the same directory, the app will be available in your local maven repository.

No comments:

Post a Comment