Contenuto principale

Publish New Version of Package

If you want to make updates to an existing package, you can edit the package and then publish a new version. To support compatibility, create a copy of the original package folder, update its contents and its version number, and then add the updated package to a repository for distribution.

Create New Working Copy of Package

Create a new working copy of the package root folder before making changes to the package. This copy becomes the root folder of the new version of your package. To help distinguish between package versions, consider updating the name of the folder. For example, if the original package root folder is named MyPackage@1.0.0 and your update is a minor update, then name the new package root folder as MyPackage@1.1.0.

The root folder of the new package version must not be in a package repository. If a package is in a repository, the package is no longer editable, and MATLAB® ignores any changes you make. If you want to install your package while still working on it, then install the package in authoring mode. If you specify the Authoring name-value argument as true, the mpminstall function installs the package in its current location rather than in the default installation area and makes the package editable. For example:

pkg = mpminstall("C:\MyCode\MyPackage@1.1.0",Authoring=true);

In authoring mode, the Editable property of the package is also set to true, so MATLAB registers changes to package metadata while you are working. The function returns a matlab.mpm.Package object for your package. While the package is in editable mode, you can modify the properties of this object and MATLAB updates the package definition file, mpackage.json, accordingly. If you edit the mpackage.json file directly, the MATLAB Package Manager updates the corresponding package object accordingly.

If you have a previous version of the package installed, additionally enable version replacement of already installed packages. If you specify the AllowVersionReplacement name-value argument as true, then mpminstall can replace any previously installed version of a package with the specified version so that only one version of the package is installed.

pkg = mpminstall("C:\MyCode\MyPackage@1.1.0",Authoring=true,AllowVersionReplacement=true);

Update Package Version Number

Update the package version number based on the magnitude of changes in your update. Package versions use semantic version syntax, which uses the format <major>.<minor>.<patch>. For more information on semantic version syntax, see Distinguish Between Packages Using Package Identifiers.

To update the package version, modify the Version property of the package. For example:

pkg.Version = "1.1.0"
pkg = 

  Package with properties:

   Package Definition
                     Name: "MyPackage"
              DisplayName: "My Package"
                  Version: 1.1.0 (1×1 Version)
                  Summary: ""
              Description: ""
                 Provider: "" (1×1 Provider)
                  Folders: [PackageApp    functions    objects] (1×3 PackageFolder)
             Dependencies: "MyDependency"
     ReleaseCompatibility: ""
              FormerNames: [1×0 string]
                       ID: "e6e98298-f0d9-4551-a1fd-1c841989c092"

   Package Installation
                Installed: 1
                 Editable: 1
        DirectlyInstalled: 1
              PackageRoot: "C:\MyCode\MyPackage@1.1.0"
    InstalledDependencies: "MyDependency@2.0.0"
      MissingDependencies: ""

   Repository
               Repository: [0×0 Repository]

  help MyPackage

Alternatively, you can manually edit the package definition file, mpackage.json, to update the version information. For example, update the version property in mpackage.json.

{
    "name": "MyPackage",
    "version": "1.1.0",
    "id": "e6e98298-f0d9-4551-a1fd-1c841989c092"
     ...
}

Update Package Content

Within the working copy of the package, you can modify, add, or remove files as usual. You can add code to a package by placing the files in the root folder or in subfolders. To add new subfolders to a package, use the addFolder function , and the MATLAB Package Manager updates the package object and the package definition file accordingly.

For example, create a subfolder in MyPackage named NewFolder and add it to the package.

mkdir("MyPackage@1.1.0/NewFolder")
addFolder(pkg,"NewFolder")

You can remove member folders from the package and its definition file by using the removeFolder function. The function removes the specified folders from mpackage.json but does not delete the folders or their contents. Subfolders in a package that are not included in the package definition file are not added to the path when the package is installed.

Package subfolders can be designated to support Java by setting the Languages property of the folder. For additional information, see addFolder. (since R2025a)

Update Existing Dependencies

When updating your package to a new version, you might need to update the dependency information. For instance if the new version of your package requires a specific version of a dependency, you can update the VersionRange property of that dependency in the package metadata.

For example, if you want your updated package to use version 2.0.0 or later of its dependency MyDependency, you can update the corresponding matlab.mpm.Dependency object that is associated with your package. First, get the dependency object from the package object.

dep = pkg.Dependencies
dep = 

  Dependency with properties:

               Name: "MyDependency"
       VersionRange: ""
                 ID: "d70a897a-c3b7-4e84-9e39-3df89a84b91e"
    ResolvedVersion: 2.0.0 (1×1 Version)

Set the VersionRange property of the dependency to ">=2.0.0" to require version 2.0.0 or later.

dep.VersionRange = ">=2.0.0"
dep = 

  Dependency with properties:

               Name: "MyDependency"
       VersionRange: ">=2.0.0"
                 ID: "d70a897a-c3b7-4e84-9e39-3df89a84b91e"
    ResolvedVersion: 2.0.0 (1×1 Version)

Then update the dependency in the package object.

pkg.Dependencies = dep;

Alternatively, you can update the version ranges of existing dependencies using the updateDependency function. Or, you can manually edit the package definition file, mpackage.json, to update dependency information. For example, update the dependencies property in mpackage.json.

"dependencies": [
        {
            "name": "MyDependency",
            "compatibleVersions": ">=2.0.0",
            "id": "d70a897a-c3b7-4e84-9e39-3df89a84b91e"
        }
]

Add or Remove Dependencies

You can add dependencies to a package using the addDependency function, and the MATLAB Package Manager updates the package object and the package definition file accordingly.

For example, add the package MyOtherPackage as a dependency of MyPackage.

addDependency(pkg,"MyOtherPackage")

You can remove dependencies from a package and its definition file by using the removeDependency function. The function removes dependencies from the package but does not uninstall them.

Change Package Name

You can change the name of a package as part of an update. If you want the new package to be discoverable by users of older versions that used the previous name, then add the old package name to the FormerNames property. When searching for packages using keywords with functions like mpmsearch, MATLAB also searches FormerNames for keywords.

For example, change the name of your updated package from "MyPackage" to "AstronomyPackage".

pkg.FormerNames = [pkg.FormerNames "MyPackage"];
pkg.Name = "AstronomyPackage";
pkg.DisplayName = "My Astronomy Package";

Update Package Version Number

The package version should be updated based on the magnitude of changes in the update. Package versions use semantic version syntax where versions are presented in the following format: <Major Version>.<Minor Version>.<Patch Version>. Consider what changes you are making and what type of update that is. For additional information on semantic version syntax, see Distinguish Between Packages Using Package Identifiers.

To update the package version you must modify the Version property of the package metadata. This can be done creating a matlab.mpm.Package object for the new package version and updating the Version property or by directly editing the Version property in the package definition file, mpackage.json.

pkg.Version = "1.1.0"
pkg = 

  Package with properties:

   Package Definition
                     Name: "MyPackage"
              DisplayName: "My Package Foo"
                  Version: 1.1.0 (1×1 Version)
                  Summary: ""
              Description: ""
                 Provider: "" (1×1 Provider)
                  Folders: [PackageApp    functions    objects] (1×3 PackageFolder)
             Dependencies: "MyOtherPackage"
     ReleaseCompatibility: ""
              FormerNames: [1×0 string]
                       ID: "e6e98298-f0d9-4551-a1fd-1c841989c092"

   Package Installation
                Installed: 1
                 Editable: 1
        DirectlyInstalled: 1
              PackageRoot: "C:\MyCode\MyPackage@1.1.0"
    InstalledDependencies: "MyOtherPackage@1.0.0"
      MissingDependencies: ""

   Repository
               Repository: [0×0 Repository]

  help MyPackage

Publish Package to Repository

Once your new version is ready for distribution, publish your package to a repository. End users can search for and install the package from the repository. When searching for a package using mpmsearch or installing a package using mpminstall, the MATLAB Package Manager finds in known repositories.

For example, publish your updated package to an existing repository.

copyfile("AstronomyPackage@1.1.0","C:\MyCode\MyRepository\AstronomyPackage@1.1.0")

For additional information on setting up and distributing packages through folder-based repositories, see Distribute Packages Using Folder-Based Repositories.

See Also

Objects

Functions

Topics