Swift Package Manager¶
The Swift Package Manager (SPM) is used for building your project's source code and dependencies. Since Vapor relies heavily on SPM, it's a good idea to understand the basics of how it works.
SPM is similar to Cocoapods, Ruby gems, and NPM. You can use SPM from the command line with commands like
swift build and
swift test or with compatible IDEs. However, unlike some other package managers, there is no central package index for SPM packages. SPM instead leverages URLs to Git repositories and versions dependencies using Git tags.
The first place SPM looks in your project is the package manifest. This should always be located in the root directory of your project and named
Take a look at this example Package manifest.
// swift-tools-version:5.2 import PackageDescription let package = Package( name: "app", platforms: [ .macOS(.v10_15) ], products: [ .executable(name: "Run", targets: ["Run"]), .library(name: "App", targets: ["App"]), ], dependencies: [ .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"), ], targets: [ .target(name: "App", dependencies: [.product(name: "Vapor", package: "vapor")]), .target(name: "Run", dependencies: ["App"]), .testTarget(name: "AppTests", dependencies: ["App"]) ] )
Each part of the manifest is explained in the following sections.
The very first line of a package manifest indicates the Swift tools version required. This specifies the minimum version of Swift that the package supports. The Package description API may also change between Swift versions, so this line ensures Swift will know how to parse your manifest.
The first argument to
Package is the package's name. If the package is public, you should use the last segment of the Git repo's URL as the name.
platforms array specifies which platforms this package supports. By specifying
.macOS(.v10_14) this package requires macOS Mojave or greater. When Xcode loads this project, it will automatically set the minimum deployment version to 10.14 so that you can use all available APIs.
Products are targets that your package produces when built. In this package, there are two targets. A library and an executable.
Dependencies are other SPM packages that your package relies on. All Vapor applications rely on the Vapor package, but you can add as many other dependencies as you want.
In the above example, you can see vapor/vapor version 4.0.0 or later is a dependency of this package. When you add a dependency to your package, you must next signal which targets depend on the newly available modules.
Targets are all of the modules, executables, and tests that your package contains. Most Vapor apps will have three targets, although you can add as many as you like to organize your code. Each target declares which modules it depends on. You must add module names here in order to import them in your code. A target can depend on other targets in your project or any modules exposed by packages you've added to the main dependencies array.
Executable targets (targets that contain a
main.swift file) cannot be imported by other modules.
This is why Vapor has both an
App and a
Any code you include in
App can be tested in the
Below is the typical folder structure for an SPM package.
. ├── Sources │ ├── App │ │ └── (Source code) │ └── Run │ └── main.swift ├── Tests │ └── AppTests └── Package.swift
.target corresponds to a folder in the
.testTarget corresponds to a folder in the
The first time you build your project, SPM will create a
Package.resolved file that stores the version of each dependency. The next time you build your project, these same versions will be used even if newer versions are available.
To update your dependencies, run
swift package update.
If you are using Xcode 11 or greater, changes to dependencies, targets, products, etc will happen automatically whenever the
Package.swift file is modified.
If you want to update to the latest dependencies, use File → Swift Packages → Update To Latest Swift Package Versions.
You may also want to add the
.swiftpm file to your
.gitignore. This is where Xcode will store your Xcode project configuration.