Maven环境搭建

Maven安装

  1. 去Maven官网http://maven.apache.org/下载Maven.
    由于我是通过ssh连接虚拟机操作的,所以找到它的地址直接这样下载(不要直接复制以下命令,这种链接很快就会失效)
    curl --remote-name http://mirrors.tuna.tsinghua.edu.cn/amaven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
    
  2. 在根目录新建一个crawler文件夹, 把maven解压到那里
    mkdir /crawler
    tar -xf apache-maven-3.3.9-bin.tar.gz -C /crawler
    
  3. 配置Maven环境变量, 在/etc/profile中添加
    export M2_HOME=/crawler/apache-maven-3.3.9/
    export PATH=$PATH:$M2_HOME/bin
    
  4. 使环境变量立即生效:
    source /etc/profile
    
  5. 测试Maven是否安装成功:
    mvn -version
    
    可以看到:
    Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
    Maven home: /crawler/apache-maven-3.3.9
    Java version: 1.7.0_101, vendor: Oracle Corporation
    Java home: /usr/lib/jvm/java-7-openjdk/jre
    Default locale: en_US, platform encoding: ANSI_X3.4-1968
    OS name: "linux", version: "4.5.4-1-arch", arch: "amd64", family: "unix"
    

Nexus安装

  1. 去官网http://www.sonatype.org/nexus/下载最新版本. ``` curl --remote-name https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.13.0-01-bundle.tar.gz
  2. 解压.
    tar -xf nexus-2.13.0-01-bundle.tar.gz -C /crawler/
    
  3. 启动Nexus
    cd /crawler/nexus-2.13.0-01/
    ./bin/nexus start
    
  4. 测试Nexus是否安装成功
    在浏览器访问http://192.168.1.111:8081/nexus/可以看到欢迎界面.

可能会遇到两个问题:

  1. WARNING - NOT RECOMMENDED TO RUN AS ROOT:
    解决方案:vi /etc/profile , 加入export RUN_AS_USER=root,source /etc/profile
  2. Failed to start Nexus OSS.这可能是由于创建nexus目录的时候是用的root用户,而使用的时候却不是.通过sudo chown -R user:user改变目录属性.

Maven与Nexus结合

配置/crawler/apache-maven-3.3.9/conf/settings.xml:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/crawler/.m2/repository</localRepository>
    <servers>
        <server>
            <id>Releases</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
        <server>
            <id>Snapshots</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
    </servers>
    <mirrors>
        <mirror>
            <id>crawler-nexus</id>
            <mirrorOf>*</mirrorOf>
            <url>http://192.168.1.13:8081/nexus/content/groups/public/</url>
        </mirror>
    </mirrors>

    <profiles>
        <profile>
            <id>crawler-nexus</id>
            <repositories>
                <repository>
                    <id>public</id>
                    <name>Public Repositories</name>
                    <url>http://192.168.1.13:8081/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
                <repository>
                    <id>Snapshot</id>
                    <name>Snapshot Repository</name>
                    <url>http://192.168.1.13:8081/nexus/content/repositories/snapshots/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>public</id>
                    <name>Public Repositories</name>
                    <url>http://192.168.1.13:8081/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </pluginRepository>
                <pluginRepository>
                    <id>Snapshot</id>
                    <name>Snapshot Repository</name>
                    <url>http://192.168.1.13:8081/nexus/content/repositories/snapshots/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>crawler-nexus</activeProfile>
    </activeProfiles>

</settings>

配置好settings.xml之后,新建一个最简单的Maven工程:

mvn archetype:generate -DgroupId=pub.willow.test -DartifactId=Test -DarchetypeArtifactId=maven-archetype-quickstart -D interactiveMode=flase

然后可以看到从Nexus里面下载需要的文件:

Downloaded: http://192.168.1.13:8081/nexus/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/1.0/maven-archetype-quickstart-1.0.pom (703 B at 0.9 KB/sec)

最后可以看到成功构建工程:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12:22 min
[INFO] Finished at: 2016-06-11T11:06:17+00:00
[INFO] Final Memory: 13M/49M
[INFO] ----------------------------------------------------------------------

进入工程运行测试用命试试:

cd Test/
mvn test

又是漫长的下载,最终结果:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running pub.willow.test.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14:15 min
[INFO] Finished at: 2016-06-11T12:15:48+00:00
[INFO] Final Memory: 13M/32M
[INFO] ------------------------------------------------------------------------

results matching ""

    No results matching ""