使用Nexus2搭建私有库

本文梳理了一些使用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

关于仓库的类型介绍:

  • hosted 类型的仓库,内部项目的发布仓库
  • releases 内部的模块中release模块的发布仓库
  • snapshots 发布内部的SNAPSHOT模块的仓库
  • 3rd party 第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去
  • proxy 类型的仓库,从远程中央仓库中寻找数据的仓库
  • group 类型的仓库,组仓库用来方便我们开发人员进行设置的仓库

创建仓库

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

生成的仓库链接将会在项目中用到: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'
}

列举一些好处

  • 更方便的版本控制。
  • 更有效的使用gradle构建项目。
  • 使用远程库时默认不会被编译到jar或aar中,可以有效的减小发布版本时的工作量。
  • 组件所依赖的组件信息也会在打包时被保存在pom文件中,在构建项目时可以进一步减小工作量。

一下是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>
打包出的文件结构是否一目了然
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,688评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,559评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,749评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,581评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,741评论 3 271
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,684评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,122评论 2 292
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,847评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,441评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,939评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,333评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,783评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,275评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,830评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,444评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,553评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,618评论 2 249

推荐阅读更多精彩内容