Monday, April 15, 2013

let's start with maven

it won't be another post about...

I don't want to writing about what maven is and what is not. I think that quote from maven's guide will be a perfect description:
"Maven is essentially a project management and comprehension tool",
but if you still need to know more about what this tool is, here you have good link for start.

It won't be also tutorial with steps "how to install maven". If you don't have this tool yet, there are plenty links which will guides you through this process. But just for saving your time - here you have one which is describing installation on Ubuntu and here is one for Windows.

So what it will be about? Short introduction to maven basics, the most useful functions. If you're not a pro and you want to start your journey with this tool I believe that article will be good start for you:)

don't waste time - let's start funny part

After installation is done and you got well prepared environment we finally can start with creating our first project.
To do it, we need to use Maven's archetype mechanism, which is no more than a template of a project (if you want to read more about archetypes - here's a source :).

To create new project you need to execute following command:
mvn archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DgroupId={groupId} \
  -DartifactId={artifactId}

Ok, what happened, what we have done and what those magic parameters are?
  • The part archetype:generate defines which plugin will be executed and which goal needs to be achieved. Overall pattern looks like this:
    mvn [plugin-name]:[goal-name]
    
  • DarchetypeGroupId determines which archetype you will use. Full list is below this link.
  • {groupId} is the namespaced group where this project should live.
  • {artifactId} is the name of your plugin.

And what happend?
Let's look at the directory where you executed a command. As you can notice, there is new directory (name is the same as given as artifactId). Let's go there:)
Inside this ${artifactId} directory you can see one file - pom.xml, and next directory, which is called src. I will write more about pom.xml file in next post, so just leave it for now.

Under src directory we have following structure:
${artifactId}
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java
This is a Maven convention and if you want to learn more about it you can read Introduction to the Standard Directory Layout.

Ok, that's all about creating new project. Let's go further.

what other command we need on daily basis?

There are also a few other commands which you will use really often. In opposite of first command, which I already described, I placed the rest of them in one paragraph, because after initial explanation there will be not so much to write about them:)

  • To compile your project you need to execute:
    mvn compile
    
    Compiled classes were placed in ${artifactId}/target/classes

  • To run tests you need to execute:
    mvn test
    
    To be honest, execution above is both: compile and execute.
    If you want only to compile test is enough to execute:
    mvn test-compile
    

  • If you want to create project package you need to execute:
    mvn package
    

  • To install artifacts to the local repository:
    mvn install
    

  • Build project site and place it in ${artifactId}/target/site:
    mvn site
    

  • Remove target with all the build data before starting so that it is fresh:
    mvn clean
    

An interesting thing to note is that phases and goals may be executed in sequence eg:
mvn clean dependency:copy-dependecies package

a few useful links

I'm using Eclipse, that's why below you got few useful links, which will help you to start using eclipse with maven.
And here you have also really useful link to Maven Repository.

No comments:

Post a Comment