Manage Directories

In the previous chapter, while explaining the Effective POM we saw how Maven configures the various directories of a Maven project. In this chapter, we show how to manage Maven directories.

Projects gain much by adapting to the Maven default directory layout. However, when migrating an existing project that is generated by an IDE or project that is using other build systems such as Apache Ant, you may wish to retain the existing directory structure and postpone the project restructuring to another day. In such circumstances, you may use build directory configurations to customize the directory layout.

Source Directories

A set of directory elements live in the build element of Super POM, which defines the directories for source, test, scripts and build files etc.,

  <build>  
    <directory>${project.basedir}/target</directory>  
    <outputDirectory>${project.build.directory}/classes</outputDirectory>  
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>  
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>  
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>  
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>  
   ...  
  </build>  
  • directory - sets the project build directory. The Maven property ${project.basedir} defaults to the top level directory of the project, so the build directory defaults to the target directory in project base dir.

    It also sets the property ${project.build.directory} to target directory and the next two output directories uses this property.

  • outputDirectory - directory to hold Java class files and it points to target/classes.

  • testOutputDirectory - directory to hold test class files and it points to target/test-classes.

  • sourceDirectory - project source code files folder and it points to src/main/java in project base dir.

  • scriptSourceDirectory - project script files folder and it points to src/main/scripts in project base dir.

  • testSourceDirectory - project test files folder and it points to src/test/java in project base dir.

 
 

Examples

We can override these elements in the project’s pom.xml and force Maven to adapt to a different directory structure.

To Mavenize a project that follows Eclipse IDE project structure and without any unit tests, add the following snippet to the build element of pom.xml.

pom.xml

  ...
  <build>
    <directory>${project.basedir}/bin</directory>
    <outputDirectory>${project.build.directory}</outputDirectory>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
  </build>
  ...

Above, we override default settings of directory, outputDirectory and sourceDirectory elements. With these settings, src is the source directory and bin is the project build and also, the output directory.

Next example is a project that uses Apache Ant and Ivy as its build system. The project uses src folder for source files, tests folder for test files and build as project build directory. The directory settings for this project is shown in next listing.

pom.xml

...
<build>
    <directory>${project.basedir}/build</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <testOutputDirectory>${project.build.directory}/tests</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
        <testSourceDirectory>${project.basedir}/tests</testSourceDirectory>
  </build>
...
</project>

With this configuration, the project can use either Ant or Maven without messing with each other.

 
 

Resources Directories

Similarly, a set of directory elements in Super POM defines directories for resources and test resources.

  …
  <build>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
   …
  </build>
   …

We can override them in the project pom.xml to adapt Maven to the existing structure.

Go with Standard

Our suggestion is to go with Maven project layout. Once you are comfortable with the Maven directory structure, it may not take much time to change project directory layout of an existing project. Purely as an ad-hoc measure, use the existing project structure, but restructure the project to Maven project layout as quickly as possible.

In the next chapter, we go through Maven Archetype Plugin we is useful to generate basic skeleton for different types of projects.