Version 3

    This page is a place to get familiar with maven (specifically maven 2) and how it is used in JBoss.

     

    Maven is designed to work around a default project structure.  The default structure can be used as is, or it can be overridden by various configuration parameters.  The file pom.xml functions as the build configuration file.  A simple pom.xml file file looks something like the following:

     

    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
    

     

    -


     

    This pom configuration expects a directory structure similar to this:

     

    my-app
    |-- pom.xml
    `-- src
        |-- main
        |   `-- java
        |       `-- com
        |           `-- mycompany
        |               `-- app
        |                   `-- App.java
        `-- test
            `-- java
                `-- com
                    `-- mycompany
                        `-- app
                            `-- AppTest.java
    
    

     

    -


     

    These default directories are defined in the super pom which is the default parent to all poms (kind of like Object in Java).  The super pom can be seen here

     

    The super pom configuration can be overridden.  For example if we wanted the source directory to be located in src/main instead of src/main/java, we could change the simple pom like this:

     

    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <build>
         <sourceDirectory>src/main/java</sourceDirectory>
      </build>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
    

     

    The only change is the addition of the build section with the sourceDirectory tag.

     

    -


     

    If we want to build the project we use the command mvn.  This command is included with the maven zip file, and can be downloaded here.  The "mvn" command will normally not work by itself, because (unlike ant) maven does not have a default task.  So we have to specify which phase of the build lifecycle we would like to complete.  The phases are described in light detail here.

     

    If we want to just compile the code, the command mvn compile is used.

     

    If we want to compile, test, and generate packaged output (jar files) the command is mvn package

     

    If we also want to install these packaged files into our local repository, the command is mvn install.

     

    -