GitHub建立個人Maven倉庫


Maven

官網: https://maven.apache.org/repository/index.html

一、配置github

設置登錄名name

    在github的個人設置中,設置好自己的姓名 。這個環節很重要,若不設置姓名,會出現一些一些意想不到的錯誤。maven的setting中的配置文件用戶名都用這個。

二、生成maven包到本地

修改pom文件發布到本地倉庫

在需要發布的項目中的pom文件中的build -> pluginManagement -> plugins標簽下加入以下插件:

<!--編譯本地倉庫包插件,生成target/mvn-repo下面-->
<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>

然后運行 mvn clean deploy 命令,即可在對應項目中的target/mvn-repo目錄下找到本地的jar。

三、發布到github(失效)

修改mvn配置文件settings.xml

Win+R輸入%MAVEN_HOME%打開maven安裝目錄,修改本地maven的配置文件settings.xml,找到其中的servers 標簽,加入如下 配置:

<server>
    <id>github</id>     <username>github登錄名</username>     <password>github登錄密碼</password> </server>

修改pom文件properties中添加下列屬性

關聯setting.xml中使用哪個github的server。

<github.global.server>github</github.global.server>

添加遠程發布插件

            <!--發布到服務器插件,需要依賴上面的部分-->
            <!--查看aliyun的倉庫https://maven.aliyun.com/mvn/search,發現需要依賴包-->
            <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><!--本地jar地址-->
                    <branch>refs/heads/master</branch><!--branch必須是refs/heads/開頭的,后邊跟分支名稱-->
                    <merge>true</merge>
                    <includes>
                        <include>**/*</include>
                    </includes>
                    <repositoryName>maven-repo</repositoryName> <!--對應github上創建的倉庫名-->
                    <repositoryOwner>your setting name</repositoryOwner> <!--github倉庫所有者setting中的name-->
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>site</goal>
                        </goals>
                        <phase>deploy</phase>
                    </execution>
                </executions>
            </plugin>

依賴插件

        <!-- 發布到服務器插件的依賴包 -->
        <dependency>
            <groupId>com.github.github</groupId>
            <artifactId>site-maven-plugin</artifactId>
            <version>0.9</version>
            <type>maven-plugin</type>
        </dependency>

mvn clean deploy運行結果

手動上傳到github

發現github上並沒有上傳文件,於是新建了一個branch並上傳。

### 在項目根目錄上面
cd ./target/mvn-repo   #進入mvn包目錄
git init   #初始化本地倉庫
git add .   #添加文件
git status   #查看狀態
git commit -m "maven倉庫"  #提交到本地  
#關聯到服務器,並沒命名倉庫為origin
git remote add origin https://github.com/AutKevin/maven-repo.git  
git branch repo    #創建repo分支
git branch -a    #查看所有分支
git checkout repo    #切換到repo分支
git push origin repo    #推送到origin遠程倉庫的repo分支

maven包上傳到服務器格式如下,在maven-repo倉庫下的repo分支中。依然讀取不到。

四、發布到ftp服務器(能發不能讀,需和http服務器聯用)

通過插件

官網: https://maven.apache.org/plugins/maven-deploy-plugin/examples/deploy-ftp.html

pom.xml中添加

<project>
  ...
  <distributionManagement>
    <repository>
      <id>ftp-repository</id>
      <url>ftp://repository.mycompany.com/repository</url>
    </repository>
  </distributionManagement>
 
  <build>
    <extensions>
      <!-- Enabling the use of FTP -->
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
         <artifactId>wagon-ftp</artifactId>
         <version>1.0-beta-6</version>
      </extension>
    </extensions>
  </build>
  ...
</project>

setting.xml文件中配置

<settings>
  ...
  <servers>
    <server>
      <id>ftp-repository</id>
      <username>user</username>
      <password>pass</password>
    </server>
  </servers>
  ...
</settings>

然后執行mvn deploy

通過idea的Deployment

Setting -> Build,Execution,Deployment -> Deployment

設置好要上傳的文件夾

上傳成功如下:

報錯500 Illegal PORT command

點擊Advanced Options,勾上Passive mode

modprobe ip_nat_ftp
modprobe ip_conntrack_ftp

使用ftp發現能上傳成功卻不能讀取。找不到原因。。。歡迎指教~

五、通過tomcat發布

在webapp下面新建一個mvn文件夾,把項目發布到mvn項目下即可。

手動部署

在項目的pom.xml中添加:

<distributionManagement>
    <repository>
        <id>file-repository</id>
        <url>file://D:\mvn-rpo</url>
    </repository>
</distributionManagement>

運行mvn deploy命令可以看到在D:\abc目錄下生成了目錄和各種文件。將整個目錄上傳到Tomcat的webapps/mvn下面。

ssh自動部署

官方文檔:https://maven.apache.org/plugins/maven-deploy-plugin/examples/deploy-ssh-external.html

<distributionManagement>
    <repository>
        <id>ssh-server</id>
        <url>scp://ip/home/java/apache-tomcat-8.5.5/webapps/mvn</url>
    </repository>
</distributionManagement>

<build>
    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh-external</artifactId>
            <version>1.0-beta-6</version>
        </extension>
    </extensions>
</build>

新建一個用戶:https://www.cnblogs.com/aeolian/p/12449679.html

Maven的settings.xml中配置服務器的SSH用戶名和密碼:

<server>
    <id>ssh-server</id>
    <username>root</username>
    <password>your-password</password>
</server>

執行mvn deploy

idea的Deployment(推薦)

使用idea的ftp發布,首先要建一個ftp用戶並給用戶指定文件夾賦予權限。

#進入到webapp目錄
cd /usr/local/tomcat/apache-tomcat-7.0.54/webapps
#新建mvn文件夾
mkdir mvn 
#給mvn文件夾賦給ftp用戶mvn
chown mvn:mvn mvn

然后用mvn用戶名登錄

點擊Tools -> Deployment -> Browse Remote Host,進行upload。

六、GitHub Package

官網: https://help.github.com/en/packages/using-github-packages-with-your-projects-ecosystem/configuring-apache-maven-for-use-with-github-packages

生成Token

點擊右上角頭像,點擊Setting -》 Developer settings -》Personal access tokens,寫上備注,勾上權限。點擊生成后一定要先復制Token,下次就不顯示Token了。

 

 

Maven的Setting配置

<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">

  <!-- 讀取mvn庫用,選擇要激活的profile -->
  <activeProfiles>
    <activeProfile>github</activeProfile>
  </activeProfiles>
  <!-- 讀取mvn庫用,配置一系列profile,一定要寫到具體倉庫 -->
  <profiles>
    <profile>
      <id>github</id>
      <repositories>
        <repository>
          <id>github</id>
          <name>GitHub OWNER Apache Maven Packages</name>
          <url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
        </repository>
      </repositories>
    </profile>
  </profiles>
  <!-- 上面兩項也可以不配置,只不過每次都要在pom.xml文件中配置,這里配置可以一勞永逸 -->

  <!-- 發布讀取都要用到 -->
  <servers>
    <server>
      <id>github</id>
      <username>USERNAME</username>
      <password>TOKEN,注意這里是Token不是你的密碼啊啊啊啊啊啊啊</password>
    </server>
  </servers>
</settings>

Pom.xml

<distributionManagement>
   <repository>
     <id>github</id>
     <name>GitHub OWNER Apache Maven Packages</name>
     <url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
   </repository>
</distributionManagement>

執行mvn deploy,注意!!!一定要把maven-deploy-plugin這個插件給注釋掉,不然只會發布到本地,不會執行發布到遠程服務器。

如果報錯,可以使用mvn -e deploy或者mvn -X deploy查看詳情。

一直報Return code is: 401, ReasonPhrase: Unauthorized,檢查了setting中的server配置的用戶名密碼正確,一天后發現password需要填的不是密碼是token。

七、使用倉庫

github package

在需要使用jar包項目的pom文件中添加github倉庫,也可以在maven的setting中配置profile。無論哪種一定要用server的用戶名和token。

    <!--引入倉庫-->
    <repositories>
            <repository>
                <id>github</id>
                <name>GitHub autumn Apache Maven Packages</name>
                <url>https://maven.pkg.github.com/AutKevin/maven-repo/</url>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
    </repositories>

然后執行mvn install。

tomcat

tomcat的端口/項目名

        <repository>
            <id>tomcat-repository</id>
            <url>http://52zt.info:88/mvn</url>
        </repository>

添加依賴

<dependency>
    <groupId>com.autumn</groupId>
    <artifactId>aeo-tool</artifactId>
    <version>1.0.0</version>
</dependency>

參考:https://www.jianshu.com/p/98a141701cc7


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM