Maven

My 5 minute Maven startup

  • Set M2_HOME env varibale and set maven\bin into the path.
  • mvn —version
  • A simple maven goal: mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app
  • In the command "mvn archtype:create", archtype is a plugin and create is a goal. a plugin is a collection of goals with a general common purpose
  • artifactId This element indicates the unique base name of the primary artifact being generated by this project. The primary artifact for a project is typically a JAR file. Secondary artifacts like source bundles also use the artifactId as part of their final name. A typical artifact produced by Maven would have the form <artifactId>-<version>.<extension> (for example, myapp-1.0.jar).
  • To build a project : mvn package. Package is a phase in the life cycle (validate,compile,test,package,integration-test,verify,install,deploy,clean,site,…). Maven will execute every phase in the sequence up to and including the one defined. Do this inside my-app directory
  • Phases are actually mapped to underlying goals
  • Running the simple app: java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
  • mvn clean dependency:copy-dependencies package : This command will clean the project, copy dependencies, and package the project (executing all phases up to package, of course).
  • mvn clean : This will remove the target directory with all the build data before starting so that it is fresh.
  • mvn eclipse:eclipse : This can be run on a normal maven project and will make eclipse config files so that you can easily import it to eclipse. Make sure you have M2_REPO defined in your eclipse. Even if you see it as a library prefix but still you need to define it as a classpath variable.
  • mvn site : creates a site with project information under /target!
  • POM = Project Object Model

Definitions and Points

  • two main directory layouts for Maven are /src/main/ and /src/test/
  • A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation. The clean and package arguments are build phases while the dependency:copy-dependencies is a goal: mvn clean dependency:copy-dependencies package
  • List of plugins : http://maven.apache.org/plugins/

How to add a library dependency

Look for the library metadata in http://repo1.maven.org/maven2/ and add it as following to your pom:

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
        <scope>compile</scope>
    </dependency>

Simple web application

mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.mycompany.app -DartifactId=my-webapp

dependency to another eclipse project

If you need to use another eclipse project in your project it is a form of dependency as well.

    <dependency>
      <groupId>com.notes.common</groupId>
      <artifactId>common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

Putting this in our pom.xml we are using project "common". In eclipse it will go under required projects in build path.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License