How to fetch maven plugin from Github packages?

Viewed 473

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

2 Answers

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.

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

Related