Sub topic 4
Generating a manifest from source or metadata names
You can write a package.xml by hand. On a real project, though, you will usually generate it. This chapter covers sf project generate manifest, its three input modes, and the limits of each. It assumes you know the package.xml structure from earlier chapters (types, members, name, version) and have a Salesforce DX project with a force-app directory.
Why generate a manifest
A hand-written manifest has two weaknesses. You can misspell a member name, and you can forget a component. A misspelled member in a retrieve or deploy fails or silently selects nothing. Generating the file from source or from a list of names removes most typing mistakes.
The command is sf project generate manifest. It writes an XML file in the same format as any other package.xml. You can open it, edit it, commit it, and pass it to --manifest later.
The three input modes
The command builds its list of types and members from one of three inputs.
| Input | Flag | What it reads | Typical use |
|---|---|---|---|
| Local source | `--source-dir` | Metadata files in the directories you name | Deploy or retrieve everything in a package directory |
| Metadata names | `--metadata` | The names you type, in `Type` or `Type:Member` form | Select a few components without opening the source |
| An org | `--from-org` | The metadata that exists in that org | Capture what an org contains before a retrieve |
You must supply exactly one source of input. The sections below cover each mode.
Generating from source
Pass one or more source directories with --source-dir. The CLI reads every metadata file under those directories and writes one member per component it finds.
sf project generate manifest \
--source-dir force-app \
--output-dir manifest \
--name package.xmlHere --output-dir is the folder to write into and --name is the file name. The spine's earlier example puts the path inside --name. Splitting it into --output-dir and --name is the safer form, because each flag then does one job. If the manifest folder does not exist, check the result after the first run.
For a project with one Apex class and one custom field, the output looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>AccountService</members>
<name>ApexClass</name>
</types>
<types>
<members>Account.Region__c</members>
<name>CustomField</name>
</types>
<version>62.0</version>
</Package>Three details matter here.
- Fields appear as
Object.Field. The CLI reads the field file underobjects/Account/fields/and writes the member in the dotted form. You do not have to compose it. - The version comes from your project. The CLI uses
sourceApiVersionfromsfdx-project.json. You can override it with--api-version. - Types are grouped for you. Each metadata type appears once, with all its members listed under it.
You can pass several directories. The flag accepts a repeated value or a comma-separated list:
sf project generate manifest \
--source-dir force-app/main/default/classes \
--source-dir force-app/main/default/triggers \
--output-dir manifest \
--name apex-only.xmlThis is a convenient way to build a manifest that covers only the code in a project, which is the usual shape for a code-only deployment.
What source generation cannot do
The CLI can only list what exists on disk. A component that is already deleted from your project has no file, so it never appears. This makes --source-dir the wrong tool for a destructive changes file. If you removed LegacyAccountService from the repository last week, no generated manifest will name it. For deletions you need the next mode, where you give the names yourself.
Generating from metadata names
With --metadata, you state the components directly. Each value is either a whole type or a Type:Member pair.
sf project generate manifest \
--metadata ApexClass:AccountService \
--metadata ApexClass:AccountServiceTest \
--metadata CustomField:Account.Region__c \
--output-dir manifest \
--name package.xmlThe result matches the manifest in the first code example of this tutorial's plan. Note how the field member is written in full as CustomField:Account.Region__c. The part before the first colon is the metadata type. Everything after it is the member name, dots included.
A bare type name selects every component of that type:
sf project generate manifest \
--metadata ApexClass \
--output-dir manifest \
--name all-classes.xmlThe output holds a * member under ApexClass. This is the wildcard form from the chapter on member naming, so it is valid for retrieve and deploy manifests only. It is not valid in a destructive changes file.
Using metadata names for a destructive file
Because you type the names, this mode suits deletions. You can name components that no longer exist locally:
sf project generate manifest \
--metadata ApexClass:LegacyAccountService \
--metadata CustomField:Account.OldRegion__c \
--output-dir manifest \
--name destructiveChangesPost.xmlThe command writes the same XML structure as a package.xml, which is exactly the format a destructive changes file uses. Two cautions apply:
- A generated file may carry a
<version>element. Destructive files accept it, so you can leave it. - The CLI does not check that the names you typed exist in any org. A typo produces a well-formed file that fails at deploy time.
The command also has a --type flag with values pre, post, destructive, and package. It is meant to produce manifests for destructive files. Its exact output can differ between CLI versions, so run sf project generate manifest --help and inspect the file it writes before you rely on it.
Generating from an org
With --from-org, the CLI lists the metadata in the named org and writes a manifest of it.
sf project generate manifest \
--from-org my-sandbox \
--output-dir manifest \
--name org-contents.xmlThis helps when you do not know what a sandbox holds, or when you want a starting point for a full retrieve. The trade-offs are practical ones.
- The manifest can be very large in a mature org.
- Packaged components are left out by default. The
--include-packagesflag, with values such asunlockedandmanaged, adds them. - The file reflects the org at the moment you ran the command. It does not update.
Treat this output as a draft. Remove types you do not need before you use it for a deployment.
Which mode to choose
| Situation | Best mode | Reason |
|---|---|---|
| Deploy everything in the project | `--source-dir` | The list always matches the files on disk |
| Deploy or retrieve three known components | `--metadata` | Short and explicit |
| Delete components | `--metadata` | The components are no longer in source |
| Explore an unfamiliar sandbox | `--from-org` | Shows what exists |
| Repeatable CI deployment | `--source-dir`, committed | Reviewable in version control |
Using the generated file
The generated file is an ordinary manifest. Pass it to retrieve or deploy with --manifest:
sf project retrieve start \
--manifest manifest/package.xml \
--target-org my-sandboxsf project deploy start \
--manifest manifest/package.xml \
--target-org my-sandboxThe next chapter covers these commands in detail. For now, note that the manifest is the bridge between "what I want" and "what the CLI moves".
Common errors
- The file lands in the wrong folder. Without
--output-dir, the command writes to the current directory. Run it from the project root, or set the flag. - The file is overwritten. Running the command again with the same name replaces the earlier file. Commit hand-edited manifests first, or use a new name.
- Components are missing. Source generation only sees the directories you list. A component outside
--source-diris not in the manifest. - An unexpected API version. The version follows
sourceApiVersion. If a retrieve fails on a type that needs a newer version, raise the value insfdx-project.jsonor pass--api-version. - A type name with the wrong case or spelling.
--metadata apexclassdoes not matchApexClass. Metadata type names are case sensitive. - Names that contain spaces or special characters. Quote the value in your shell so it reaches the CLI as one argument.
Exercise
You deleted the Apex class OldBilling from your repository last month, and the class still exists in production. You want a file to feed into a destructive deployment. Why does sf project generate manifest --source-dir force-app fail to help, and which command would you run instead?
Show answer
The --source-dir mode reads files on disk, and OldBilling no longer has a file, so it never appears in the output. Use the --metadata mode, which takes names you type, for example: sf project generate manifest --metadata ApexClass:OldBilling --output-dir manifest --name destructiveChangesPost.xml. Then read the file to confirm the member name matches the component in the org.
Exercise
A teammate runs sf project generate manifest --metadata ApexClass --output-dir manifest --name destructiveChangesPost.xml and deploys it as a post destructive file. What is wrong with this approach?
Show answer
A bare type name produces a wildcard member (*) under ApexClass. Wildcards are not supported in destructive changes files, so the file is invalid for deletion. Deletions need each component named exactly, such as ApexClass:LegacyAccountService.