nginx 搭建maven 私有仓库

搭建maven仓库有多种方法,实际上只需要有一个可以访问的http服务就可以搭建一个maven仓库。在这里介绍通过nginx作为web容器的搭建方法。

1.安装nginx  nginx 的安装

2.配置nginx 静态资源服务器  nginx静态资源服务器配置

我的配置如下

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /remoteRepo/{
            root   /usr/local/;
            autoindex on;
    }

    新建目录 /usr/local/remoteRepo
    注意目录的权限否者可能不能上传(使用chown 和 chmod)

访问截图

3.maven 向服务器发布jar包

      3.1修改maven settings,xml  添加server

      

<servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
	<server>
        <id>my-server</id>
        <username>wl</username><!-- ssh 服务器用户名 注意nginx映射的文件夹要属于该用户 -->
        <password>*****</password><!-- ssh 服务器密码 -->
	    <filePermissions>664</filePermissions>
    </server>
  </servers>

   3.2修改工程pom.xml   增加如下代码

    

<!-- 打包源码插件-->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <executions>
          <execution>
            <id>attach-sources</id>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>

    <extensions>
      <extension>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-ssh</artifactId>
        <version>2.10</version>
      </extension>
    </extensions>
  </build>

  <!-- 打包到本地私有服务器 -->
  <distributionManagement>
    <repository>
       <!-- 与上面的server id一致 -->
      <id>my-server</id>
       <!-- 上传到服务器的路径 -->
      <url>scp://192.168.46.128/usr/local/remoteRepo</url>
    </repository>
  </distributionManagement>

    3.3执行mvn deploy 发布

[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ email-api ---
Downloading: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/maven-metadata.xml
Downloaded: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/maven-metadata.xml (988 B at 1.4 KB/sec)
Uploading: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/email-api-1.0-20180726.151304-3.jar
Uploaded: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/email-api-1.0-20180726.151304-3.jar (5 KB at 139.0 KB/sec)
Uploading: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/email-api-1.0-20180726.151304-3.pom
Uploaded: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/email-api-1.0-20180726.151304-3.pom (2 KB at 70.7 KB/sec)
Downloading: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/maven-metadata.xml
Downloaded: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/maven-metadata.xml (290 B at 8.3 KB/sec)
Uploading: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/maven-metadata.xml
Uploaded: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/maven-metadata.xml (988 B at 40.2 KB/sec)
Uploading: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/maven-metadata.xml
Uploaded: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/maven-metadata.xml (290 B at 10.9 KB/sec)
Uploading: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/email-api-1.0-20180726.151304-3-sources.jar
Uploaded: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/email-api-1.0-20180726.151304-3-sources.jar (5 KB at 34.4 KB/sec)
Uploading: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/maven-metadata.xml
Uploaded: scp://192.168.46.128/usr/local/remoteRepo/com/wl/common/email/api/email-api/1.0-SNAPSHOT/maven-metadata.xml (988 B at 23.5 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

 服务器上也上传成功

4.从私有仓库下载jar包

    4.1修改maven  settings.xml

          4.1.1 增加镜像

           

<mirror>
	  		<id>my-repo</id>
	  		<mirrorOf>my-repo</mirrorOf>
			<name>私有仓库</name>
	  		<url>http://192.168.46.128/remoteRepo/</url>
	  	</mirror>

		<mirror>
	  		<id>central</id>
	  		<mirrorOf>central</mirrorOf>
			<name>中央仓库</name>
	  		<url>http://repo1.maven.org/maven2/</url>
</mirror>

    4.1.2  增加profile

     

<profile>
	    <id>my-repo-profile</id>
	    <repositories>
	        <repository>
	            <id>my-repo</id>
	            <url>http://192.168.46.128/remoteRepo/</url>
	            <releases><enabled>true</enabled></releases>
	            <snapshots><enabled>true</enabled></snapshots>
	        </repository>
	    </repositories>
	    <pluginRepositories>
	        <pluginRepository>
	            <id>my-repo</id>
	            <url>http://192.168.46.128/remoteRepo/</url>
	            <releases><enabled>true</enabled></releases>
	            <snapshots><enabled>true</enabled></snapshots>
	        </pluginRepository>
	    </pluginRepositories>
	</profile>


  <profile>
	    <id>central-profile</id>
	    <repositories>
	        <repository>
	            <id>central</id>
	            <url>http://repo1.maven.org/maven2/</url>
	            <releases><enabled>true</enabled></releases>
	            <snapshots><enabled>true</enabled></snapshots>
	        </repository>
	    </repositories>
	    <pluginRepositories>
	        <pluginRepository>
	            <id>central</id>
	            <url>http://repo1.maven.org/maven2/</url>
	            <releases><enabled>true</enabled></releases>
	            <snapshots><enabled>true</enabled></snapshots>
	        </pluginRepository>
	    </pluginRepositories>
	</profile>

4.2.3 

<activeProfiles>
    <activeProfile>my-repo-profile</activeProfile>
	<activeProfile>central-profile</activeProfile>
  </activeProfiles>

现在就可以下载私有远程仓库的jar包了

注意:通过ide刷新maven依赖可能不会拉取远程仓库的jar包 这时需要执行 mvn intall  或者 mvn package 之后在刷新依赖

文章参考  https://www.cnblogs.com/shuaiandjun/p/7604619.html

                

猜你喜欢

转载自blog.csdn.net/name_is_wl/article/details/81229171