Github Package

不想重複code,使用自己的package!

如果你想要:

  1. 防止Code duplicate(不想copy/paste一樣的code)

  2. 共享你的Utility code到不同project/repository

Github Package是一個private central repository的平台。或是你可以使用myMavenRepo.com的服務。這篇以Github Package為主。

我主要是用Maven,所以這篇都會以Maven做紀錄。

取得Github Personal Token

為了要上傳/下載package到Github,我們必須要身份認證。需要先到Github取得Token。

點此移動到Token頁面:https://github.com/settings/tokens

選擇Scope的時候把package相關的選項都選起來。

Ref:

在Local電腦中設定Github連線

Navigate到你local maven的檔案位置

~/.m2/settings.xml

按照官方文件輸入你的資料在settings.xml的檔案中:

Authenticating to GitHub Packages

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <activeProfiles>
    <activeProfile>github</activeProfile>
  </activeProfiles>

  <profiles>
    <profile>
      <id>github</id>
      <repositories>
        <repository>
          <id>github</id>
          <name>GitHub owen31302 Apache Maven Packages</name>
          <url>https://maven.pkg.github.com/owen31302/TestPackage</url>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

  <servers>
    <server>
      <id>github</id>
      <username>owen31302</username>
      <password>我的Token</password>
    </server>
  </servers>
</settings>

發布Package

到你想要發布的repo,在pom中distributionManagement:

  
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.owen</groupId>
    <artifactId>testpackage</artifactId>
    <version>1.0-SNAPSHOT</version>

    <distributionManagement>
        <repository>
            <id>github</id>
            <name>GitHub Owen31302 Apache Maven Packages</name>
            <url>https://maven.pkg.github.com/owen31302/TestPackage</url>
        </repository>
    </distributionManagement>

</project>

之後在console中輸入:

mvn deploy

就會發布到Github。

Ref: Publishing a package

確認Package是否發布到Github

使用Package

加入dependency到你想要使用package的repository,就可以開始使用了!

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.owen</groupId>
    <artifactId>usetestpackage</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>com.owen</groupId>
            <artifactId>testpackage</artifactId>
            <version>1.0-snapshot</version>
        </dependency>
    </dependencies>

</project>

Ref: Installing a package

myMavenRepo可以參考以下設定方式:

Last updated