能否将Angular自定义原理图发布至Azure DevOps npm或私有仓库用作模板?
Absolutely! You totally can publish your custom Angular schematic collection to Azure DevOps Artifacts (or any private npm registry) and use it as a reusable project template with the Angular CLI's --collection flag. Let me walk you through how to set this up step by step:
1. Prepare Your Schematic Collection
First, make sure you've built out your custom schematic project. If you haven't started yet, create a new schematics workspace with:
ng new my-custom-schematics --create-application=false
Then add your schematic logic—define templates, modify the collection.json config, and test it locally to ensure it generates the exact project structure you want as a template.
2. Configure Your Package for Publishing
In your schematics project's package.json, double-check these key details:
- A clear
name(ideally scoped to your organization, like@your-org/custom-templates) - Correct
versionnumber (follow semantic versioning for updates) - A
schematicsfield pointing to your collection config file:"schematics": "./src/collection.json"
Verify that collection.json correctly references your schematic's entry points and template paths—this tells Angular CLI where to find your generation logic.
3. Authenticate with Azure DevOps Artifacts
Before publishing, you need to connect your local npm to your Azure DevOps feed:
- Get a Personal Access Token (PAT) from Azure DevOps: Go to your profile → Security → Personal access tokens, create one with at least
Packaging (Read & Write)permissions. - Configure npm to use your Azure feed:
npm config set registry https://pkgs.dev.azure.com/[Your-Org-Name]/[Your-Project-Name]/_packaging/[Your-Feed-Name]/npm/registry/ - Log in to the registry:
When prompted, enter any username (it’s ignored) and use your PAT as the password.npm login --registry=https://pkgs.dev.azure.com/[Your-Org-Name]/[Your-Project-Name]/_packaging/[Your-Feed-Name]/npm/registry/
4. Publish the Schematic Collection
Head to your schematics project root and run:
npm publish
Once successful, you’ll see your package listed in your Azure DevOps Artifacts feed.
5. Use the Template with Angular CLI
Now you can generate new projects using your private schematic collection:
- If your npm is already configured to use the Azure feed as default, just run:
ng new my-new-project --collection=@your-org/custom-templates - If you use multiple registries, either install the schematic package globally first:
Or configure your organization’s npm scope to point to the Azure feed permanently:npm install -g @your-org/custom-templates --registry=https://pkgs.dev.azure.com/[Your-Org-Name]/[Your-Project-Name]/_packaging/[Your-Feed-Name]/npm/registry/
This way, you won’t need to specify the registry every time you install or use packages undernpm config set @your-org:registry https://pkgs.dev.azure.com/[Your-Org-Name]/[Your-Project-Name]/_packaging/[Your-Feed-Name]/npm/registry/@your-org.
Quick Tips
- Test your schematic locally first with
npm linkto avoid publishing broken versions. - Update your PAT if it expires to keep publishing access.
- Use semantic versioning (e.g.,
1.0.0,1.0.1) to manage updates to your template.
内容的提问来源于stack exchange,提问作者Henzard Kruger

