Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | Email

第 2 章 Build Tools

目录

2.1. Apache Ant
2.1.1. Project
2.1.1.1. ant
2.1.1.2. environment
2.1.2. path
2.1.3. copy
2.1.4. javac
2.1.5. condition
2.1.6. exec
2.1.6.1. sshexec
2.1.7. if
2.1.8. macrodef
2.1.8.1. Git
2.1.8.2. Rsync
2.1.8.3. SSH
2.1.9. Javascript
2.1.10. FAQ
2.1.10.1. warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
2.2. Apache Ivy
2.2.1. Ivy Install
2.2.1.1. source code
2.2.1.2. apt-get
2.2.2. Test example
2.3. maven
2.3.1. 安装
2.3.1.1. Ubuntu
2.3.1.2. 源码安装
2.3.2. Maven 仓库
2.3.3. properties
2.3.4. dependencies
2.3.5. Maven 命令
2.3.5.1. clean
2.3.5.2. compile
2.3.5.3. package
2.3.5.4. install
2.3.6. copy-dependencies
2.4. Gradle
2.4.1. gradle 命令
2.4.1.1. tasks 列出任务
2.4.2. build.gradle
2.4.2.1. repositories
2.4.2.2. dependencies
2.4.2.3. jar
2.4.3. gradle.properties
2.4.3.1. 列出 properties
2.4.3.2. 自定义 gradle.properties
2.4.3.3.
2.4.3.4. System.properties
2.4.4. Example

2.1. Apache Ant

http://ant.apache.org/

cd /usr/local/src
wget http://mirror.bjtu.edu.cn/apache//ant/binaries/apache-ant-1.8.1-bin.tar.gz
tar zxvf apache-ant-1.8.1-bin.tar.gz
mv apache-ant-1.8.1 /usr/local/
cd ..
ln -s apache-ant-1.8.1 apache-ant
	
ANT_HOME=/usr/local/apache-ant
export CLASSPATH=$CLASSPATH:$ANT_HOME/lib
	

2.1.1. Project

		
<description>project Name</description>
		
		
		
<property name="src" value="src"/>
<property name="dest" value="classes"/>
<property name="hello" value="hello.jar"/>		
		
		

2.1.1.1. ant

Project name

			
${ant.project.name}
			
			

2.1.1.2. environment

			
<property environment="env"/>
<echo message="JAVA_HOME is set to = ${env.JAVA_HOME}" />			
			
			

2.1.2. path

定义

		
	<path id="classpath">  
        <fileset dir="${env.JAVA_HOME}/lib">  
            <include name="*.jar" />  
        </fileset>  
        <fileset dir="${env.CATALINA_HOME}/lib">  
            <include name="*.jar" />  
        </fileset>  
        <fileset dir="WebRoot/WEB-INF/lib" includes="*.jar"/>
    </path>    
		
		

引用

		
	<javac srcdir="${src.dir}" destdir="${classes.dir}" source="1.5" target="1.5">  
		<classpath refid="classpath" />  
	</javac>		
		
		

		
    <classpath>
		<path refid="classpath"/>
    </classpath>		
		
		

2.1.3. copy

复制目录

		
	<copy todir="${basedir}/WebContent"> 
		<fileset dir="${basedir}/WebRoot" includes="**/*"/>
	</copy>	    
		
		

复制指定扩展名文件

		
	<copy todir="${dest}">  
		<fileset dir="${src}">  
			<include name="**/*.xml" />  
			<include name="**/*.properties" />  
		</fileset>
	</copy> 	
		
		

2.1.4. javac

		
	<path id="classpath">
		<fileset dir="${env.JAVA_HOME}/lib">
			<include name="*.jar" />
		</fileset>
		<fileset dir="${env.CATALINA_HOME}/lib">
			<include name="*.jar" />
		</fileset>
		<fileset dir="${project.dir}/WebRoot/WEB-INF/lib" includes="*.jar" />
	</path>

	<javac srcdir="${project.src}" destdir="${project.build}/WEB-INF/classes" debug="true" listfiles="true">
			<classpath refid="classpath" />
			<include name="**/*.java"/>
			<exclude name="**/*.xml"/>
			<exclude name="**/*.properties"/>
	</javac>
		
		

listfiles 显示编译文件列表

debug 显示调试信息,编译错误信息

2.1.5. condition

		
<?xml version="1.0"?>
<project name="test" default="doFoo" basedir=".">
  <property name="directory" value="/www/directory"/>

  <target name="doFoo" depends="dir.check" if="dir.exists">
    <echo>${directory} exists</echo>
  </target>

  <target name="doBar" depends="dir.check" unless="dir.exists">
    <echo>${directory} missing"</echo>
  </target>

  <target name="dir.check">
    <condition property="dir.exists">
      <available file="${directory}" type="dir"/>
    </condition>
  </target>
</project>		
		
		

2.1.6. exec

		
<project name="{{ name }}" default="help" basedir=".">
  
  <property name="username" value="{{ username }}"/>
  <property name="host" value="{{ host }}"/>
  <property name="dir" value="/srv/{{ path }}/"/>

  <tstamp>
    <format property="TODAY_UK" pattern="yyyyMMddhhmmss" locale="en,UK"/>
  </tstamp>

  <target name="help" description="show available commands" >
    <exec executable="ant" dir="." failonerror="true">
      <arg value="-p"/>
    </exec>
  </target>
  
  <target name="deploy-to" description="show where we are deploying to" >
    <echo>${username}@${host}:${dir}</echo>
  </target>

  <target name="deploy" description="deploy usng rsync" >
    <exec executable="rsync" dir="." failonerror="true">
      <arg value="-r"/>
      <arg value="."/>
      <arg value="${username}@${host}:${dir}"/>
      <arg value="--exclude-from=rsync.excludes"/>
      <arg value="-v"/>
    </exec>
  </target>

  <target name="deploy-test" description="test deploy usng rsync with the dry run flag set" >
    <exec executable="rsync" dir="." failonerror="true">
      <arg value="-r"/>
      <arg value="."/>
      <arg value="${username}@${host}:${dir}"/>
      <arg value="--exclude-from=rsync.excludes"/>
      <arg value="--dry-run"/>
      <arg value="-v"/>
    </exec>
  </target>

  <target name="backup" description="backup site" >
    <exec executable="scp" dir="." failonerror="true">
      <arg value="-r"/>
      <arg value="${username}@${host}:${dir}"/>
      <arg value="backups/${TODAY_UK}"/>
    </exec>
  </target>

</project>		
		
		

2.1.6.1. sshexec

			
<sshexec host="${remove}" keyfile="~/.ssh/id_rsa" command="/srv/apache-tomcat/bin/catalina.sh stop -force" />			
			
			

2.1.7. if

		
		
<if>
  <available file="my_directory" type="dir" />
  <then>
    <echo message="Directory exists" />
  </then>
  <else>
    <echo message="Directory does not exist" />
  </else>
</if>		
		
		
		

2.1.8. macrodef

2.1.8.1. Git

			
<macrodef name = "git">
    <attribute name = "command" />
    <attribute name = "dir" default = "" />
    <element name = "args" optional = "true" />
    <sequential>
        <echo message = "git @{command}" />
        <exec executable = "git" dir = "@{dir}">
            <arg value = "@{command}" />
            <args/>
        </exec>
    </sequential>
</macrodef>
<macrodef name = "git-clone-pull">
    <attribute name = "repository" />
    <attribute name = "dest" />
    <sequential>
        <git command = "clone">
            <args>
                <arg value = "@{repository}" />
                <arg value = "@{dest}" />
            </args>
        </git>
        <git command = "pull" dir = "@{dest}" />
    </sequential>
</macrodef>
			
			
			
<git command = "clone">
    <args>
        <arg value = "git://github.com/280north/ojunit.git" />
        <arg value = "ojunit" />
    </args>
</git>

<git command = "pull" dir = "repository_path" />		

<git-clone-pull repository="git://github.com/280north/ojunit.git" dest="ojunit" />	
			
			

2.1.8.2. Rsync

			
			
	<macrodef name="rsync">
		<attribute name="option" default="auzv" />
		<attribute name="src" default="" />
		<attribute name="dest" default="" />
		<element name="args" optional="true" />
		<sequential>
			<exec executable="rsync">
				<arg value="@{option}" />
				<arg value="@{src}" />
				<arg value="@{dest}" />
				<args />
			</exec>
		</sequential>
	</macrodef>			
			
			
			
			
	<target name="deploy" depends="compile">
		<rsync option="-auzv" src="${project.dest}" dest="${remote}:${destination}">
			<args>
				<arg value="-P" />
			</args>
		</rsync>
	</target>			
			
			

2.1.8.3. SSH

			
	<macrodef name="ssh">
		<attribute name="host" />
		<attribute name="command" />
		<attribute name="keyfile" default="~/.ssh/id_rsa" />
		<element name="args" optional="true" />
		<sequential>
			<exec executable="ssh">
				<arg value="@{host}" />
				<!-- arg value="-i @{keyfile}" / -->
				<args />
				<arg value="@{command}" />
			</exec>
		</sequential>
	</macrodef>
			
			
			
	<target name="stop" depends="">
		<!-- ssh host="${remote}" command="/srv/apache-tomcat/bin/catalina.sh stop -force" keyfile="~/.ssh/id_rsa" / -->
		<ssh host="${remote}" command="/srv/apache-tomcat/bin/shutdown.sh" />
	</target>
	<target name="start" depends="">
		<ssh host="${remote}" command="/srv/apache-tomcat/bin/startup.sh" keyfile="~/.ssh/id_rsa" />
	</target>		
			
			

2.1.9. Javascript

		

$ cat build.xml 
<project name="Attachments" default="print">
    <property name="numAttachments" value="20" />
    <target name="generate">
        <script language="javascript"><![CDATA[
            var list = '1';
            var limit = project.getProperty( "numAttachments" );
            for (var i=2;i<limit;i++)
            { 
                list = list + ',' + i;
            }
            project.setNewProperty("list", list);            
		print(list);
        ]] >
        </script>    
    </target>
</project>

		
		
		
$ ant generate
Buildfile: /www/testing/build.xml

generate:
   [script] 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19

BUILD SUCCESSFUL
Total time: 0 seconds
		
		

2.1.10. FAQ

2.1.10.1. warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

includeantruntime="false"

			
<target name="compile" depends="init">
   <javac includeantruntime="false" srcdir="${src}" destdir="${dest}"/>
</target>
			
			

or

			
<property name="build.sysclasspath" value="last"/>
			
			
comments powered by Disqus