【问题标题】:How to create own Maven Repository?如何创建自己的 Maven 存储库?
【发布时间】:2019-08-24 14:43:34
【问题描述】:

我想知道,如何创建自己的依赖项以在其他项目中使用我的代码。

我正在学习教程。 我试图用简单的类创建项目作为 Maven 项目。 我做了干净的包装。创建了 github 存储库。在那里添加了我的项目与“目标”包。 在 pom.xml 我添加了

<properties>
        <java.version>1.8</java.version>
        <github.global.server>github</github.global.server>
        <github.maven-plugin>0.12</github.maven-plugin>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.1</version>
                <configuration>
                    <altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.github.github</groupId>
                <artifactId>site-maven-plugin</artifactId>
                <version>${github.maven-plugin}</version>
                <configuration>
                    <message>Maven artifacts for ${project.version}</message>
                    <noJekyll>true</noJekyll>
                    <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
                    <branch>refs/heads/mvn-repo</branch>
                    <includes><include>**/*</include></includes>
                    <repositoryName>GITHUB_NAME_REPOSITORY</repositoryName>
                    <repositoryOwner>MY_GITHUB_NICKNAME</repositoryOwner>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>site</goal>
                        </goals>
                        <phase>deploy</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

在 .m2 目录的根目录之后,我创建了 settings.xml:

<settings>
  <servers>
    <server>
      <id>github</id>
      <username>[username]</username>
      <password>[password]</password>
    </server>
  </servers>
</settings>

再次清理+打包并推送到github。

尝试使用依赖项后 - 未找到。 在 github repo 中没有 mvn-repo 分支

【问题讨论】:

    标签: maven dependencies repository


    【解决方案1】:

    我还没有使用新的 GitHub 存储库,但到目前为止效果很好:

    • 私有,单机使用:mvn install -> 工件将安装在您的本地 Maven 存储库中,并且可以被同一台机器上的任何其他项目引用
    • 开源,多机器/开发人员:mvn deployMaven Central。有关配置和涉及的步骤的更多信息,请参阅documentation
    • 闭源,多台机器/开发人员:mvn deploy 到您自己的 Maven 存储库管理器,例如 Nexus(相应地配置 distributionManagement)

    也就是说,best practice 在所有 3 种情况下都使用您自己的 Maven 存储库管理器并定义 single group

    来自Maven default lifecycle 文档:

    打包:获取编译后的代码,并将其打包成可分发的格式,例如 JAR。

    安装:将包安装到本地仓库,作为本地其他项目的依赖。

    部署:在集成或发布环境中完成,将最终包复制到远程存储库以与其他开发人员和项目共享。

    【讨论】:

    • 谢谢,我确实部署而不是包,它可以工作。我认为在 pom.xml 插件中它会通过“打包”为部署做准备,之后我会自己推送它
    • @ekstra777 请参阅默认的 Maven 生命周期文档以获取有关阶段的更多信息:maven.apache.org/guides/introduction/…
    猜你喜欢
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多