Linux--Maven和nexus私服搭建

  Maven是基于项目对象模型(POM),管理项目的构建,报告和文档的软件项目管理工具。Nexus是一个高效的Maven私有服务器,通过POM文件或者Maven的setting.xml指向私服地址,这样我们便可以通过私服管理jar包;本文就介绍了如何在linux安装maven和nexus私服。

软件准备

下载后上传:

  1. 本博客Maven版本使用apache-maven-3.2.3-src.tar.gz 下载地址https://archive.apache.org/dist/maven/maven-3/3.2.3/source/apache-maven-3.2.3-bin.tar.gz
  2. Nexus版本使用nexus-2.11.4-01-bundle.tar.gz 下载地址https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.4-01-bundle.tar.gz

Maven搭建

  • 一、解压安装
  1. 解压: tar -zxvf apache-maven-3.2.3-bin.tar.gz
  2. 移动并改名:mv apache-maven-3.2.3 /opt/maven
  3. 添加环境变量:vim /etc/profile,添加下面两行
    1
    2
    export MAVEN_HOME=/opt/maven
    export PATH=${MAVEN_HOME}/bin:$PATH
  • 二、检查是否安装好

  • 三、修改配置
  1. 把maven的repository目录指定到其他目录,即修改maven安装目录下conf中的配置文件settings.xml文件会下载很多的依赖jar到该目录,建议修改到目录到数据盘

  2. 修改mirror地址为开源中国(速度快一些)

Nexus私服搭建

  1. 解压tar.gz包:tar -zxvf nexus-2.11.0-bundle.tar.gz
  2. 移动两个文件到同一目录,切换目录,到nexus-2.11.4-01

  3. 到bin目录启动,nexus

  4. 查看详细参数

  5. 尝试第一次启动,服务,通过上面的参数start。发现如下提示,提示我们最好不要使用root启动。这里我们有两种处理方式:

  6. 第一种:修改nexus配置文件RUN_AS_USER="root",然后启动服务即可,这种方法比较简便。

  7. 第二种:使用非root账号,这里我们使用用户momo,修改RUN_AS_USER="momo"不然启动方式还是root。

  8. 回退到目录nexus-2.11.4-01,修改所属用户组为momo。注意此时只修改了nexus-2.11.4-01目录。

    重启服务,会失败:


    查看控制台/nexus console日志信息->Nexus work directory already in use

    1
    2
    3
    4
    5
    jvm 1    | 2016-04-20 22:54:46,396+0800 WARN  [jetty-main-1] *SYSTEM org.eclipse.jetty.util.component.AbstractLifeCycle - FAILED org.eclipse.jetty.server.Server@4409a155: java.lang.IllegalStateException: **Nexus work directory already in use**: /usr/local/nexus/sonatype-work/nexus
    jvm 1 | java.lang.IllegalStateException: Nexus work directory already in use: /usr/local/nexus/sonatype-work/nexus
    jvm 1 | at com.google.common.base.Preconditions.checkState(Preconditions.java:200) ~[na:na]
    jvm 1 | at org.sonatype.nexus.webapp.WebappBootstrap.contextInitialized(WebappBootstrap.java:117) ~[na:na]
    jvm 1 | at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:782) ~[jetty-server-8.1.16.v20140903.jar:8.1.16.v20140903]
  9. 所以我们需要把sonatype-work所属权限也分配给momo用户(在这里提出这个错误主要因为我失误过

    重启后成功,2654为PID:
    Nexus安装成功
    Nexus私服安装完成

  10. 配置Nexus Repository

    1. 打开WEB管理界面:http://120.24.163.105:8081/nexus(*自己的私服请换掉IP*)
    2. 点击右上角Log In进行登录,默认帐号:admin、密码:admin123(记得修改)
    3. 点击左侧Repositories -> central仓库 -> Configuration -> Download Remote Indexes=True -> Save表示下载远程仓库的索引
    4. 右键central仓库 -> Update Index更新远程仓库索引到本地,这一步能够加速本地索引
      Nexus配置
  • 说明:
  • 1.新搭建的neuxs是空的,需手动和远程中心库进行同步,nexus默认是关闭远程索引下载,最重要的一件事情就是开启远程索引下载。
  • 2.列表中的三个仓库Apache SnapshotsCodehaus SnapshotsMaven Central,然后分别修改Download Remote Indexes为true。然后Repari Index(下载远程的索引文件)Update Index(更新远程仓库索引到本地)
  • 3.新建公司的内部仓库,步骤为Repositories –> Add –> Hosted Repository,在页面的下半部分输入框中填入Repository IDRepository Name即可,另外把Deployment Policy设置为Allow Redeploy。

Maven和Nexus使用

  • 一、这里准备了一个自己创建的项目

通过tree查看目录结构

  1. 如果没有tree命令,可以使用yum install -y tree安装即可。
  2. 该项目是按照Maven约定目录结构建立。
  • 二、Maven构建项目约定

src/main/java —— 存放项目的.java文件
src/main/resources —— 存放项目资源文件,如spring, log4j,数据库配置文件
src/test/java —— 存放所有测试.java文件,如JUnit测试类
src/test/resources —— 测试资源文件
target —— 项目输出位置
pom.xml

  • 二、使用命令清理mvn clean, target文件夹消失了

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    [root@iZ94r8hgrjcZ Person]# mvn clean
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building person 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ person ---
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 0.432 s
    [INFO] Finished at: 2016-04-20T23:46:30+08:00
    [INFO] Final Memory: 5M/15M
    [INFO] ------------------------------------------------------------------------
  • 三、执行编译:mvn compile,target文件夹创建了,并且Person.java被编译了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@iZ94r8hgrjcZ Person]# mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building person 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ person ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ person ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.069 s
[INFO] Finished at: 2016-04-20T23:57:05+08:00
[INFO] Final Memory: 7M/17M
[INFO] ------------------------------------------------------------------------

  • 四、执行测试:mvn test,执行了单元测试,target中有多了几个文件夹(测试报告等)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@iZ94r8hgrjcZ Person]# mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building person 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ person ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ person ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ person ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ person ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ person ---
[INFO] Surefire report directory: /home/up/Person/target/surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.xxo.maven.PersonTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.866 s
[INFO] Finished at: 2016-04-21T00:01:35+08:00
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------
  • 五、执行打包:mvn package
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[root@iZ94r8hgrjcZ Person]# mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building person 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ person ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ person ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ person ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ person ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ person ---
[INFO] Surefire report directory: /home/up/Person/target/surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.xxo.maven.PersonTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.066 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ person ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.250 s
[INFO] Finished at: 2016-04-21T00:03:42+08:00
[INFO] Final Memory: 8M/20M
[INFO] ------------------------------------------------------------------------

  • 六、编译:mvn install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@iZ94r8hgrjcZ Person]# mvn install -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building person 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ person ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ person ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ person ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ person ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ person ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ person ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ person ---
[INFO] Installing /home/up/Person/target/person-0.0.1-SNAPSHOT.jar to /home/up/repo/com/xxo/maven/person/0.0.1-SNAPSHOT/person-0.0.1-SNAPSHOT.jar
[INFO] Installing /home/up/Person/pom.xml to /home/up/repo/com/xxo/maven/person/0.0.1-SNAPSHOT/person-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.306 s
[INFO] Finished at: 2016-04-21T00:15:51+08:00
[INFO] Final Memory: 8M/19M
[INFO] ------------------------------------------------------------------------

-Dmaven.test.skip=true,可以忽略test。或者mvn install -DskipTests或者在配置文件中配置:

1
2
3
4
5
6
7
8
9
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>

  • 七、发布到远程私服:mvn deploy

一、添加到,下面配置到项目pom一般我们搭建的maven项目是通过继承的所以该文件一般放置到父级即可

1
2
3
4
5
6
7
8
9
10
11
12
13
<distributionManagement>
<snapshotRepository>
<id>releases</id>
<name>User Porject Snapshot</name>
<url>http://120.24.163.105:8081/nexus/content/repositories/snapshots/</url>
<uniqueVersion>true</uniqueVersion>
</snapshotRepository>
<repository>
<id>snapshots</id>
<name>User Porject Release</name>
<url>http://120.24.163.105:8081/nexus/content/repositories/releases/</url>
</repository>
</distributionManagement>

二、该账号密码配置到settings.xml,Deployment用户默认密码为deployment123(自己记得修改账号密码,注:该server id必须与上面相同

1
2
3
4
5
6
7
8
9
10
11
12
<servers>
<server>
<id>releases</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<server>
<id>snapshots</id>
<username>deployment</username>
<password>deployment123</password>
</server>
</servers>

三、发布到远端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
[root@iZ94r8hgrjcZ Person]# mvn deploy -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building person 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ person ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ person ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ person ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ person ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ person ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ person ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ person ---
[INFO] Installing /home/up/Person/target/person-0.0.1-SNAPSHOT.jar to /home/up/repo/com/xxo/maven/person/0.0.1-SNAPSHOT/person-0.0.1-SNAPSHOT.jar
[INFO] Installing /home/up/Person/pom.xml to /home/up/repo/com/xxo/maven/person/0.0.1-SNAPSHOT/person-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ person ---
Downloading: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/0.0.1-SNAPSHOT/maven-metadata.xml
Downloaded: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/0.0.1-SNAPSHOT/maven-metadata.xml (769 B at 6.6 KB/sec)
Uploading: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/0.0.1-SNAPSHOT/person-0.0.1-20160421.022446-2.jar
Uploaded: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/0.0.1-SNAPSHOT/person-0.0.1-20160421.022446-2.jar (3 KB at 20.0 KB/sec)
Uploading: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/0.0.1-SNAPSHOT/person-0.0.1-20160421.022446-2.pom
Uploaded: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/0.0.1-SNAPSHOT/person-0.0.1-20160421.022446-2.pom (2 KB at 15.2 KB/sec)
Downloading: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/maven-metadata.xml
Downloaded: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/maven-metadata.xml (279 B at 10.9 KB/sec)
Uploading: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/0.0.1-SNAPSHOT/maven-metadata.xml
Uploaded: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/0.0.1-SNAPSHOT/maven-metadata.xml (769 B at 14.4 KB/sec)
Uploading: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/maven-metadata.xml
Uploaded: http://120.24.163.105:8081/nexus/content/repositories/snapshots/com/xxo/maven/person/maven-metadata.xml (279 B at 7.4 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.505 s
[INFO] Finished at: 2016-04-21T10:24:46+08:00
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------

Nexus已发布到远程

  • 八、手动上传项目构件到Nexus中

选择Nexus某个仓库的Artifact Upload,填写相关信息即可,如下图所示(一般不建议这么做):

Nexus已发布到远程

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器