有时候我们自己做一些starter,然后让大家使用。但是苦于自己没有私服。代码写的再好但是其他的同学却获取不到。显然这让人很难受。那么如何将github作为自己的私服?下面让我们一探究竟。
我们在本地创建一个maven项目
在您的pom文件中加入如下配置
<?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.scaffold</groupId>
<artifactId>test-starter</artifactId>
<version>1.0</version>
<profiles>
<profile>
<id>JDK-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
<properties>
<github.global.server>github</github.global.server>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<fork>true</fork>
<executable>
C:\Program Files\Java\jdk1.8.0_211\bin\javac.exe
</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<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>
</build>
</project>
然后执行
mvn clean deploy
在target的目录中可以看到mvn-repo的文件夹,这个文件夹就是我们需要上传的jar包。
我们在github上创建一个自己的仓库。
之后github会出现项目的一些git命令。就是要指导我们将本地代码提交到远程仓库的。这里主要需要修改一下git add *就是将该文件夹下的文件全部都添加到本地仓库的意思。
之后我们执行这些命令
然后刷新页面即可看到我们的需要提交的jar包已经提交到github仓库了。
那么我们如何使用我们的包?
在我们的项目中导入
<dependency>
<groupId>com.scaffold</groupId>
<artifactId>test-starter</artifactId>
<version>1.0</version>
</dependency>
但是由于maven不知道我们的仓库在哪里。所以找也找不到。所以我们可以在setting.xml中进行指定。或者在pom.xml中指定。这里演示在pom.xml指定。
在pom文件中添加:
<repositories>
<repository>
<id>github</id>
<url>https://raw.github.com/scaffold-repository/test-starter/master/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
这里的url就是我们的https://raw.github.com/+组织名/仓库名/分支,由于网络可能延迟比较严重,所以很多时候包拉不全。需要您耐心多尝试几次。