RSS

Maven directory structure in a Java project

28 Abr

As we mentioned in the previous post Creating a new Java project with Maven , we are going to write about the basic directory structure that a project built with Maven has. As we said in An introduction to Maven, one of the main features of Maven is that follows the Convention over configuration pattern. This means that we have to place our classes, tests or resources in a predetermined directory so that Maven deals with them.

Mavenhas four source directory:

  • src/main/java : where we have to save our java source classes. Below this path we will place our classes in different packages.
  • src/main/resources : here we save our resources (xml and properties files, images, …). Any Spring or Hibernate configuration file has to be placed here too.
  • src/test/java : in this directory test classes should be saved. Here could it be our unitary JUnit tests.
  • src/test/resources : we save the test’s resources

We can see a simple Maven structure created in Eclipse:

Our created pom.xml file will have the following aspect:

<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>com.hop2croft.examples</groupId>
	<artifactId>example-post</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>EjemploMaven</name>
	<description>Ejemplo de proyecto con Maven</description>
</project>

The pom.xml file is almost empty, how knows Maven that our java and resources files are within the four directories previously mentioned?. The pom.xml file showed by the Eclipse editor isn’t the real pom file (called effective pom). Our pom.xml file inherits some properties from a parent pom (called super pom). In that parent pom, there are several standard properties used by anyMaven‘s project. We can see how it looks executing the next command from console.

mvn help:effective-pom

Fortunately, the Eclipse editor allows us to inspect the effective pom content. This effective pom contains the default dependencies, plugins or properties used by Maven. The next capture show us the build part of the pom file.

 
<build>
    <sourceDirectory>/Users/hop/example-post/src/main/java</sourceDirectory>
    <scriptSourceDirectory>/Users/hop/example-post/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>/Users/hop/example-post/src/test/java</testSourceDirectory>
    <outputDirectory>/Users/hop/example-post/target/classes</outputDirectory>
    <testOutputDirectory>/Users/hop/example-post/target/test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>/Users/hop/example-post/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>/Users/hop/example-post/src/test/resources</directory>
      </testResource>
    </testResources>
    <directory>/Users/hop/example-post/target</directory>
    <finalName>example-post-0.0.1-SNAPSHOT</finalName>
    <pluginManagement>
... 
</build>

If we look carefully we can realized where are defined the directories from where Maven will look for our project’s classes and resources. Besides, we can find the output directory (target directory) where our compiled classes will be saved.

We are going to use an Hibernate project in orden to show how project files are distributed. The next imagen shows us it.

The source folders have several packages where entities, daos or services are places. Tests classes are generated to check our dao classes. In the resources folder different Hibernate configuration files are saved.

Once we have built our project (and tests), we can see some folder within the target directory. Compiled classes will be generated (we can see it in the second image below). There is a test-classes folder where our compiled test classes will be placed. There are some more folders like surefire-reports one. Surefire is a Maven plugin that generates some reports in our project. Lastly we can observe that a jar file has been generated.

hop-e105bb50cb:Hibernate hop$ cd target/classes/
hop-e105bb50cb:classes hop$ ls -lah
total 16
drwxr-xr-x  6 hop  staff   204B 28 abr 23:03 .
drwxr-xr-x  9 hop  staff   306B 28 abr 23:03 ..
drwxr-xr-x  3 hop  staff   102B 28 abr 23:03 META-INF
drwxr-xr-x  3 hop  staff   102B 28 abr 23:03 com
-rw-r--r--  1 hop  staff   922B 28 abr 23:03 hibernate-jpa.cfg.xml
-rw-r--r--  1 hop  staff   842B 28 abr 23:03 hibernate.cfg.xml
hop-e105bb50cb:classes hop$ cd com/hibernate/
daos/         entities/     otros/        servicesImpl/ 
 
1 comentario

Publicado por en 28 abril, 2011 en Maven

 

Etiquetas: , ,

Una respuesta a “Maven directory structure in a Java project

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

 
A %d blogueros les gusta esto: