Bundler简介

来源:互联网 发布:男黑头知乎 编辑:程序博客网 时间:2024/06/16 06:46

最近接触一个ROR项目,在进入项目根目录安装依赖项的时候,简直就郁闷死了,一百多个依赖项,比如mail v2.3.3

虽然装了Rails v3.2.7,但是mail v.2.4.4的版本太高,不符合要求。

开始的时候我还手动的一个个的用gem安装,后来一边安装一边找了资料看看,原来完全不必这么麻烦。

使用bundler可以替我们解决这些烦恼。


进入到项目根目录,运行:

bundle intall

Bundler会根据gemfile自动的安装相关的依赖项。


安装bundle:

$ gem install bundler


Bundler manages an application's dependencies through its entire life across many machines systematically and repeatably.


Getting Started

Getting started with bundler is easy
$ gem install bundler
Specify your dependencies in a Gemfile in your project's root
source "http://rubygems.org"gem "nokogiri"gem "rack", "~>1.1"gem "rspec", :require => "spec"
Learn More: Gemfiles
Install all of the required gems from your specified sources
$ bundle install$ git add Gemfile Gemfile.lock
Learn More: bundle install
Make sure to add Gemfile.lock to your repository. This will ensure that other developers on your app, as well as your deployment environment, use exactly the same third-party code as you just installed.
Inside your app, load up the bundled environment
require "rubygems"require "bundler/setup"# require your gems as usualrequire "nokogiri"
Learn More: Bundler.setup
Run an executable that comes with a gem in your bundle
$ bundle exec rspec spec/models

In some cases, running executables without bundle exec may work, if the executable happens to be installed in your system and does not pull in any gems that conflict with your bundle.

However, this is unreliable and is the source of considerable pain. Even if it looks like it works, it may not work in the future or on another machine.

If you want a way to get a shortcut to gems in your bundle
$ bundle install --binstubs$ bin/rspec spec/models
The executables installed into bin are scoped to the bundle and will always work
Learn More: Executables

Using Bundler with Rails

Bundler works out of the box with Rails 3. Once you understand the basics of using bundler, you know everything you need to know.Using Bundler with Rails 3
Bundler works with Rails 2.3 with a small change to boot.rb and adding a preinitializer.rb.Using Bundler with Rails 2.3

Checking Out an Application With a Gemfile for Development

Install the required gems
$ bundle install
Learn More: bundle install
If your system stores its gems in a root-owned location (this is the default for Mac OSX), bundler will ask you for your password, so it can install the gems there.

Updating Your Dependencies

Make a change to your Gemfile
# changegem "nokogiri", "1.4.2"# togem "nokogiri", "1.4.3"
Install the new gems
$ bundle install

After making a change to your Gemfile, the next bundle install will try to update the gems in your snapshot (Gemfile.lock) without forcing an update to any of the other gems in your Gemfile.

This will usually work for simple dependencies, like nokogiri or sqlite3. On the other hand, updating Rails will usually require an update to some other component, because of the amount of dependencies it has.

If bundler reports a conflict, tell bundler to explicitly update the gem, but none of the other top-level dependencies (the ones in your Gemfile)
$ bundle update rails
Learn More: bundle update
In some rare cases, bundler will be unable to update the dependency without updating the top-level dependencies as well. In this case, tell bundler to update all dependencies
$ bundle update

Deploying Your Application

On production servers, you can enable deployment mode:
$ bundle install --deployment
Do not use this flag on development machines. The --deployment flag turns on defaults that are appropriate for a deployment environment. Gems are installed to vendor/bundle and the Gemfile.lockmust be checked in and up to date before Bundler is run.
Learn More: deploying

Digging Further

Store all of the required gems in your application. All future installs will get gems from this cache, bypassing rubygems.org
$ bundle package
Learn More: bundle package
Put dependencies in a group, so they can be ignored at install time or required together in your application
group :development do  gem "wirble"end
Learn more: Groups
Use a gem that is stored in git and has a .gemspec at its root. Bundler will make the executables available to bundle exec and compile C extensions
gem "nokogiri", :git =>  "git://github.com/tenderlove/nokogiri.git"
Learn more: Git
Use a gem that you are actively developing on your file system
gem "nokogiri", :path => "~/Code/nokogiri"
Install gems to an alternate location. By default, bundler installs your gems to the system location
$ bundle install --path vendor
When installing to a path, bundler completely isolates your gems from the system, guaranteeing you a clear set of dependencies.
Learn More: bundle install