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:

  1. organisation -> name revision

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

  1. play -> pdf 1.0

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

  1. commons-lang -> commons-lang 2.5

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

  1. 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:

  1. # Application dependencies
  2. require:
  3. - 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:

  1. # Application dependencies
  2. require:
  3. - play 1.2
  4. - com.google.guava -> guava r07

The ‘play dependencies’ command

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

  1. $ play dependencies
  2. ~ _ _
  3. ~ _ __ | | __ _ _ _| |
  4. ~ | '_ \| |/ _' | || |_|
  5. ~ | __/|_|\____|\__ (_)
  6. ~ |_| |__/
  7. ~
  8. ~ play! 1.2, http://www.playframework.org
  9. ~ framework ID is gbo
  10. ~
  11. ~ Resolving dependencies using ~/Desktop/scrapbook/coco/conf/dependencies.yml,
  12. ~
  13. ~ com.google.guava->guava r07 (from mavenCentral)
  14. ~ com.google.code.findbugs->jsr305 1.3.7 (from mavenCentral)
  15. ~
  16. ~ Downloading required dependencies,
  17. ~
  18. ~ downloaded http://repo1.maven.org/maven2/com/google/guava/guava/r07/guava-r07.jar
  19. ~ downloaded http://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/1.3.7/jsr305-1.3.7.jar
  20. ~
  21. ~ Installing resolved dependencies,
  22. ~
  23. ~ lib/guava-r07.jar
  24. ~ lib/jsr305-1.3.7.jar
  25. ~
  26. ~ Done!
  27. ~

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:

  1. # Application dependencies
  2. require:
  3. - play 1.2
  4. - com.google.guava -> guava r07:
  5. transitive: false

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

  1. # Application dependencies
  2. transitiveDependencies: false
  3. require:
  4. - play 1.2
  5. - com.google.guava -> guava r07

3. You can exclude any specific dependency explicitely:

  1. # Application dependencies
  2. require:
  3. - play 1.2
  4. - com.google.guava -> guava r07:
  5. exclude:
  6. - com.google.code.findbugs -> *

Keep lib/ and modules/ directory in sync

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

  1. $ play deps
  2. ~ _ _
  3. ~ _ __ | | __ _ _ _| |
  4. ~ | '_ \| |/ _' | || |_|
  5. ~ | __/|_|\____|\__ (_)
  6. ~ |_| |__/
  7. ~
  8. ~ play! 1.2, http://www.playframework.org
  9. ~ framework ID is gbo
  10. ~
  11. ~ Resolving dependencies using ~/Desktop/scrapbook/coco/conf/dependencies.yml,
  12. ~
  13. ~ com.google.guava->guava r07 (from mavenCentral)
  14. ~
  15. ~ Installing resolved dependencies,
  16. ~
  17. ~ lib/guava-r07.jar
  18. ~
  19. ~ ******************************************************************************************************************************
  20. ~ WARNING: Your lib/ and modules/ directories and not synced with current dependencies (use --sync to automatically delete them)
  21. ~
  22. ~ Unknown: ~/Desktop/scrapbook/coco/lib/jsr305-1.3.7.jar
  23. ~ ******************************************************************************************************************************
  24. ~
  25. ~ Done!
  26. ~

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:

  1. 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:

  1. # Application dependencies
  2. require:
  3. - play 1.2
  4. - com.google.guava -> guava r07:
  5. transitive: false
  6. - commons-lang 3.0

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

  1. play dependencies
  2. ~ _ _
  3. ~ _ __ | | __ _ _ _| |
  4. ~ | '_ \| |/ _' | || |_|
  5. ~ | __/|_|\____|\__ (_)
  6. ~ |_| |__/
  7. ~
  8. ~ play! 1.2, http://www.playframework.org
  9. ~ framework ID is gbo
  10. ~
  11. ~ Resolving dependencies using ~/Desktop/scrapbook/coco/conf/dependencies.yml,
  12. ~
  13. ~ com.google.guava->guava r07 (from mavenCentral)
  14. ~
  15. ~ Some dependencies have been evicted,
  16. ~
  17. ~ commons-lang 3.0 is overriden by commons-lang 2.5
  18. ~
  19. ~ Installing resolved dependencies,
  20. ~
  21. ~ lib/guava-r07.jar
  22. ~
  23. ~ Done!
  24. ~

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:

  1. # Application dependencies
  2. require:
  3. - play 1.2
  4. - com.google.guava -> guava r07:
  5. transitive: false
  6. - commons-lang 3.0:
  7. 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:

  1. # Application dependencies
  2. require:
  3. - play 1.2
  4. - com.google.guava -> guava r07:
  5. transitive: false
  6. - commons-lang 3.0:
  7. force: true
  8. - com.zenexity -> sso 1.0
  9. # My custom repositories
  10. repositories:
  11. - zenexity:
  12. type: http
  13. artifact: "http://www.zenexity.com/repo/[module]-[revision].[ext]"
  14. contains:
  15. - 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:

  1. # Application dependencies
  2. require:
  3. - play
  4. - play -> scala 0.8
  5. - org.jbpm -> jbpm-persistence-jpa 5.0.0:
  6. exclude:
  7. - javassist -> javassist *
  8. - org.hibernate -> hibernate-annotations *
  9. - javax.persistence -> persistence-api *
  10. repositories:
  11. - jboss:
  12. type: iBiblio
  13. root: "http://repository.jboss.org/nexus/content/groups/public-jboss/"
  14. contains:
  15. - org.jbpm -> *
  16. - 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. Sql Server系列:数据库组成及系统数据库

    1. 数据库组成 数据库的存储结构分为逻辑存储结构和物理存储结构. ◊ 逻辑存储结构:说明数据库是由哪些性质的信息所组成.SQL Server的数据库不仅仅只是数据的存储,所有与数据处理操作相关的信息 ...

  2. 轻量级前端MVVM框架avalon - 模型转换

    接上一章 ViewModel modelFactory工厂是如何加工用户定义的VM? 附源码 洋洋洒洒100多行内部是魔幻般的实现 1: function modelFactory(scope) { ...

  3. Vue.js实现checkbox的全选和反选

    小颖之前写的代码存在一个bug,就是当你选择全选的时候去掉后面的一个选项,再点全选结果就是反的了.很感谢博客园的朋友帮我改了这个问题嘻嘻,下面一起来看看具体是怎么实现的吧. 1.html <te ...

  4. ASP.NET MVC之分部视图和ChildAction(三)

    前言 上节我们已经非常清晰并且明确的讲了@Html.ActionLink的作用,这一节我们开始讲讲分部视图以及孩子Action. 话题 在C#中我们知道继承的目的是为了代码的复用,在Web应用程序同样 ...

  5. Android应用中实现系统“分享”接口

    在android下各种文件管理器中,我们选择一个文件,点击分享可以看到弹出一些app供我们选择,这个是android系统分享功能,我们做的app也可以出现在这个列表中. 第一步:在Manifest.x ...

  6. 使用nginx解决跨域问题(flask为例)

    背景 我们单位的架构是在api和js之间架构一个中间层(python编写),以实现后端渲染,登录状态判定,跨域转发api等功能.但是这样一个中间会使前端工程师的工作量乘上两倍,原本js可以直接ajax ...

  7. WCF 实体更改发布后,如何不影响调用方?

    应用场景:使用 WCF 有一个坏处,就是如果我们经常对 WCF 应用程序更新,有时候调用方也要进行 Update Service,但调用方往往会很多,那么这个工作就会很讨厌,比如 WCF Servic ...

  8. C/C++ 双精度double 数据相加出错缺陷解释

    不知道有没有人和我一样遇到过这样一个问题,请看下面代码. #include<iostream> using namespace std; int main(){ double a=2.3, ...

  9. C++ 连接数据库的入口和获取列数、数据

    这里不具体放出完整的程序,分享两个核心函数: 由于这里用到的函数是编译器自己的库所没有的,需要自己下载mysql.h库或者本地有数据库,可以去bin找到,放进去. 前提,我自己的测试数据库是WampS ...

  10. centos7 mysql数据库安装和配置

    一.系统环境 yum update升级以后的系统版本为 [root@yl-web yl]# cat /etc/redhat-release CentOS Linux release 7.1.1503 ...