Play’s dependency management system allows you to express your application’s external dependencies in a single dependencies.yml file.

A Play application can have three kinds of dependencies:

  • The Play framework itself, since a Play application always depends on the Play framework.
  • Any Java library, provided as JAR file installed in your application’s lib/ directory.
  • A Play module (in fact an application fragment) installed in your application’s modules/ directory.

Once you have expressed these dependencies in your application’s conf/dependencies.yml file, Play will resolve, download and install all required dependencies.

Dependency format

A dependency is described by an organisation a name and a revision number. In thedependencies.yml file you will write it like this:

organisation -> name revision

So, for instance version 1.0 of the Play PDF module is expressed like this:

play -> pdf 1.0

Sometimes the organisation name exactly matches the dependency name, as is the case forcommons-lang:

commons-lang -> commons-lang 2.5

In this case, you can omit the organisation from the dependency declaration:

commons-lang 2.5

Dynamic revisions

The revision can be fixed (1.2, for instance) or dynamic. A dynamic revision expresses a range of allowed revisions.

For example:

  • [1.0,2.0] matches all versions greater or equal to 1.0 and lower or equal to 2.0
  • [1.0,2.0[ matches all versions greater or equal to 1.0 and lower than 2.0
  • ]1.0,2.0] matches all versions greater than 1.0 and lower or equal to 2.0
  • ]1.0,2.0[ matches all versions greater than 1.0 and lower than 2.0
  • [1.0,) matches all versions greater or equal to 1.0
  • ]1.0,) matches all versions greater than 1.0
  • (,2.0] matches all versions lower or equal to 2.0
  • (,2.0[ matches all versions lower than 2.0

dependencies.yml

When you create a new Play application, a dependencies.yml descriptor is automatically created in the conf/ directory:

# Application dependencies

require:
- play 1.2

The require section list all dependencies needed by your application. Here the new application only depends of Play version 1.2. But let’s say your application needs Google Guava; you would have:

# Application dependencies

require:
- play 1.2
- com.google.guava -> guava r07

The ‘play dependencies’ command

To ask Play to resolve, download and install the new dependencies, run play dependencies:

$ play dependencies
~ _ _
~ _ __ | | __ _ _ _| |
~ | '_ \| |/ _' | || |_|
~ | __/|_|\____|\__ (_)
~ |_| |__/
~
~ play! 1.2, http://www.playframework.org
~ framework ID is gbo
~
~ Resolving dependencies using ~/Desktop/scrapbook/coco/conf/dependencies.yml,
~
~ com.google.guava->guava r07 (from mavenCentral)
~ com.google.code.findbugs->jsr305 1.3.7 (from mavenCentral)
~
~ Downloading required dependencies,
~
~ downloaded http://repo1.maven.org/maven2/com/google/guava/guava/r07/guava-r07.jar
~ downloaded http://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/1.3.7/jsr305-1.3.7.jar
~
~ Installing resolved dependencies,
~
~ lib/guava-r07.jar
~ lib/jsr305-1.3.7.jar
~
~ Done!
~

Now Play has downloaded two JARs (guava-r07.jar, jsr305-1.3.7.jar) from the central Maven repository, and installed them into the application lib/ directory.

Why two jars, since we only declared one dependency? Because Google Guava has a transitive dependency. In fact this dependency is not really required and we would like to exclude it.

Transitive dependencies

By default, any transitive dependencies are automatically retrieved. But there are several ways to exclude them if needed.

1. You can disable transitive dependencies for a particular dependency:

# Application dependencies

require:
- play 1.2
- com.google.guava -> guava r07:
transitive: false

2. You can disable transitive dependencies for the whole project:

# Application dependencies

transitiveDependencies: false    

require:
- play 1.2
- com.google.guava -> guava r07

3. You can exclude any specific dependency explicitely:

# Application dependencies

require:
- play 1.2
- com.google.guava -> guava r07:
exclude:
- com.google.code.findbugs -> *

Keep lib/ and modules/ directory in sync

Now if you run play dependencies again, the findbugs dependency will be omitted:

$ play deps
~ _ _
~ _ __ | | __ _ _ _| |
~ | '_ \| |/ _' | || |_|
~ | __/|_|\____|\__ (_)
~ |_| |__/
~
~ play! 1.2, http://www.playframework.org
~ framework ID is gbo
~
~ Resolving dependencies using ~/Desktop/scrapbook/coco/conf/dependencies.yml,
~
~ com.google.guava->guava r07 (from mavenCentral)
~
~ Installing resolved dependencies,
~
~ lib/guava-r07.jar
~
~ ******************************************************************************************************************************
~ WARNING: Your lib/ and modules/ directories and not synced with current dependencies (use --sync to automatically delete them)
~
~ Unknown: ~/Desktop/scrapbook/coco/lib/jsr305-1.3.7.jar
~ ******************************************************************************************************************************
~
~ Done!
~

However the jsr305-1.3.7.jar artifact downloaded before is still present in the application lib/ directory.

To keep the lib/ and modules/ directory synced with the dependency management system, you can specify the --sync command to the dependencies command:

play dependencies --sync

If you run this command again the unwanted jar will be deleted.

Conflict resolution

Whenever two components need different revisions of the same dependency, the conflicts manager will choose one. The default is to keep the latest revision and to evict the others.

But there is an exception. When a core dependency of Play framework itself is involved in a conflict, the version available in $PLAY/framework/lib is preferred. For instance, Play depends of commons-lang 2.5. If your application requires commons-lang 3.0:

# Application dependencies

require:
- play 1.2
- com.google.guava -> guava r07:
transitive: false
- commons-lang 3.0

Running play dependencies will evict commons-lang 3.0 even if this version is newer:

play dependencies
~ _ _
~ _ __ | | __ _ _ _| |
~ | '_ \| |/ _' | || |_|
~ | __/|_|\____|\__ (_)
~ |_| |__/
~
~ play! 1.2, http://www.playframework.org
~ framework ID is gbo
~
~ Resolving dependencies using ~/Desktop/scrapbook/coco/conf/dependencies.yml,
~
~ com.google.guava->guava r07 (from mavenCentral)
~
~ Some dependencies have been evicted,
~
~ commons-lang 3.0 is overriden by commons-lang 2.5
~
~ Installing resolved dependencies,
~
~ lib/guava-r07.jar
~
~ Done!
~

Also, note that dependencies already available in $PLAY/framework/lib will not be installed in your application’s lib/ directory.

Sometimes you want to force a specific dependency version, either to override a core dependency or to choose another revision that the latest version available.

So you can specify the force option on any dependency:

# Application dependencies

require:
- play 1.2
- com.google.guava -> guava r07:
transitive: false
- commons-lang 3.0:
force: true

Adding new repositories

By default, Play will search for JAR dependencies in the central Maven repository, and will search forPlay modules in the central Play modules repository.

You can, of course, specify new custom repositories in the repositories section:

# Application dependencies

require:
- play 1.2
- com.google.guava -> guava r07:
transitive: false
- commons-lang 3.0:
force: true
- com.zenexity -> sso 1.0 # My custom repositories
repositories: - zenexity:
type: http
artifact: "http://www.zenexity.com/repo/[module]-[revision].[ext]"
contains:
- com.zenexity -> *

Using this configuration all dependencies of the com.zenexity organisation will be retrieved and downloaded from a remote HTTP server.

Maven repositories

You can also add maven2-compatible repositories using the iBiblio type, like this:

# Application dependencies

require:
- play
- play -> scala 0.8
- org.jbpm -> jbpm-persistence-jpa 5.0.0:
exclude:
- javassist -> javassist *
- org.hibernate -> hibernate-annotations *
- javax.persistence -> persistence-api *
repositories:
- jboss:
type: iBiblio
root: "http://repository.jboss.org/nexus/content/groups/public-jboss/"
contains:
- org.jbpm -> *
- org.drools -> *

Continuing the discussion

Dependency management的更多相关文章

  1. Chapter 7. Dependency Management Basics 依赖管理基础

    This chapter introduces some of the basics of dependency management in Gradle. 7.1. What is dependen ...

  2. Spring mvc 4系列教程(二)——依赖管理(Dependency Management)和命名规范(Naming Conventions)

    依赖管理(Dependency Management)和命名规范(Naming Conventions) 依赖管理和依赖注入(dependency injection)是有区别的.为了将Spring的 ...

  3. Maven介绍---POM、Dependency Management、Coordinates

    Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建.报告和文档的软件项目管理工具. POM(Project Object Model,对象模型): 仅仅只是一个xml配置文 ...

  4. Yarn概述——FAST, RELIABLE, AND SECURE DEPENDENCY MANAGEMENT

    官网链接:https://yarnpkg.com/lang/en/ 特性 Ultra Fast. Yarn caches every package it downloads so it never ...

  5. 谈谈软件项目的dependency

    说到软件项目的依赖管理,可以从三个方面来考虑: 一.由build system控制的dependency 现在的build system,都支持一定程度上的dependency management, ...

  6. Maven错误Failed to read artifact descriptor for xxx:jar 和 missing artifact maven dependency

    可参考:http://stackoverflow.com/questions/6111408/maven2-missing-artifact-but-jars-are-in-place http:// ...

  7. Yeoman 学习笔记

    yoeman 简介:http://www.infoq.com/cn/news/2012/09/yeoman yeoman 官网: http://yeoman.io/ yeoman 是快速创建骨架应用程 ...

  8. Java资源大全中文版(Awesome最新版)

    Awesome系列的Java资源整理.awesome-java 就是akullpp发起维护的Java资源列表,内容包括:构建工具.数据库.框架.模板.安全.代码分析.日志.第三方库.书籍.Java 站 ...

  9. JavaScript资源大全中文版(Awesome最新版)

    Awesome系列的JavaScript资源整理.awesome-javascript是sorrycc发起维护的 JS 资源列表,内容包括:包管理器.加载器.测试框架.运行器.QA.MVC框架和库.模 ...

随机推荐

  1. Atitit.软件开发的几大规则,法则,与原则Principle v3

    Atitit.软件开发的几大规则,法则,与原则Principle  v31.1. 修改历史22. 设计模式六大原则22.1. 设计模式六大原则(1):单一职责原则22.2. 设计模式六大原则(2):里 ...

  2. iOS---iOS9搜索功能

    前言 在iOS9之前我们只能使用Spotlight来搜索应用名称来打开指定App,而其他的内容都是提供给系统使用(信息,联系人,邮件等).在iOS9以后Apple允许开发者设置应用中任意内容可以被Sp ...

  3. SQL Server数据库sql语句生成器(SqlDataToScript)的使用(sql server自增列(id)插入固定值)

    SqlDataToScript是根据表数据进行生成 Insert Into语句,此工具还有一个好处是可以对自增列插入固定值,例如:自增的列id值为5,但是5这个行值已经删除,如果想存储Id自增列值为5 ...

  4. Spring学习记录(三)---bean自动装配autowire

    Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系,少写几个ref autowire: no ---默认情况,不自动装配,通过ref手动引用 byName---根据 ...

  5. OPENVPN+MYSQL认证+客户端配置

    安装环境:ubuntu 12.04 x64 一 服务器端 1.安装openvpn及相应包 1 2 root@jkb:~# aptitude install openvpn root@jkb:~# ap ...

  6. [汇编与C语言关系]5. volatile限定符

    现在研究一下编译器优化会对生成的指令产生什么影响,在此基础上介绍C语言的volatile限定符.首先看下面的C程序: /* artificial device registers */ unsigne ...

  7. easyui-datagrid 列单击事件

    首先要注意的就是,先添加一个js方法,名字可以自定义,但是必须得与下面的option里面的onClickRow:后面的一致即可      <script type="text/java ...

  8. 一张H5游戏页引起的思考

    最近开发了一个移动端的端午活动页面,做完后就想写点东西总结一下,感受最深的就是打草稿. 刚开始并没有打草稿,直接开干,越做到后面就越觉得代码很乱很杂,非常不舒服,做到哪个页面写这个页面的CSS,没有大 ...

  9. 解析C#类中的构造函数

    <解析C#类中的构造函数> 一.  C#中的构造函数概述: C#中类包含数据成员和函数成员.函数成员提供了操作类中数据的某些功能,包括方法.属性.构造器和终结器.运算符和索引器. 构造函数 ...

  10. 浅谈2D游戏设计模式--游戏剧情设计(1)

    博主不才,人生有2大爱好,写程序和玩游戏,本人玩的又是一款2D的在旁人看来弱智的网络游戏. 这款游戏在中国的名称叫做冒险岛,不知道园子里有没有人玩过. 我打算有空的话,就把我玩游戏中的心得和程序结合起 ...