Windows7下安装Maven和部署Nexus

        在开发过程中配置Maven能够省去构建项目时需要重复迁移大量依赖包的麻烦,同时能同maven很好的了解不同依赖包之间的相互依赖关系,为开发过程提供了极大的便利。同时Maven也能提供很方便的项目发布方式,从而节约了开发环节中的启动项目与发布导出项目的大量时间和精力。

Windows7下安装Maven

        在Maven官网中可以很方便找到各个版本与不同操作系统下所需的maven文件,下载完成后解压即可使用。为了方便在Windows系统下的使用,可以在环境变量中定义 MAVEN_HOME环境变量,变量值为maven的解压目录。然后在path环境变量的最后加入 %MAVEN_HOME%\bin 即可完成安装配置。

        进入系统盘(c:)下,在当前的用户目录下找到 .m2 文件夹(例如C:\Users\Administrator\.m2),如果没有也可以自己建一个同名文件夹,当使用maven更新过项目的依赖包后 .m2文件夹下会生成 repository 文件夹,此为本机上的maven仓库,其中装着从服务器上同步下来的各个版本的依赖包。

        打开命令窗口输入 mvn -version 出现相关maven参数与版本信息,即为安装配置成功。

官网下载地址:http://maven.apache.org/download.cgi


部署Nexus私服

        直接使用maven时,如果有相关依赖的改变,maven则会从官方仓库中进行下载相关依赖包。在一个协同项目中如果所有开发人员都直接从官方仓库中获取依赖包,将会浪费大量的不必要流量和时间;同时,如果开发过程中还存在多个项目之间的相互依赖,例如:A项目是一个底层服务项目,导出为jar包被B项目调用。那么每次A项目进行升级后都必须通知B项目的开发人员进行相关的手动升级,这非常的麻烦。

        因此,maven官方非常鼓励开发组织与公司内部构建自己的私服,通过私服链接开发人员与官方仓库。开发人员每次从私服上获取最新的依赖包,同时将自己开发的底层项目直接发布到私服上,供其他项目引用,将会非常的方便。

        选用Nexus来搭建一个maven私服是非常好的选择。Nexus可以通过两种方式来搭建私服:一、通过官方下载的服务器版直接启动Nexus服务器;二、将Nexus的war包直接放到服务器上,也可完成Nexus项目的部署。此处,以第一种方法为例来搭建一个自己的私服。

        在下载的压缩包解压后,进入 bin 目录下的 jsw 目录即可看到各个系统下的操作目录,进入对应操作系统的目录下即可找到Nexus的操作命令。从命令窗口或是直接双击运行 start-nexus.bat 都可以启动Nexus服务器。在浏览器中输入 http://127.0.0.1:8081/nexus/ 出现Nexus的欢迎界面即为启动成功。其他脚本分别对应安装,停止,暂停,恢复,卸载Nexus服务。其中使用 install-nexus.bat 脚本可将Nexus服务注册到系统服务,然后就可以在Windows的服务界面中看到nexus服务,并进行相关启动/停止控制。

        Nexus自身已经具备了相当完善的操作功能以提供私服的维护与控制,通过默认用户 admin/admin123 可以登录Nexus并进行仓库的管理。

        搭建好私服后需要对相关配置文件进行配置才能在开发中通过私服来更新项目依赖包与发布项目。

官网下载地址:http://www.sonatype.org/nexus/go/


配置setting.xml和pom.xml文件

        如果搭建了私服则需要在 .m2 文件夹中加入名为 setting.xml 的配置文件进行私服的配置。

setting.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">

    <profiles>
        <profile>
            <id>develop</id>
            <repositories>
                <repository>
                    <id>local-nexus</id>
                    <url>http://127.0.0.1:8081/nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>develop</activeProfile>
    </activeProfiles>

	<servers>
		<server>
	        <id>nexus-releases</id>
	        <username>admin</username>
	        <password>admin123</password>
	    </server>
	    <server>
	        <id>nexus-snapshots</id>
	        <username>admin</username>
	        <password>admin123</password>
	    </server>
	</servers>
    
</settings>

        项目中的 pom.xml 文件也需要进行相关的配置。

pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <!-- Project的版本 -->
    <modelVersion>4.0.0</modelVersion>
    <!-- 组织标识 -->
    <groupId></groupId>
    <!-- 项目ID -->
    <artifactId></artifactId>
    <!-- 使用Maven打包的类型 war,jar,pom等 -->
    <packaging></packaging>
    <!-- Project的版本 -->
    <version>1.0-SNAPSHOT</version>
    <!-- Project的名称 -->
    <name></name>
    <!-- Project的访问URL -->
    <url>http://</url>

    <!-- 定义相关参数 -->
    <properties>
        <!-- 配置相关依赖包的版本参数 -->
        <spring.version>4.0.5.RELEASE</spring.version>

        <!-- Plugin的属性定义 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- JDK的版本参数 -->
        <jdk.version>1.7</jdk.version>
    </properties>

    <!-- Build相关参数 -->
    <build>
        <!-- 文件打包后的名称 -->
        <finalName></finalName>
        <!-- Plugin的配置 -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                        <!-- 是否提示警告 -->
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <!-- 项目中的依赖关系配置 -->
    <dependencies>
        <dependency>
            <!-- 依赖包的组织标识 -->
            <groupId></groupId>
            <!-- 依赖包的项目ID -->
            <artifactId></artifactId>
            <!-- 依赖包的项目版本 -->
            <version>1.8.3</version>
            <!-- 依赖包的例外配置 可选 -->
            <exclusions>
                <exclusion>
                    <groupId></groupId>
                    <artifactId></artifactId>
                </exclusion>
            </exclusions>
            <!-- 依赖包的引入范围 test等 可选 -->
            <scope></scope>
        </dependency>

    </dependencies>

    <!--Maven部署配置-->
    <!-- 与setting文件中的Server id相对应 -->
    <distributionManagement>
        <!-- 稳定仓库 -->
        <repository>
            <id>nexus-releases</id>
            <name>Release</name>
            <url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url>
        </repository>
        <!-- 沙盒仓库 -->
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Snapshot</name>
            <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
</project>


        profile 定义了更新时使用的相关配置,包括使用的仓库 repository;activeProfile 定义了开发中使用的 profile 配置。server 为项目发布时的服务器使用参数,其中的 id 需要与 pom.xml 中定义的 repository 的 id 相对应。


注:在首次通过maven进行deploy的发布操作时,经常遇到 Return code is: 401, ReasonPhrase: Unauthorized 的错误。

错误详细提示:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.906 s
[INFO] Finished at: 2015-02-14T16:26:39+08:00
[INFO] Final Memory: 19M/181M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project core: Failed to deploy artifacts: Could not transfer artifact com.darknight:core:jar:1.0-20150214.082639-28 from/to nexus-snapshots (http://127.0.0.1:8081/nexus/content/repositories/snapshots/): Failed to transfer file: http://127.0.0.1:8081/nexus/content/repositories/snapshots/com/darknight/core/1.0-SNAPSHOT/core-1.0-20150214.082639-28.jar. Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

        此为发布项目的目标server没有正确配置而导致的,请检查 setting.xml 和 pom.xml 中的发布配置是否相匹配,且用户名是否具有对应的Nexus操作权限(对该错误的解释原文见 http://maven.apache.org/guides/mini/guide-deployment-security-settings.html)。


转载于:https://my.oschina.net/u/1156626/blog/378642

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值