解决的问题(Ant在项目中编译后打JAR包直接发布到Nexus中),Ant、 IVY不多说说这些网上都有资料说明,今天在来点干货,上码。

1、ivy.xml

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

2、ivysettings.xml

 
  
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <ivysettings> 
  3.   <settings defaultResolver="nexus"/> 
  4.   <credentials host="localhost" 
  5.                       realm="Sonatype Nexus Repository Manager" 
  6.                       username="yourname" passwd="yourpwd"/> 
  7.   <property name="nexus-public" value="http://localhost:8081/nexus/content/groups/public"/> 
  8.   <property name="nexus-releases" value="http://localhost:8081/nexus/content/repositories/releases"/> 
  9.   <property name="nexus-snapshots" value="http://localhost:8081/nexus/content/repositories/snapshots"/> 
  10.   <resolvers> 
  11.     <ibiblio name="nexus" m2compatible="true" root="${nexus-public}"/> 
  12.     <ibiblio name="nexus-releases" m2compatible="true" root="${nexus-releases}"/> 
  13.     <ibiblio name="nexus-snapshots" m2compatible="true" root="${nexus-snapshots}" /> 
  14.   </resolvers> 
  15. </ivysettings> 

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

 
  
  1. <project default="create-jar" xmlns:ivy="antlib:org.apache.ivy.ant"> 
  2.     <property name="build.dir" value="classes" /> 
  3.     <property name="src.dir" value="src" /> 
  4.     <property name="lib.dir" value="lib" /> 
  5.     <property name="jar.name" value="tagedb" /> 
  6.     <property name="publish.revision" value="1.0" /> 
  7.      
  8.     <path id="master-classpath"> 
  9.         <fileset dir="${lib.dir}"> 
  10.             <include name="*.jar" /> 
  11.         </fileset> 
  12.     </path> 
  13.  
  14.     <target name="clean" description="Clean output dirs (build, weblib, dist)"> 
  15.         <echo>clean build dir</echo> 
  16.         <delete dir="${build.dir}" /> 
  17.         <mkdir dir="${build.dir}" /> 
  18.         <delete dir="dist" /> 
  19.         <mkdir dir="dist" /> 
  20.     </target> 
  21.      
  22.     <!-- 项目编译打成jar包 --> 
  23.     <target name="create-jar" depends="clean"> 
  24.         <javac srcdir="${src.dir}" destdir="${build.dir}"> 
  25.             <classpath refid="master-classpath" /> 
  26.         </javac> 
  27.         <jar destfile="dist/${jar.name}.jar" basedir="${build.dir}" /> 
  28.     </target> 
  29.      
  30.     <!-- ivy.xml转换成pom的配置文件(可以手动以pom文件的形式上传jar到nexus仓库中) --> 
  31.     <target name="prepare" description="Generate POM" depends="create-jar"> 
  32.         <!-- Generate the Maven POM --> 
  33.         <ivy:makepom ivyfile="ivy.xml" pomfile="${jar.name}.pom" /> 
  34.     </target> 
  35.      
  36.     <!-- nexus仓库中的jar下载到缓存中 --> 
  37.     <target name="resolve" description="retreive dependencies with ivy"> 
  38.         <!-- 
  39.         <ivy:configure file="../ivysettings.xml" /> 
  40.         <ivy:resolve file="my-ivy.xml" conf="default, myconf" /> 
  41.         --> 
  42.         <ivy:resolve /> 
  43.     </target> 
  44.      
  45.     <!-- 取得缓存中的jar放到指定目录 --> 
  46.     <target name="retrieve" depends="resolve"> 
  47.         <ivy:retrieve pattern="lib/[artifact]-[revision].[ext]" conf="default" /> 
  48.     </target> 
  49.      
  50.     <!--* 用ant编译后打包发布到nexus中 *--> 
  51.     <target name="publish" depends="resolve,prepare" description="Upload to Nexus"> 
  52.         <ivy:publish resolver="nexus-releases" pubrevision="${publish.revision}"> 
  53.             <artifacts pattern="dist/[artifact].[ext]" /> 
  54.         </ivy:publish> 
  55.     </target> 
  56.      
  57. </project> 

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

 

开发二组 Polaris(海侠)