2

I have deployed a java package to Github packages successfully, I tried to consume it successfully as a dependency:

<dependency>
      <groupId>com.example</groupId>
      <artifactId>demo</artifactId>
      <version>1.0.0-SNAPSHOT</version>
</dependency>

but I can't resolve(fetch) it as a maven plugin like this:

<build>
    <plugins>
      <plugin>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>1.0.0-SNAPSHOT</version>
      </plugin>
...

I am getting this error:

Plugin com.example:demo:1.0.0-SNAPSHOT or one of its dependencies could not be resolved:
Could not find artifact com.example:demo:1.0.0-SNAPSHOT -> [Help 1]

also the .m2/settings.xlm is ready and works fine as I tested it by fetching the package as a dependency not plugin, and it contains:

<server>
      <id>github</id>
      <username>USER</username>
      <password>PERSONAL_TOKEN_SCOPE_REPO_W_R_PACKAGES</password>
</server>
<profile>
      <id>github</id>
      <repositories>
        <repository>
          <id>github</id>
          <name>GitHub OWNER Apache Maven Packages</name>
          <url>https://maven.pkg.github.com/OWNER/REPO-NAME</url>
          <releases>
              <enabled>true</enabled>
          </releases>
          <snapshots>
            <updatePolicy>always</updatePolicy>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
</profile>
<activeProfiles>
    <activeProfile>github</activeProfile>
</activeProfiles>

Any Idea? Thanks in advance

Jehad Nasser
  • 3,227
  • 5
  • 27
  • 39

2 Answers2

1

I found out how to configure it the right way.

You can see a sample project with the working GitHub Actions CI and the GitHub Package Registry here:

To see how the dependency can be included check the: GitHub-plugin-registry-example Template

The trick was to add an authentification to the GitHub API in the global maven settings.xml.

<servers>
    <server>
        <id>github</id>
        <username>YOUR_USERNAME</username>
        <password>YOUR_AUTH_TOKEN</password>
    </server>
</servers>

Replace the YOUR_USERNAME with your GitHub login name. Replace the YOUR_AUTH_TOKEN with a generated GitHub personal access token: GitHub > Settings > Developer Settings > Personal access tokens > Generate new token: The token needs at least the read:packages scope. Otherwise you will get a Not authorized exception.

It wasn't clear that this auth is also needed to read a package. Especially because the jar is available without any login on the package page: https://github.com/TobseF/HelloMaven/packages

So it's a little bit nasty because we have to add the github... and hope others will also provide the the repository with the github id. Otherwise we have to add a server config for every github dependency.

Keep in mind that every GitHub repo is its own maven repo. So there is no global registry like the maven central. Every dependency has to provide its own repository link declaration.

But in combination with the GitHub Actions CI, it's a very nice alternative without any third party plugins.

jahantaila
  • 840
  • 5
  • 26
  • Thanks @boetis . I noticed that you meant "dependency" not "Plugin" as I successfully able to fetch dependency but I can't fetch the package as a plugin. Did I understand u right? – Jehad Nasser May 31 '21 at 15:20
  • Yes, sorry. I made a mistake. If my answer helped you, please checkmark it and upvote so it can help the community. – jahantaila May 31 '21 at 15:25
  • unfortunately ur answer is perfect for fetching a dependency, but not plugin which I need. and my issue is not authentication issue, as I said I tested my tag by fetching dependency from the same repo and it was successful, and user as owner and password as token with scopes repo + package(w/r). – Jehad Nasser May 31 '21 at 15:39
  • hi again, and thanks a lot for helping. I posted my solution down, maybe you would like to check it. have a nice day. – Jehad Nasser Jun 01 '21 at 08:51
1

Eureka! For fetching a plugin we should use <pluginRepositories> instead of <repositories> which is used for fetching dependencies.

Remember: pluginRepositories contains the information needed for establishing connections with remote repository.

My setup was right, I just needed to add this section inside <profile>:

<pluginRepositories>
     <pluginRepository>
          <id>github</id>
          <name>GitHub OWNER Apache Maven Packages</name>
          <url>https://maven.pkg.github.com/OWNER/REPO-NAME</url>

          <!-- next elements are up to you -->
          <releases>
              <enabled>true</enabled>
          </releases>
          <snapshots>
            <updatePolicy>always</updatePolicy>
            <enabled>true</enabled>
          </snapshots>
     </pluginRepository>
</pluginRepositories>

See a sample ~/.m2/settings.xml here

Jehad Nasser
  • 3,227
  • 5
  • 27
  • 39