如何用Ant+IVY构建的包发布到Nexus

解决的问题(Ant在项目中编译后打JAR包直接发布到Nexus中)

1、ivy.xml

<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd"> 
    <info 
        organisation="com.targtime" 
        module="tagedb" 
        status="integration" revision="1.0"> 
    </info> 
    <!-- 定要发布的jar --> 
    <publications> 
        <artifact name="tagedb" type="jar" ext="jar" /> 
    </publications>

2、ivysettings.xml

<?xml version="1.0" encoding="UTF-8"?>   
<ivysettings> 
  <settings defaultResolver="nexus"/> 
  <credentials host="mvn.magicwall.org" 
                      realm="Sonatype Nexus Repository Manager" 
                      username="yourname" passwd="yourpwd"/> 
  <property name="nexus-public" value="http://mvn.magicwall.org:8081/nexus/content/groups/public"/> 
  <property name="nexus-releases" value="http://mvn.magicwall.org:8081/nexus/content/repositories/releases"/> 
  <property name="nexus-snapshots" value="http://mvn.magicwall.org:8081/nexus/content/repositories/snapshots"/> 
  <resolvers> 
    <ibiblio name="nexus" m2compatible="true" root="${nexus-public}"/> 
    <ibiblio name="nexus-releases" m2compatible="true" root="${nexus-releases}"/> 
    <ibiblio name="nexus-snapshots" m2compatible="true" root="${nexus-snapshots}" /> 
  </resolvers> 
</ivysettings>

3、build.xml(重点在这里)

<project default="create-jar" xmlns:ivy="antlib:org.apache.ivy.ant"> 
    <property name="build.dir" value="classes" /> 
    <property name="src.dir" value="src" /> 
    <property name="lib.dir" value="lib" /> 
    <property name="jar.name" value="tagedb" /> 
    <property name="publish.revision" value="1.0" /> 
     
    <path id="master-classpath"> 
        <fileset dir="${lib.dir}"> 
            <include name="*.jar" /> 
        </fileset> 
    </path> 
 
    <target name="clean" description="Clean output dirs (build, weblib, dist)"> 
        <echo>clean build dir</echo> 
        <delete dir="${build.dir}" /> 
        <mkdir dir="${build.dir}" /> 
        <delete dir="dist" /> 
        <mkdir dir="dist" /> 
    </target> 
     
    <!-- 项目编译打成jar包 --> 
    <target name="create-jar" depends="clean"> 
        <javac srcdir="${src.dir}" destdir="${build.dir}"> 
            <classpath refid="master-classpath" /> 
        </javac> 
        <jar destfile="dist/${jar.name}.jar" basedir="${build.dir}" /> 
    </target> 
     
    <!-- ivy.xml转换成pom的配置文件(可以手动以pom文件的形式上传jar到nexus仓库中) --> 
    <target name="prepare" description="Generate POM" depends="create-jar"> 
        <!-- Generate the Maven POM --> 
        <ivy:makepom ivyfile="ivy.xml" pomfile="${jar.name}.pom" /> 
    </target> 
     
    <!-- nexus仓库中的jar下载到缓存中 --> 
    <target name="resolve" description="retreive dependencies with ivy"> 
        <!-- 
        <ivy:configure file="../ivysettings.xml" /> 
        <ivy:resolve file="my-ivy.xml" conf="default, myconf" /> 
        --> 
        <ivy:resolve /> 
    </target> 
     
    <!-- 取得缓存中的jar放到指定目录 --> 
    <target name="retrieve" depends="resolve"> 
        <ivy:retrieve pattern="lib/[artifact]-[revision].[ext]" conf="default" /> 
    </target> 
     
    <!--* 用ant编译后打包发布到nexus中 *--> 
    <target name="publish" depends="resolve,prepare" description="Upload to Nexus"> 
        <ivy:publish resolver="nexus-releases" pubrevision="${publish.revision}"> 
            <artifacts pattern="dist/[artifact].[ext]" /> 
        </ivy:publish> 
    </target> 
     
</project>

最后用ant执行build.xml就可以再nexus中看到我们上次的jar了。

关于Zeno Chen

本人涉及的领域较多,杂而不精 程序设计语言: Perl, Java, PHP, Python; 数据库系统: MySQL,Oracle; 偶尔做做电路板的开发,主攻STM32单片机
此条目发表在Java分类目录。将固定链接加入收藏夹。