简体   繁体   中英

Using github repository as maven dependency

I have one project created which contains some reusable methods which can be used in other projects by adding it as a dependency. To do the same I have added the following in pom.xml file and when I run mvn clean deploy branch is created successfully with artifacts.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.abc</groupId>
    <artifactId>api-authenticator</artifactId>
    <version>1.0.0</version>
    <name>API Authenticator</name>
    <description>Project to authenticate API usage</description>

    <distributionManagement>
        <repository>
            <id>internal.repo</id>
            <name>Staging Repository</name>
            <url>file://${project.build.directory}/${version}</url>
        </repository>
    </distributionManagement>

    <properties>
        <github.global.server>github</github.global.server>
        <java.version>11</java.version>
        <java-jwt.version>3.16.0</java-jwt.version>
        <jwks-rsa.version>0.18.0</jwks-rsa.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>${java-jwt.version}</version>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>jwks-rsa</artifactId>
            <version>${jwks-rsa.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.github.github</groupId>
                <artifactId>site-maven-plugin</artifactId>
                <version>0.12</version>
                <configuration>
                    <message>Maven artifacts for ${project.version}</message>
                    <noJekyll>true</noJekyll>
                    <outputDirectory>${project.build.directory}</outputDirectory>
                    <branch>refs/heads/${version}</branch>
                    <includes>
                        <include>**/*</include>
                    </includes>
                    <merge>true</merge>
                    <repositoryName>rwg-dip-api-authenticator</repositoryName>
                    <repositoryOwner>robert-walters</repositoryOwner>
                    <server>github</server>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>site</goal>
                        </goals>
                        <phase>deploy</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <configuration>
                    <altDeploymentRepository>
                        internal.repo::default::file://${project.build.directory}/${version}
                    </altDeploymentRepository>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

In settings.xml (Maven home: /opt/homebrew/Cellar/maven/3.8.2/libexec) I have generated personal access token with all access required and added it.

<server>
   <id>github</id>
   <password>access_token</password>
</server>   

To use this as a dependency I have added the following in the project where I want to reuse the functions and when I do reimport the dependency from IntelliJ I can see my internal project's dependency in external dependencies But when I run the project it shows java: package com.abc.util does not exist althoght I am getting the suggestions for the class in Intellj.

    <repositories>
        <repository>
            <id>https://github.com/me/api-authenticator</id>
            <url>https://github.com/me/api-authenticator/1.0.0</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

    <dependency>
        <groupId>com.abc</groupId>
        <artifactId>api-authenticator</artifactId>
        <version>1.0.0</version>
   </dependency>

I have gone through the below but it was not helpful for the above case

Hosting a Maven repository on github

https://www.baeldung.com/maven-repo-github

https://dev.to/iamthecarisma/hosting-a-maven-repository-on-github-site-maven-plugin-9ch

UPDATE

As M.Deinum has suggested I have removed spring-boot-maven-plugin and now I am able to use it as depency in other projects in Intellj and it works fine locally but when the pipeline execute to create build for the project mvn package steps fails with "The POM for com.abc:api-authenticator:jar:1.0.0 is missing, no dependency information available"

If you are ok with using an external service for that, you can check https://jitpack.io/ out.

It is able to build your maven project directly from a GitHub repository and then acts as a maven repository that is serving the artifact.

It takes time to download the artifact for the first time (as it builds the project only with the first request for the artifact), but then it reuses the already built artifacts for the next requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM