You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Flutter包发布到本地.m2或私有Nexus仓库?

解决Flutter私有包本地/私有仓库管理的方案

Hey there! I get it—you want to reuse your Flutter package across multiple projects without copying code, and you don't want to publish it publicly on pub.dev. Let's walk through the best options for your use case, including handling local storage and private Nexus repositories.

First, a quick note: Dart/Flutter's pub package manager doesn't natively support publishing to Maven's .m2 directory (since that's a Java ecosystem standard). But we have great alternatives that fit your needs perfectly.

1. 本地路径依赖(最简单的自用方案)

If you're only using the package on your local machine, this is the quickest way to go. Just reference the local directory of your package directly in your project's pubspec.yaml:

dependencies:
  your_private_flutter_package:
    path: ../relative/path/to/your/package/folder
  • Pros: Instant updates when you modify the package, no extra setup needed.
  • Cons: Only works on your local machine—won't help if you need to share across devices or with a team.

2. 私有Git仓库(适合跨设备/小团队)

If you need to access the package from multiple devices or share it with a small team, a private Git repository (like GitHub Private, GitLab, or Gitee Private) is a solid choice. Add this to your project's pubspec.yaml:

dependencies:
  your_private_flutter_package:
    git:
      url: git@your-git-host.com:your-username/your-package-repo.git
      ref: main # Optional: specify a branch, tag, or commit hash for stability
  • You can use SSH (as above) or HTTPS. For HTTPS, if your repo requires authentication, you can set up environment variables or use a credential helper to avoid entering passwords every time.
  • Pros: Works across devices/teams, easy version control with Git tags/branches.
  • Cons: Requires maintaining a private Git repo, updates need to be pushed/pulled manually.

3. 私有Pub服务器(Nexus 方案)

For enterprise-level private package management, Nexus supports Dart/pub repositories. Here's how to set it up:

Step 1: Configure Nexus

  • Log into your Nexus instance and create a Hosted Repository of type "Dart".
  • Note down the repository's URL (e.g., http://your-nexus-server:8081/repository/your-dart-private-repo/).

Step 2: Set up your package for private publishing

Add the publish_to field to your package's pubspec.yaml:

publish_to: "http://your-nexus-server:8081/repository/your-dart-private-repo/"

Step 3: Publish the package

Run this command from your package's root directory:

flutter pub publish --server=http://your-nexus-server:8081/repository/your-dart-private-repo/

Step 4: Configure authentication (if needed)

If your Nexus repo requires authentication, add your credentials to the ~/.pub-cache/credentials.json file (create it if it doesn't exist):

{
  "http://your-nexus-server:8081/repository/your-dart-private-repo/": {
    "token": "your-nexus-auth-token"
  }
}

Step 5: Use the package in other projects

In your project's pubspec.yaml, add the dependency and specify the private repo as a source (if it's not your default pub source):

dependencies:
  your_private_flutter_package:
    hosted:
      url: http://your-nexus-server:8081/repository/your-dart-private-repo/
      name: your_private_flutter_package

Quick note on .m2

Since .m2 is part of the Maven ecosystem, Dart/pub doesn't support publishing directly to it. If you absolutely need to integrate with a Maven-based workflow, you'd have to wrap your Flutter package in a JAR (which isn't ideal for Flutter use cases). Stick to the above options for a smoother experience.

内容的提问来源于stack exchange,提问作者Mani76

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:22:31