ant+ivy+nexus的使用

本文的大前提是先要搭建好nexus本地服务器。

工程目录结构


附件里是示例代码。

ivysettings_nexus.rar是ivysettings.xml连接nexus服务的文件代码。

关于配置截图请参考笔者的新浪博客( http://blog.sina.com.cn/s/blog_4f925fc30102v6k8.html)

ivysettings.xml文件代码如下:
<ivysettings> 
  <ivy:configure>
	<credentials host="127.0.0.1" realm="Sonatype Nexus Repository Manager" username="admin" passwd="admin123"/>
  </ivy:configure>
  <settings defaultResolver="defaultChain" defaultConflictManager="latest-revision" />
  <caches defaultCacheDir="F:/antlvy/javapro/ivy" />
  <property name="nexus-public" value="http://127.0.0.1:8081/nexus/content/groups/public"/> 
  <property name="nexus-releases" value="http://127.0.0.1:8081/nexus/content/repositories/releases"/> 
  <property name="nexus-snapshots" value="http://127.0.0.1:8081/nexus/content/repositories/snapshots"/>
  <property name="nexus-thirdparty" value="http://127.0.0.1:8081/nexus/content/repositories/thirdparty"/>
  <resolvers>
	<chain name="defaultChain" checkmodified="true" changingPattern=".*SNAPSHOT">
		<ibiblio name="nexus-public" m2compatible="true" usepoms="true" root="${nexus-public}" pattern="[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]" /> 
		<ibiblio name="nexus-releases" m2compatible="true" usepoms="true" root="${nexus-releases}" pattern="[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]" /> 
		<ibiblio name="nexus-snapshots" m2compatible="true" usepoms="true" root="${nexus-snapshots}" pattern="[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]" />
		<ibiblio name="nexus-thirdparty" m2compatible="true" usepoms="true" root="${nexus-thirdparty}" pattern="[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]" />
	</chain>
  </resolvers>
</ivysettings>


ivy.xml文件代码如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.    
-->
<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.shihuan"
        module="shihuanjava"
        status="integration">
	</info>
	
	<dependencies>
		<dependency org="log4j" name="log4j" rev="1.2.17" conf="default"></dependency>
		<dependency org="commons-logging" name="commons-logging" rev="1.2" conf="default" />
		<dependency org="org.slf4j" name="slf4j-api" rev="1.6.4" conf="default" />
		<dependency org="org.slf4j" name="slf4j-log4j12" rev="1.6.4" conf="default" />
		<dependency org="commons-lang" name="commons-lang" rev="2.6" conf="default" />
		<dependency org="com.cloudhopper.proxool" name="proxool" rev="0.9.1" conf="default" />
		<dependency org="mysql" name="mysql-connector-java" rev="5.1.34" conf="default" />
	</dependencies>
	
</ivy-module>


build.xml文件代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:ivy="antlib:org.apache.ivy.ant" basedir="." name="TestJavaCl" default="genericjar">

	<property name="publish.version" value="0.1" />   
	<property name="ivy.report.todir" value="build" />

    <target name="resolve">
        <ivy:configure file="ivysettings.xml" />
        <ivy:resolve file="ivy.xml" conf="default" />   
		<ivy:retrieve pattern="lib/[conf]/[artifact]-[revision].[ext]" />
		<ivy:report />
    </target>

	<path id="lib-classpath">
		<fileset dir="${basedir}/lib/default">
			<include name="**/*.jar"/>   
            <exclude name="**/*.bak"/>
		</fileset>
	</path>

	<target name="clean">
		<delete dir="${basedir}/bin" />
		<delete dir="${basedir}/dist" />
	</target>

	<target name="init" description="设置初始化打war包需要的路径变量">    
        <property name="name" value="TestJavaCl"/>    
        <property name="src" value="${basedir}/src"/>    
    	<property name="resources" value="${basedir}/resources"/>    
        <property name="packages" value="com.*"/>    
                
        <property name="build.class" value="${basedir}/bin"/>   
        <property name="build.javadocs" value="${basedir}/dist/doc"/>    
        <property name="jar.dest" value="${basedir}/dist/jar"/>
		<property name="mainclass" value="com.shihuan.test.TestUrlClassLoader"/>

		<property name="build.encoding" value="utf-8"/>
		<property name="year" value="2014"/>
		<property name="version" value="1.0.0"/>
          
        <echo message="----------- ${name} ${version} [${year}] ------------" />  
        <echo message="----------- ${src} ------------" />  
        <echo message="----------- ${resources} ------------" />  
        <echo message="----------- ${build.class} ------------" />
        <echo message="----------- ${build.javadocs} ------------" />  
        <echo message="----------- ${jar.dest} ------------" />  
        
    	<patternset id="ignored.files">
    		<exclude name="**/.svn/**" />
    	</patternset>
          
        <filter token="year" value="${year}" />  
        <filter token="version" value="${version}" />  
        <filter token="date" value="${TODAY}" />  
        <filter token="log" value="true" />  
        <filter token="verbose" value="true" />  
    </target>    
        
    <target name="prepare" depends="clean,init" description="创建打包需要的路径,拷贝源文件到打包路径下">
		<tstamp/>
        <mkdir dir="${build.class}"/>    
        <mkdir dir="${jar.dest}"/>
    </target>

	<target name="compile" depends="prepare,resolve">
        <javac encoding="${build.encoding}" srcdir="${src}" destdir="${build.class}" includeantruntime="false" source="1.6" debug="yes" verbose="yes" failonerror="true" optimize="false">
			<compilerarg line="-encoding UTF-8"/>   
			<classpath refid="lib-classpath" />
        </javac>    
        <copy todir="${build.class}">    
            <fileset dir="${resources}">    
                <include name="**/*.properties"/>
            	<include name="**/*.xml"/>
                <exclude name="**/*.bak"/>  
            </fileset>    
        </copy>
    </target>

	<target name="antjar" depends="compile">
        <!--Create a property containing all .jar files,  
        prefix lib/, and seperated with a space-->  
        <pathconvert property="mf.classpath" pathsep=" ">  
            <mapper>  
                <chainedmapper>  
                    <!-- jar包文件只留文件名,去掉目录信息 -->  
                    <flattenmapper/>  
                    <!-- add lib/ prefix -->  
                    <globmapper from="*" to="lib/default/*"/>  
                </chainedmapper>  
            </mapper> 
			<path refid="lib-classpath"/>
        </pathconvert>
		<jar destfile="${jar.dest}/myapp-${DSTAMP}.jar" basedir="${build.class}">  
            <manifest>  
                <attribute name="Main-class" value="${mainclass}"/>  
                <attribute name="Class-Path" value="${mf.classpath}"/>  
            </manifest>  
        </jar>
	</target>

	<target name="genericjar" depends="antjar"></target>

</project>

猜你喜欢

转载自shihuan830619.iteye.com/blog/2162206
Ivy