Compiling Application Sources

As mentioned in the introduction, at a very high level, you tell Maven what you need, in a declarative way, in order to accomplish the desired task. Before you issue the command to compile the application sources, note that this one simple command encompasses Maven’s four foundational principles:

  • Convention over configuration
  • Reuse of build logic
  • Declarative execution
  • Coherent organization of dependencies

These principles are ingrained in all aspects of Maven, but the following analysis of the simple compile command shows you the four principles in action and makes clear their fundamental importance in simplifying the development of a project.

Change to the <my-app> directory. The <my-app> directory is the base directory, ${basedir}, for the my-app project. Then, in one fell swoop, compile your application sources using the following command:

C:mvnbookmy-app> mvn compile 

After executing this command you should see output similar to the following:

INFO--------------------------------------------------
------------------
[INFO] Building Maven Quick Start Archetype
[INFO] task-segment: [compile]
[INFO]------------------------------------------------
-------------------
[INFO] artifact org.apache.maven.plugins:maven-resources-plugin:
checking for updates from central ...
[INFO] artifact org.apache.maven.plugins:maven-compiler-plugin:
checking for updates from central ...
[INFO] [resources:resources] ...
[INFO] [compiler:compile] Compiling 1 source file to
c:mvnbookmy-apptargetclasses
[INFO]------------------------------------------------
-------------------
[INFO] BUILD SUCCESSFUL
[INFO]------------------------------------------------
-------------------
[INFO] Total time: 3 minutes 54 seconds
[INFO] Finished at: Fri Sep 23 15:48:34 GMT-05:00 2005
[INFO] Final Memory: 2M/6M
[INFO]------------------------------------------------
-------------------

Now let’s dissect what actually happened and see where Maven’s four principles come into play with the execution of this seemingly simple command.

How did Maven know where to look for sources in order to compile them? And how did Maven know where to put the compiled classes? This is where Maven’s principle of “convention over configuration” comes into play. By default, application sources are placed in src/main/java. This default value (though not visible in the POM above) was, in fact, inherited from the Super POM. Even the simplest of POMs knows the default location for application sources. This means you don’t have to state this location at all in any of your POMs, if you use the default location for application sources. You can, of course, override this default location, but there is very little reason to do so. The same holds true for the location of the compiled classes which, by default, is target/classes.

What actually compiled the application sources? This is where Maven’s second principle of “reusable build logic” comes into play. The standard compiler plugin, along with its default configuration, is the tool used to compile your application sources. The same build logic encapsulated in the compiler plugin will be executed consistently across any number of projects.

Although you now know that the compiler plugin was used to compile the application sources, how was Maven able to decide to use the compiler plugin, in the first place? You might be guessing that there is some background process that maps a simple command to a particular plugin. In fact, there is a form of mapping and it is called Maven’s default build life cycle.

So, now you know how Maven finds application sources, what Maven uses to compile the application sources, and how Maven invokes the compiler plugin. The next question is, how was Maven able to retrieve the compiler plugin? After all, if you poke around the standard Maven installation, you won’t find the compiler plugin since it is not shipped with the Maven distribution. Instead, Maven downloads plugins as they are needed.

The first time you execute this (or any other) command, Maven will download all the plugins and related dependencies it needs to fulfill the command. From a clean installation of Maven this can take quite a while (in the output above, it took almost 4 minutes with a broadband connection).1 The next time you execute the same command again, because Maven already has what it needs, it won’t download anything new. Therefore, Maven will execute the command much quicker.

As you can see from the output, the compiled classes were placed in target/classes, which is specified by the standard directory layout. If you’re a keen observer you’ll notice that using the standard conventions makes the POM above very small, and eliminates the requirement for you to explicitly tell Maven where any of your sources are, or where your output should go. By following the standard Maven conventions you can get a lot done with very little effort.

Thank you for requesting a Maestro evaluation! This is our passion, and we want you to be successful. Please let us know how we may help!

Please enter your name, company email address and phone, and we will send you a link to your pre-built hosted evaluation within minutes.






I have read and agree to the Terms and Conditions.