如果 jar 包在本地,只能每次手動導入,不太方便。可以使用以下方式搭建一個 Maven 倉庫
1、使用 GitHub Pages 搭建公有倉庫
jar 包本質上是一個靜態文件,所以可以使用 GitHub 來存放,再利用 GitHub Pages 提供一個地址讓其可直接訪問下載。
jar 發佈方
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>
<groupId>wang.depp</groupId>
<artifactId>substruction-common</artifactId>
<version>1.0.0</version>
<name>substruction-common</name>
<description>Substruction common jar</description>
<distributionManagement>
<repository>
<id>local-repo-release</id>
<name>GitHub Release</name>
<url>file://${project.basedir}/maven-repo</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
<distributionManagement>
是配置本地打包後的文件的路徑,當運行 mvn clean package deploy
後,將發佈到根路徑的 maven-repo
文件夾。id 和 name 可以隨便填寫。
默認打包後將包含 jar 包,和 pom 文件。可以添加 2 個插件,使其可以生成 source 源文件 jar 和 javadoc 文檔 jar 包。
maven-repo
└── depp
└── wang
└── substruction-common
├── 1.0.0
│ ├── substruction-common-1.0.0-javadoc.jar
│ ├── substruction-common-1.0.0-javadoc.jar.md5
│ ├── substruction-common-1.0.0-javadoc.jar.sha1
│ ├── substruction-common-1.0.0-sources.jar
│ ├── substruction-common-1.0.0-sources.jar.md5
│ ├── substruction-common-1.0.0-sources.jar.sha1
│ ├── substruction-common-1.0.0.jar
│ ├── substruction-common-1.0.0.jar.md5
│ ├── substruction-common-1.0.0.jar.sha1
│ ├── substruction-common-1.0.0.pom
│ ├── substruction-common-1.0.0.pom.md5
│ └── substruction-common-1.0.0.pom.sha1
├── maven-metadata.xml
├── maven-metadata.xml.md5
└── maven-metadata.xml.sha1
md5 爲消息摘要算法,用於檢驗 jar 包是否被修改?將消息摘要散列生成一串字符串,下載時,再次散列一次,判斷結果是否一致?
因代碼裏包含 Lambda 表達式,不能使用默認 Maven 1.5 的編譯器,需要使用 8 或以上的編譯器。maven-compiler-plugin
當 push 到 GitHub 後,給項目開啓 GitHub Pages,直接訪問 https://depp.wang/substruction-common/ 將打開根目錄 index.html 文件
引用方
<dependency>
<groupId>wang.depp</groupId>
<artifactId>substruction-common</artifactId>
<version>1.0.0</version>
</dependency>
除了正常引用依賴外,還需要指明依賴 jar 獲取地址
<repositories>
<repository>
<id>substruction-common</id>
<name>The Maven Repository on Github</name>
<url>https://depp.wang/substruction-common/maven-repo/</url>
</repository>
</repositories>
2、使用 GitHub Packages 搭建私有倉庫
相關教程:發佈 Artifact
GitHub Setting -> Developer settings -> Personal access tokens
新建 Token,至少選擇 repo、write:packages、read:packages
發佈方
settings.xml 配置 GitHub 賬號密碼(token)
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups></pluginGroups>
<proxies></proxies>
<servers>
<server>
<id>github-release</id>
<username>your-github-name</username>
<password>e1a589c**01027697e</password>
</server>
</servers>
<mirrors></mirrors>
<profiles></profiles>
</settings>
pom.xml 配置倉庫地址
<distributionManagement>
<repository>
<id>github-release</id>
<name>GitHub Release</name>
<url>https://maven.pkg.github.com/DeppWang/provider-client</url>
</repository>
</distributionManagement>
id 需要和 settings.xml 的 id 相同。通過此 id 查找 settings.xml 的賬號密碼。url 所包含的 provider-client
無需再 package 中新建,但必須存在一個同名倉庫,因爲 Package 本質上是依賴於倉庫實現的。
否則可能遇到的錯誤:
Failed to deploy artifacts: Could not find artifact ** in github-release (https://maven.pkg.github.com/DeppWang/provider-client)
解決方案。
當發佈後,更新版本再次發佈時,可能遇到 409 的錯誤:
provider-client: Failed to deploy artifacts: Could not transfer artifact wang.depp:provider-client:jar:sources:1.0.2 from/to github-release (https://maven.pkg.github.com/DeppWang/provider-client): Failed to transfer file https://maven.pkg.github.com/DeppWang/provider-client/wang/depp/provider-client/1.0.2/provider-client-1.0.2-sources.jar with status code 409
此時可能已經發布成功了,可查看 package
引用方
pom.xml 配置 repository
<distributionManagement>
<repository>
<id>github-release</id>
<name>GitHub Release</name>
<url>https://maven.pkg.github.com/DeppWang/provider-client</url>
</repository>
</distributionManagement>
如果引用方和發佈方不是一臺電腦需要額外申請一個具有 repo、read:packages 的 Token?可否使用同一個 token?
3、使用 Nexus 免費版搭建私有倉庫
上面這種方式依賴於項目,無法集中管理所有的 jar 包,還需要設置 settings.xml。使用 Nexus 免費版搭建私有倉庫是一個更好的方式。相關教程:Installation。
當需要將自己開發的 jar 包同步到中央倉庫,以供所有人下載使用時,可使用 Sonatype 來實現,相關教程:發佈 Artifact