Managing module dependencies

As of play 1.2, a new dependency mechanism has been introduced to make the developers life much easier. It is based on Apache Ivy and thus does not require you to have an own repository. You can use all the existing infrastructure and libraries which are provided by the existing maven repositories.

The source code of the example is available at examples/chapter5/dependencies.

Getting ready

After a new application has been created, you will find a conf/dependencies.yml file in your application. The only dependency is the Play framework itself by default.

How to do it...

In Chapter 2 the recipe Writing your own renderRSS method as controller output showed how to write out RSS feeds with the Rome library. Rome was downloaded back then and put into the lib/ folder. When searching for Rome on a Maven repository search engine, you will find an entry like http://mvnrepository.com/artifact/rome/rome/0.9. All you need to is to extend the existing configuration to:

require:
    - play
    - rome 0.9

And rerun play deps. When checking your lib/ directory you will see the Rome and JDOM jar files copied into it. JDOM is a dependency of Rome, so it is copied as well. If you remove the line with the Rome dependency again, you should run play deps --sync to make sure, both JAR files are removed again.

The next step is to put the actual dependency into your module instead of your application.

So create a conf/dependencies.yml in your module:

self: play -> depmodule 1.0

require:
    - rome 0.9
    - rome georss 0.1

Also note that the version of the module is set explicitly here to 1.0.

Now run play deps in the module directory to make sure the JAR files are put into the lib/ directory.

Put the module dependency in your application conf/dependencies.yml file:

require:
    - play
    - play -> depmodule 1.0

How it works...

As you have probably found out while reading the preceding paragraphs, the dependencies are again defined as a YML file, similar to the fixtures feature of Play. Furthermore, the definition of module dependencies is a little bit different when compared to the normal applications as it has to have a self: part defined first.

Generally, the syntax of the name of dependencies is similar to the one of Maven. A Maven package contains a groupId, an artifactId, and a version. A dependency is written in the form of:

- $groupId $artifactId $version

However, if groupId and artifactId are the same, one may be omitted, as seen with the preceding Rome example.

There's more...

This recipe is rather short, because you will get used to this mechanism pretty quickly and it is already well documented.

Learn more about dependency management with play

As there are more options available, you should definitely read the dependency documentation at http://www.playframework.org/documentation/1.2/dependency. In particular, the parts about versioning and transitive dependencies should be read in order to make sure not to override dependencies from the framework itself. Also, if you are interested in Apache Ivy, you can read more about it at http://ant.apache.org/ivy/

Search for jar files in Maven repositories

If you are searching for jar files in public repositories, you should use one of the following sites:

http://mvnrepository.com/

http://mavensearch.net/

http://www.jarvana.com/

http://search.mvnsearch.org/

http://www.mvnbrowser.com/ (also searches many other non-official Maven repositories)

See also

It is also very easy to create your own repositories for Play modules without creating any Maven repositories. See the recipe Adding private module repositories in this chapter for more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.29.22