使用github搭建自己的maven庫

趙安家發表於2018-12-15

建議使用maven中央倉庫進行釋出,不過我嫌步驟太繁瑣了,還需要稽核,所以才用github來做。釋出中央倉庫的可以參考Maven 釋出自己的專案到 Maven 中央倉庫

使用github分兩種,一種是mvn install 或者deploy到本地路徑,然後git add && commit && push ,一種是maven-plugins

兩種方案都需要在github建立相應的repo,具體建立步驟不多說,自行百度。

1. maven-plugins

參考 stackoverflow.com/a/14013645

1.修改 ~/.m2/settings.xml

<!-- NOTE: MAKE SURE THAT settings.xml IS NOT WORLD READABLE! -->
<settings>
  <servers>
    <server>
      <id>github</id>
      <username>YOUR-USERNAME</username>
      <password>YOUR-PASSWORD</password>
    </server>
  </servers>
</settings>
複製程式碼

2.修改pom.xml

<properties>
    <!-- github server corresponds to entry in ~/.m2/settings.xml -->
    <github.global.server>github</github.global.server>
</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>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
                <configuration>
                    <altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
                </configuration>
            </plugin>
		</plugins>
	</build>
複製程式碼

3.修改pom.xml,配置maven-plugins

<build>
    <plugins>
        <plugin>
            <groupId>com.github.github</groupId>
            <artifactId>site-maven-plugin</artifactId>
            <version>0.12</version>
            <configuration>
                <message>Maven artifacts for ${project.version}</message>  <!-- git commit message -->
                <noJekyll>true</noJekyll>                                  <!-- disable webpage processing -->
                <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory> <!-- matches distribution management repository url above -->
                <branch>refs/heads/master</branch>                       <!-- remote branch name -->
                <includes><include>**/*</include></includes>
                <repositoryName>YOUR-REPOSITORY-NAME</repositoryName>      <!-- github repo name -->
                <repositoryOwner>YOUR-GITHUB-USERNAME</repositoryOwner>    <!-- github username  -->
                <force>false</force> <!-- force commit or no -->
                <merge>true</merge> <!-- merge or no -->
            </configuration>
            <executions>
              <!-- run site-maven-plugin's 'site' target as part of the build's normal 'deploy' phase -->
              <execution>
                <goals>
                  <goal>site</goal>
                </goals>
                <phase>deploy</phase>
              </execution>
            </executions>
        </plugin>
    </plugins>
</build>
複製程式碼

4.提交到github

mvn clean deploy
複製程式碼

2. mvn deploy && git add && commit && push

參考 blog.csdn.net/u010442302/…

1.clone github repo

git clone https://github.com/YOUR-USERNAME/YOUR-PROJECT-NAME /home/my/code/maven-repo/
複製程式碼

2.命令列執行mvn deploy

mvn deploy -DaltDeploymentRepository=my-mvn-repo::default::file:/home/my/code/maven-repo/
複製程式碼

3.push到github

cd /home/my/code/maven-repo/
git add .
git commit -m 'Maven artifacts for 0.0.1'
git push -u origin master
複製程式碼

3. 在專案中使用github repo

<repositories>
    <repository>
        <id>YOUR-PROJECT-NAME-mvn-repo</id>
        <url>https://raw.githubusercontent.com/YOUR-USERNAME/YOUR-PROJECT-NAME/master/</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
</repositories>
複製程式碼

總結

如果可以修改pom建議用maven-plugins,如果不能修改pom,則使用命令列mvn deploy&&git add&&commit&&push

相關文章