使用nexus、ant和ivy創建開發環境

新公司技術部門已經有半年了,目前項目管理仍是使用svn管理全部的源碼和依賴,jar包直接丟到svn的lib目錄下,每次公共jar包更新時,因爲目前使用的jackson使用默認的配置,必須更新每一個項目的jar包,公共依賴愈來愈有必要,因而趁週末研究了相關的工具使用,筆記整理以下:web

nexus搭建

在nexus官網下載最新版的nexus:nexus-2.11.4-01-bundle.zip 直接解壓,而後經過cmd切換目前到nexus的bin目錄,執行spring

nexus install

便可將nexus安裝爲windows服務,而後再用apache

nexus start

便可啓動服務,正常安裝和啓動以下:windows

輸入圖片說明

輸入圖片說明

若是出現以下異常信息:maven

輸入圖片說明

請用管理員權限運行CMD便可。svn

啓動後使用:http://localhost:8081/nexus 打開主頁,默認管理員:密碼是admin:admin123工具

輸入圖片說明 ###ivy-setting.xml 此文件主要配置nexus的認證信息,用於公共jar包的發佈;以及ivy依賴的倉庫地址。ui

<ivysettings>
    <properties file="${basedir}/ivy-settings.properties"/>
    <settings defaultResolver="local"/>
    <credentials host="127.0.0.1" username="${repo.user}" passwd="${repo.user.password}" realm="Sonatype Nexus Repository Manager"/>
    <property name="nexus-public" value="${repo.url}/content/groups/public"/>
    <property name="nexus-releases" value="${repo.url}/content/repositories/releases"/>
    <property name="nexus-snapshots" value="${repo.url}/content/repositories/snapshots"/>
    <resolvers>
        <ibiblio name="local" m2compatible="true" root="${nexus-public}"/>
        <ibiblio name="releases" m2compatible="true" root="${nexus-releases}"/>
        <ibiblio name="snapshots" m2compatible="true" root="${nexus-snapshots}"/>
    </resolvers>
</ivysettings>

###ivy.xml 此文件主要是設置發佈的jar包信息和工程的依賴信息,其中依賴包加上conf="default"將不會引入resources.jar和doc.jarurl

<ivy-module version="2.0">
    <info organisation="${organisation}.2" module="${module}"/>
   <publications>
        <artifact name="${module}" type="jar"/>
    </publications>
    <dependencies>
        <dependency org="commons-lang" name="commons-lang" rev="2.0" conf="default"/>
        <dependency org="org.springframework" name="spring-web" rev="4.1.8.RELEASE" conf="default"/>
        <dependency org="commons-cli" name="commons-cli" rev="1.0" conf="default"/>
        <dependency org="com.ichson" name="ichson-um" rev="2.0" conf="default"/>
    </dependencies>
</ivy-module>

###ivy-settings.properties 主要項目的信息,便於ivy.xml和ivy-setting.xml在各個工程的相對通用。spa

organisation=com.ichson
module=ichson-um
pubrevision=2.0
repo.url=http://127.0.0.1:8081/nexus
repo.user=admin
repo.user.password=admin123

###build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:ivy="antlib:org.apache.ivy.ant" name="test-ivy" default="init-ivy"
         basedir="." xmlns="antlib:org.apache.tools.ant">
    <property name="ivy.install.version" value="2.4.0"/>
    <property name="ivy.home" value="${user.home}/.ant"/>
    <property name="ivy.jar.dir" value="${ivy.home}/lib"/>
    <property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar"/>
    <condition property="ivy.exist">
        <available file="${ivy.jar.file}"/>
    </condition>

    <target name="download-ivy" if="!ivy.exist">
        <echo message="ivy.exist:${ivy.exist}"></echo>
        <mkdir dir="${ivy.jar.dir}"/>
        <get
                src="http://maven.oschina.net/service/local/repositories/central/content/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
                dest="${ivy.jar.file}" usetimestamp="true"/>
    </target>
    <target name="init-ivy" depends="download-ivy">
        <path id="ivy.lib.path">
            <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
        </path>
        <taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant"
                 classpathref="ivy.lib.path"/>
        <ivy:settings file="${basedir}/ivy-settings.xml"/>
    </target>

    <target name="clean-lib" depends="init-ivy">
        <ivy:cleancache/>
        <delete dir="lib"></delete>
    </target>

    <target name="get-lib" depends="init-ivy">
        <ivy:cleancache/>
        <ivy:retrieve />
    </target>

    <target name="publish" description="publish" depends="init-ivy">
        <ivy:retrieve/>
        <ivy:publish resolver="releases" pubrevision="${pubrevision}"
                     overwrite="true" update="true" forcedeliver="true" status="release">
            <artifacts pattern="dist/[module].[ext]"/>
        </ivy:publish>
    </target>
</project>

###build.xml的target說明

  • download-ivy:下載指定的ivy.jar
  • init-ivy:引入ivy.jar,以後才能ivy相關的ant命令
  • clean-lib:清理ivy的cache,從新從依賴庫下載jar包
  • get-lib:下載依賴包
  • publish:發佈公告jar包
相關文章
相關標籤/搜索