Importing Dependencies

Kitura is provided as a Swift package which is distributed by the Swift Package Manager (SPM).

This guide demonstrates how to add a package to your project to leverage the functionality that package provides.

Step 1: Add the URL to the Package.swift

To add a new dependency to our project we need to edit the Package.swift file:

open Package.swift

Next we need to add the package we want to use to our package dependencies:

.package(url: "<Link to github repo>", from: "<latest-version>"),

Note that from: “x.y.z” is shorthand for .upToNextMajor(from: “x.y.z”)), and will result in obtaining the most recent tag of the package with major version x.

Step 2: Add the package name to the target dependencies

Finally we need to add the package as a dependency of our application:

.target(
      name: "<project-name>",
      dependencies: ["Kitura", "<dependency-name>"]),

The resulting Package.swift should look as follows:

// swift-tools-version:5.0
import PackageDescription

let package = Package(
    name: "MyKituraApp",
    dependencies: [
        .package(url: "https://github.com/IBM-Swift/Kitura.git", from: "x.x.x"),
        .package(url: "<Link to github repo>", from: "<latest-version>"),
    ],
    targets: [
        .target(name: "MyKituraApp", dependencies: [ .target(name: "Application"), "Kitura"]),
        .target(name: "Application", dependencies: [ "Kitura", "<dependency-name>"]),
        .testTarget(name: "ApplicationTests" , dependencies: [.target(name: "Application"), "Kitura"])
    ]
)

Substitute "x.x.x" with the Kitura release you are using.

Step 3: Update the project to download new package

Now that we have added the new package to our project we need to update the project.

SPM provides a command line interface to do this.

So we will open a terminal and run the following:

swift package update

If you are on macOS and using Xcode you will also need to generate a new Xcode project by running the following:

swift package generate-xcodeproj

swift package update looks at our Package.swift file and checks to see if we've added any new packages.

It also checks to see if there are any new versions of our existing packages as well.

We have now added a new package to our project and are ready to start using this in our application.

Back to top