使用Nexus2搭建私有库

2019-01-05  本文已影响0人  胡二囧

本文梳理了一些使用Nexus搭建Maven私服的方法。Maven私服Nexus的作用,主要是为了节省资源,在内部作为Maven开发资源共享服务器来使用。另外Nexus3和Nexus2之间存在较大差异,因此本方法只适用于Nexus2.x,Nexus3请绕开。

更多说明请参考官网help:https://help.sonatype.com/repomanager3/installation/run-as-a-service

在Ubuntu8.2下搭建Nexus2

下载Nexus

通过root用户进去Ubuntu server

$ cd /opt
$ wget https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.14.0-04-mac.tgz

注意:

  1. 使用wget下载时比较慢,下载过程可以使用迅雷下载,之后再拷贝到/opt下
  2. Nexus并没有提供Linux版本,但可以使用mac版或者unix版,此处我们直接使用mac版
  3. 官网地址:https://www.sonatype.com/download-oss-sonatype
选择Nexus版本

启动

环境准备,启动Nexus之前,必须先完成JDK环境的配置。此处略过

$ cd /opt/
$ tar -zxvf nexus-3.14.0-04-mac.tgz
$ mv nexus-3.14.0-04 nexus3
$ cd /opt/nexus3/bin
$ vi nexus.rc

最后是将run_as_user设为root,否则root用户将无法启动Nexus服务:

run_as_user="root"

vi的使用方式请自行百度

run_as_user

站点的其他配置文件设置命令如下,可以按需修改:

$ vi /opt/nexus3/etc/nexus-default.properties
nexus-default

保存退出之后,即可启动nexus,命令如下:

$ ./nexus start

启动之后即可以访问:
http://127.0.0.1:8081/

或者

http://ip:8081/

以上,Nexus的服务器就算搭建起来了

配置Nexus

默认的管理员账户:admin,密码:admin123

关于仓库的类型介绍:

创建仓库

创建仓库 起个名字 生成仓库链接

生成的仓库链接将会在项目中用到:http://10.30.11.56:8081/nexus/content/repositories/Component/

在AndroidStudio集成

打包上传到远程库

为了使module的gradle尽量整洁,将打包配置信息单独放置在==maven-release-aar.gradle==中


maven-release-aar声明位置

以下为maven-release-aar.gradle的内容

// 1.maven-插件
apply plugin: 'maven'
// 2.maven-信息
ext {
    PUBLISH_GROUP_ID = 'com.inspur'
    PUBLISH_ARTIFACT_ID = 'cpaframe'//组件名
    PUBLISH_VERSION = '1.0.0.1'//组件版本
    PUBLISH_PACHAGE = 'jar'//打包类型,根据场景可选jar或aar
}
// 3.maven-路径
uploadArchives {
    repositories.mavenDeployer {
        //指定maven仓库url
        repository(url: "http://10.30.11.56:8081/nexus/content/repositories/Component/") {
            //Nexus登录默认用户名和密码
            authentication(userName: "admin", password: "admin123")
        }
        pom.project {
            groupId project.PUBLISH_GROUP_ID
            artifactId project.PUBLISH_ARTIFACT_ID
            version project.PUBLISH_VERSION
            pom.packaging = PUBLISH_PACHAGE
        }
    }
}

//aar包内包含注释
task androidSourcesJar(type: Jar) {
    classifier = 'sources'
    from android.sourceSets.main.java.sourceFiles
}

artifacts {
    archives androidSourcesJar
}

最后使用Terminal执行如下命令:

gradlew uploadArchives //win下执行
./gradlew uploadArchives //mac下执行
打包成功 Nexus管理界面可见

以上,打包上传的过程完成

更新组件版本

每次有更新时,只需要修改PUBLISH_VERSION的值,再使用Terminal执行gradlew命令即可。

注意:每次打包的版本不要和远程库中的版本号有重复,否则会看到以下结果:

> Task :app:uploadArchives FAILED
Could not transfer artifact com.inspur:cpaframe:aar:1.0.0.3 from/to remote (http://10.30.11.56:8081/nexus/content/repositories/Component/): Fa
iled to transfer file: http://10.30.11.56:8081/nexus/content/repositories/Component/com/inspur/cpaframe/1.0.0.3/cpaframe-1.0.0.3.aar. Return c
ode is: 400, ReasonPhrase: Bad Request.
Could not transfer artifact com.inspur:cpaframe:pom:1.0.0.3 from/to remote (http://10.30.11.56:8081/nexus/content/repositories/Component/): Fa
iled to transfer file: http://10.30.11.56:8081/nexus/content/repositories/Component/com/inspur/cpaframe/1.0.0.3/cpaframe-1.0.0.3.pom. Return c
ode is: 400, ReasonPhrase: Bad Request.
Could not transfer artifact com.inspur:cpaframe:jar:sources:1.0.0.3 from/to remote (http://10.30.11.56:8081/nexus/content/repositories/Compone
nt/): Failed to transfer file: http://10.30.11.56:8081/nexus/content/repositories/Component/com/inspur/cpaframe/1.0.0.3/cpaframe-1.0.0.3-sourc
es.jar. Return code is: 400, ReasonPhrase: Bad Request.


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:uploadArchives'.
> Could not publish configuration 'archives'
   > Failed to deploy artifacts: Could not transfer artifact com.inspur:cpaframe:aar:1.0.0.3 from/to remote (http://10.30.11.56:8081/nexus/con
tent/repositories/Component/): Failed to transfer file: http://10.30.11.56:8081/nexus/content/repositories/Component/com/inspur/cpaframe/1.0.0
.3/cpaframe-1.0.0.3.aar. Return code is: 400, ReasonPhrase: Bad Request.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full ins
ights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

因为默认情况下,远程库是将部署策略设置为禁止重新部署的关闭的,如下:

Deployment Policy

使用远程库

方法很简单,和开源库的使用方法类似,以上面的Component为例:

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "http://10.30.11.56:8081/nexus/content/repositories/Component/"
        }
    }
}


dependencies {
    ...
    implementation 'com.inspur:cpaframe:1.0.0.1'
}

列举一些好处

一下是pom的文件结构

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.inspur</groupId>
  <artifactId>cpaframe</artifactId>
  <version>1.0.0.4</version>
  <packaging>aar</packaging>
  <dependencies>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>appcompat-v7</artifactId>
      <version>26.1.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.github.bumptech.glide</groupId>
      <artifactId>okhttp3-integration</artifactId>
      <version>1.5.0</version>
      <type>aar</type>
      <scope>compile</scope>
      <exclusions>
        <exclusion>
          <artifactId>*</artifactId>
          <groupId>*</groupId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
      <version>3.12.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.github.bumptech.glide</groupId>
      <artifactId>glide</artifactId>
      <version>4.8.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>jp.wasabeef</groupId>
      <artifactId>glide-transformations</artifactId>
      <version>4.0.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.7</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.android.support</groupId>
      <artifactId>support-annotations</artifactId>
      <version>26.1.0</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>
打包出的文件结构是否一目了然
上一篇下一篇

猜你喜欢

热点阅读