如何通过maven将小型开源库提供给其他开发人员? 一种方法是将其部署在Maven Central Repository上 。 我想要做的是将其部署到github ,因此我可以自由地对其进行修改。 这篇文章将告诉您如何做到这一点。

我将工件部署到github的典型方法是使用mvn deploy 。 步骤如下:

  • 使用site-maven-plugin将工件推送到github
  • 使用maven-javadoc-plugin推送Javadoc
  • 使用maven-source-plugin推送源
  • 配置Maven以将远程mvn-repo用作Maven存储库

配置Maven部署插件

首先,我添加以下代码片段以告知Maven将工件部署到目标目录内的临时位置:

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

配置Maven

然后,我将我的github.com身份验证信息添加到~/.m2/settings.xml以便github site-maven-plugin可以将其推送到github:

<settings>
  <servers>
    <server>
      <id>github</id>
      <password>OAUTH2TOKEN</password>
    </server>
  </servers>
</settings>

要么

<settings>
  <servers>
    <server>
      <id>github</id>
      <username>GitHubLogin</username>
      <password>GitHubPassw0rd</password>
    </server>
  </servers>
</settings>

就我个人而言,我更喜欢第一种方法,因为它更安全(无需明确显示密码)。 要获取github项目的OAUTH2TOKEN ,请转到settings --> Applications --> Genreate new token

配置site-maven-plugin

配置site-maven-plugin从我的临时位置上传到github上的mvn-repo分支:

<plugin>
  <groupId>com.github.github</groupId>
  <artifactId>site-maven-plugin</artifactId>
  <version>0.9</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>pengyifan-commons</repositoryName>
    <repositoryOwner>yfpeng</repositoryOwner>
    <server>github</server>
  </configuration>
  <executions>
    <execution>
      <goals>
        <goal>site</goal>
      </goals>
      <phase>deploy</phase>
    </execution>
  </executions>
</plugin>

写这篇文章时,存在0.9版site-maven-plugin 。 要解决此问题,请git clone 0.10-SNAPSHOT版本,并手动mvn install

配置maven-source-plugin

要将源代码包添加到mvn-repo中,我们需要配置maven-source-plugin 。 在pom.xml添加以下代码:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-source-plugin</artifactId>
  <version>2.3</version>
  <executions>
    <execution>
      <id>attach-sources</id>
      <goals>
        <goal>jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>

配置Maven-javadoc-plugin

要将java doc软件包添加到mvn-repo中,我们需要配置maven-javadoc-plugin 。 在pom.xml添加以下代码:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-javadoc-plugin</artifactId>
  <executions>
    <execution>
      <id>attach-javadocs</id>
      <goals>
        <goal>jar</goal>
      </goals>
  </execution>
  </executions>
</plugin>

现在运行mvn clean deploy 。 我看到maven-deploy-plugin将文件“上传”到目标目录中的本地暂存库中,然后site-maven-plugin提交这些文件并将其推送到服务器。

要验证所有二进制文件是否存在,请在浏览器中访问github ,然后选择mvn-repo分支。

配置Maven以将远程mvn-repo用作Maven存储库

我们应该采取的另一步骤是配置任何poms以知道我们的存储库在哪里。 我们可以将以下代码段添加到任何项目的pom.xml中:

<repositories>
  <repository>
    <id>PROJECT-NAME-mvn-repo</id>
    <url>https://raw.github.com/USERNAME/PROJECT-NAME/mvn-repo/</url>
    <snapshots>
      <enabled>true</enabled>
      <updatePolicy>always</updatePolicy>
    </snapshots>
  </repository>
</repositories>

翻译自: https://www.javacodegeeks.com/2014/09/hosting-a-maven-repository-on-github-with-sources-and-javadoc.html

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐