Maven and PHP

Apache Maven is a build automation tool with the purpose of building, reporting, and creating documentation of projects. It builds projects using a Project Object Model (POM) and a set of plugins shared by all projects using Maven, thus providing a uniform build system. Once you familiarize yourself with how one Maven project builds, you automatically know how all Maven projects build. This saves you an immense amount of time from trying to navigate the build process of many different projects.

Through this article you will gain familiarity with Maven for PHP, and how to install and use the PHP-Maven plugin from the command line and in Eclipse.

Install Maven

PHP-Maven uses the power of Maven for building, reporting, and creating documentation of your PHP projects. It adapts the Maven build life cycle to the PHP world while fully supporting PHP 5. PHP-Maven uses PHPUnit for unit testing and phpDocumentor for creating the project documentation.

Unpack the archive wherever you would like to store the binaries, and a folder named apache-maven-<version> will be created.

Add its bin folder to your PATH.

Make sure JAVA_HOME is set to the location of your JDK.

After you’ve completed the above steps, to test whether Maven is installed correctly or not you should run mvn --version at a command prompt.

Once Maven is successfully installed, go to the settings.xml file (found in ~/.m2 on Unix/Mac OS X and in C:Documents and Settingsusername.m2 on Windows) and add the PHP for Maven repository. If there is no settings.xml file, you must create it.

Create Your First Project

To create a simple project from the command line I’ll use the Maven Archetype Plugin. The Archetype Plugin allows a user to create a Maven project from an existing template called an archetype. You can run the Maven Archetype plugin with the mvn archetype:generate command to generate a new project from an archetype, in a folder corresponding to its artifact ID.

Maven will start downloading all the dependencies needed to your computer. At some point, Maven will ask you to define a value for groupId, artifactId, version and package, as you see here:

To execute a test you use the command mvn test. In the official documentation has a section How to Ignore Failing Tests, and you can find there some commands that may help you run your test. (The only command that actually worked for me was the one that executed a single test.)

To build your newly created Maven project, you run the command mvn:package. You will notice again that Maven automatically starts downloading any dependencies needed for your project.

After downloading all needed dependencies and performing the build actions, you should get the following success message:

To create documentation from the project, you’ll need the phpDocumentor 2 PEAR package. In the src/site folder, create the site.xml file with the following contents:

The PHPMaven update site is listed and you can choose the PHP-Maven option.

Hit Next/Finish to install the plugins.

After the installation you will find the PHP-Maven project option when you go to create a new project. Select it, and press Next in order to install the plugin.

To create a new project within Eclipse, select File > New > Other (or press the CTRL + N combination) and you should see something like in the image below:

After pressing the Next button you will receive a list of different archetypes.

Choose your artifact and you will be prompted for the project information, just like earlier on the command prompt.

The new project will be added to the Project Explorer tab. The folder structure is mostly similar to the one for the project created from command line.

After you create the project, right click on it and you’ll see the PHP-Maven option at the bottom of the menu, and its children list the most important phases of a PHP-Maven project.

Summary

In this article you’ve learned how to install and use the PHP-Maven plugin from the command line and in Eclipse. Maven for PHP is a capable build automation tool for the PHP platform. Using Maven, the user only needs to provide the configuration for the project, while the configurable plugins do the work of compiling the project, running unit tests, generating API documentation and so on. Maven for PHP will quickly become a necessary tool in the PHP developer’s toolbox.

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

http://incarnated.net John

A nice write-up Octavia, thanks.
I’ve used Ant and Phing in the past to do similar work, and Composer is almost the standard dependency management tool for PHP (it appears in nearly every PHPMaster tutorial now). I’m curious as to what the advantages and disadvantages would be of using PHP-Maven instead.

Pierre

+1 on John’s comment, given that with composer you have full dependency management, packagist is the counterpart to the Maven repository and I am not sure whether composer has the idea of lifecycles that Maven has, but they seem to overlap quite a bit.

Paritybit

This is the most stupidest Idea ever! Anybody who has used Maven on Java side will tell you the horrors and I can’t believe some body thought this would be cool idea (not your article)! Sorry but I work with maven every day and that is not a good thing! But it is a good well written article.

http://www.grafcaps.com Ben Harold

I’m confused. What does Maven actually do? I read the entire article and did a little Googling. It looks like it’s just another (proprietary) package manager with a wrapper for PHPUnit and phpDocumentor. Also, what PHP developer in their right mind uses Eclipse?