简体   繁体   English

使用 github 存储库作为 maven 依赖项

[英]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 文件中添加了以下内容,当我运行mvn clean deploy branch 时,使用工件成功创建了分支。

pom.xml 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.在 settings.xml(Maven 主页:/opt/homebrew/Cellar/maven/3.8.2/libexec)中,我生成了具有所需所有访问权限的个人访问令牌并添加了它。

<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.为了将其用作依赖项,我在项目中添加了以下内容,我想在其中重用函数,当我从 IntelliJ 重新导入依赖项时,我可以在外部依赖项中看到内部项目的依赖项但是当我运行项目时,它显示java: package com.abc.util 虽然不存在但我在 Intellj 中得到了 class 的建议。

    <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 在 github 上托管一个 Maven 存储库

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

https://dev.to/iamthecarisma/hosting-a-maven-repository-on-github-site-maven-plugin-9ch 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"正如 M.Deinum 所建议的那样,我已经删除了spring-boot-maven-plugin ,现在我可以将它用作 Intellj 中其他项目的依赖项,并且它在本地工作正常但是当管道执行为项目创建构建时mvn package步骤失败并显示“缺少 com.abc:api-authenticator:jar:1.0.0 的 POM,没有可用的依赖信息”

If you are ok with using an external service for that, you can check https://jitpack.io/ out.如果您愿意为此使用外部服务,您可以查看https://jitpack.io/

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.它能够直接从 GitHub 存储库构建您的 maven 项目,然后充当为工件提供服务的 maven 存储库。

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.第一次下载工件需要时间(因为它仅在第一次请求工件时构建项目),但随后它会为下一个请求重用已构建的工件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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