Make delicious recipes!

Maven



Maven aims to promote common directory structure and conventions over software projects by providing sensible default behavior for projects.
If your project is setup according to Maven defaults, then your build file is practically non-existent!

Some default directories used in Maven are:
pom.xml ${basedir}/pom.xml
source ${basedir}/src/main/java
resource ${basedir}/src/main/resources
Tests ${basedir}/src/test
Test code ${basedir}/src/test/java
Test resources ${basedir}/src/test/resources
Compiled classes ${basedir}/target/classes
Distributable JAR file ${basedir}/target
(Maven assumes that a project will produce a JAR file).

So if everything is according to Maven defaults:
  1. Developers across different projects can expect same build structure across teams.

  2. Build files become non-existent.

  3. Testing becomes very easy. If all JUnit tests are put in default directory, mvn test runs each JUnit test automaticlly.



To see the default directory structure yourself, run the following command:
mvn archetype:generate \
-DgroupId=org.companyname.simple \
-DartifactId=simple \
-Dpackage=org.companyname.simple \
-Dversion=1.0-SNAPSHOT


This creates the following files and directories:
simple
simple/pom.xml
simple/src
simple/src/main
simple/src/main/java
simple/src/main/java/org
simple/src/main/java/org/companyname
simple/src/main/java/org/companyname/simple
simple/src/main/java/org/companyname/simple/App.java
simple/src/test
simple/src/test/java
simple/src/test/java/org
simple/src/test/java/org/companyname
simple/src/test/java/org/companyname/simple
simple/src/test/java/org/companyname/simple/AppTest.java


Note: This also produces some sample Java source and test files as follows:
// simple/src/main/java/org/companyname/simple/App.java

package org.companyname.simple;

/**
 * Hello world!
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}



// simple/src/test/java/org/companyname/simple/AppTest.java

package org.companyname.simple;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}



A POM (Project Object Model) file is also produced which is as follows:
simple/pom.xml
<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>org.companyname.simple</groupId>
  <artifactId>simple</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>simple</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>



Running the above program




To run, execute the command mvn install:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building simple 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ simple ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Work\mvn_projects\simple\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ simple ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ simple ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Work\mvn_projects\simple\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ simple ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ simple ---
[INFO] Surefire report directory: C:\Work\mvn_projects\simple\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.companyname.simple.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ simple ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ simple ---
[INFO] Installing C:\Work\mvn_projects\simple\target\simple-1.0-SNAPSHOT.jar to
C:\Users\SG\.m2\repository\org\companyname\simple\simple\1.0-SNAPSHOT\simple-1.0-SNAPSHOT.jar
[INFO] Installing C:\Work\mvn_projects\simple\pom.xml to
C:\Users\SG\.m2\repository\org\companyname\simple\simple\1.0-SNAPSHOT\simple-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.436s
[INFO] Finished at: Sat Oct 12 17:56:35 PDT 2013
[INFO] Final Memory: 9M/152M
[INFO] ------------------------------------------------------------------------



To convince yourself of the fact that everything is proper, you can run the Hello World program yourself:

/cygdrive/c/Program\ Files/Java/jdk1.7.0_17/jre/bin/java \
    -cp target/simple-1.0-SNAPSHOT.jar org.companyname.simple.App

=> Hello World!






Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal