如何从 Github 包安装 Maven 依赖项?

How to install maven dependency from Github packages?

我在 Github 包中创建了一个 Maven 依赖项。我正在尝试在我的另一个 mule 项目中安装此依赖项。

settings.xml:

<server>
    <id>github</id>
    <username><owner></username>
    <password><Token></password>
</server>

<profile>
      <id>github</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>https://repo1.maven.org/maven2</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
          <id>github</id>
          <url>https://maven.pkg.github.com/<owner>/<repo-name></url>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <releases>
            <enabled>true</enabled>
          </releases>
        </repository>
      </repositories>
    </profile>
  </profiles>

来自 Github 包的依赖项:

<dependency>
  <groupId>com.mycompany</groupId>
  <artifactId>hello-world-3</artifactId>
  <version>1.0.11-SNAPSHOT</version>
</dependency>

我创建了一个 hello world mule 应用程序并在 Pom.xml 和 运行 mvn install 命令提示符中添加了依赖项。

出现此错误:

D:\WorkSpace\test-package-dependency>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.mycompany:test-package-dependency >----------------
[INFO] Building test-package-dependency 1.0.0-SNAPSHOT
[INFO] --------------------------[ mule-application ]--------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.845 s
[INFO] Finished at: 2021-11-19T17:21:57+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project test-package-dependency: Could not resolve dependencies for project com.mycompany:test-package-dependency:mule-application:1.0.0-SNAPSHOT: Failure to find com.mycompany:hello-world-3:jar:1.0.11-SNAPSHOT in https://maven.anypoint.mulesoft.com/api/v2/maven was cached in the local repository, resolution will not be reattempted until the update interval of anypoint-exchange-v2 has elapsed or updates are forced -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

我已经在线搜索但没有找到针对我的错误的解决方案。另外,尝试了 jitpack。它也没有帮助。

您必须在您要访问的项目的pom.xml中定义访问存储库url。

像下面这样定义,

<repositories>
    <repository>
      <id>central</id>
      <url>https://repo1.maven.org/maven2</url>
      <releases><enabled>true</enabled></releases>
      <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <repository>
      <id>github</id>
      <url>https://maven.pkg.github.com/<owner>/<repo-name></url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
      <releases>
        <enabled>true</enabled>
      </releases>
    </repository>
  </repositories>

或者也只尝试使用 id 和 url

<repositories>
    <repository>
      <id>central</id>
      <url>https://repo1.maven.org/maven2</url>
    </repository>
    <repository>
      <id>github</id>
      <url>https://maven.pkg.github.com/<owner>/<repo-name></url>
    </repository>
  </repositories>

注意:确保您已授予读取或下载 jar 的权限,以便 Maven 可以在 运行 maven 阶段轻松地为您下载。