Java/Ant/Properties
Содержание
- 1 Ant buildin properties
- 2 Custom properties
- 3 Define and reference property
- 4 Define custom property based on existing properties
- 5 In init target set the properties
- 6 Load file through URL
- 7 Redefine property in the children target
- 8 Redefine property in the children target 2
- 9 Reference ant.project.name
- 10 Referrence property: basedir
- 11 Separate two values
- 12 Value in the properties file overwrite the value in the build.xml
Ant buildin properties
<?xml version="1.0"?>
<project name="Apache Ant Properties Project" default="properties.built-in" basedir=".">
<target name="properties.built-in">
<echo message="The base directory: ${basedir}"/>
<echo message="This file: ${ant.file}"/>
<echo message="Ant version: ${ant.version}"/>
<echo message="Project name: ${ant.project.name}"/>
<echo message="Java version: ${ant.java.version}"/>
</target>
</project>
Custom properties
<?xml version="1.0"?>
<project name="Apache Ant Properties Project" default="properties.custom" basedir=".">
<target name="properties.custom">
<property name="build.no" value="1.1"/>
<echo message="Build no. = ${build.no}"/>
</target>
</project>
Define and reference property
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="build" name="Agnotion">
<property name="JUNIT_HOME" value="eclipse/plugins/org.junit_3.8.1"/>
<path id="project.classpath">
<pathelement location="build/java"/>
<pathelement location="build/test"/>
<pathelement location="${JUNIT_HOME}/junit.jar"/>
</path>
<target name="init">
<mkdir dir="build/test"/>
<mkdir dir="build/java"/>
<mkdir dir="dist"/>
</target>
<target name="clean">
<delete dir="build/test"/>
<delete dir="build/java"/>
<delete dir="dist"/>
</target>
<target depends="init" name="build">
<javac destdir="build/java">
<src path="src/java"/>
<classpath refid="project.classpath"/>
</javac>
<javac destdir="build/test">
<src path="src/test"/>
<classpath refid="project.classpath"/>
</javac>
</target>
<target name="dist" depends="build">
<jar destfile="dist/${ant.project.name}.jar"
basedir="build/java" />
</target>
</project>
Define custom property based on existing properties
<?xml version="1.0"?>
<project name="Apache Ant Properties Project" default="properties.custom" basedir=".">
<target name="properties.custom">
<property name="fs" value="${file.separator}"/>
<property name="ps" value="${path.separator}"/>
<echo message="File: ${basedir}${fs}build.xml"/>
<echo message="Path: ${basedir}${fs}build.xml${ps}${basedir}${fs}build.properties"/>
</target>
</project>
In init target set the properties
<project name="YourName" default="all">
<target name="all" depends="init,clean,compile,createJars,copyBuild" >
</target>
<target name="init" description="Project">
<property environment="env" />
<property name="j2sdkApi" value="${env.JAVA_HOME}/jre/lib/rt.jar" />
<property name="src" value="./src" />
<property name="build" value= "./build" />
</target>
<target name="clean" description="build" depends="init">
<delete dir="${build}" />
<mkdir dir="${build}" />
</target>
<target name="compile" description="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" >
<classpath path="${j2sdkApi}" />
<include name="**/application/**"/>
<include name="**/types/**"/>
</javac>
</target>
<target name="copyBuild" description="desccription here">
<copy file="${src}/LOGOUNB_CAPA.JPG" todir="${build}/" />
<copydir src="${src}/config" dest="${build}/config" />
</target>
<target name="createJars" description="jars" depends="compile">
<mkdir dir="${build}/xml/jar"/>
<jar jarfile="${build}/br/xml/jar/xpfg.jar"
basedir="${build}/unb/cic/xml" />
<manifest file="manifest.mf">
<attribute name="Main-Class" value="${build}/br/unb/cic/xml/XMLMain" />
</manifest>
</target>
</project>
Load file through URL
<?xml version="1.0"?>
<project name="Apache Ant Properties Project" default="properties.url" basedir=".">
<target name="properties.url">
<property url="http://localhost:8080/antBook/properties/build.properties"/>
<path id="build.classpath.id">
<pathelement path="${build.classpath}"/>
</path>
<property name="build.classpath.property" refid="build.classpath.id"/>
<echo message="Server URL: ${server.url}"/>
<echo message="Build classpath: ${build.classpath}"/>
<echo message="Build classpath converted: ${build.classpath.property}"/>
</target>
</project>
Redefine property in the children target
/*
Buildfile: build.xml
print-global:
[echo] In print-global
[echo] The value of property.example is: Local File
print-target:
[echo] In print-target
[echo] The value of property.example is: Local File
BUILD SUCCESSFUL
Total time: 0 seconds
C:\Java_Dev\WEB\dev\java\api\Proant\ch03>
*/
<?xml version="1.0"?>
<project name="Apache Ant Properties Project" default="print-target" basedir=".">
<property file="build.properties"/>
<property file="build.properties.default"/>
<property name="property.example" value="Global"/>
<target name="print-global">
<echo message="In print-global"/>
<echo message="The value of property.example is: ${property.example}"/>
</target>
<target name="print-target" depends="print-global">
<property name="property.example" value="Target"/>
<echo message="In print-target"/>
<echo message="The value of property.example is: ${property.example}"/>
</target>
</project>
--------------------------------------------------------------------------------------
File: build.properties
property.example=Local File
property.file.example=build.properties
--------------------------------------------------------------------------------------
File: build.properties.default
property.example=Default File
property.file.example=build.properties.default
Redefine property in the children target 2
/*
Buildfile: build.xml
print-global:
[echo] In print-global
[echo] The value of property.example is: Local File
print-target:
[echo] In print-target
[echo] The value of property.example is: Local File
print-file:
[echo] In print-file
[echo]
[echo] The value of property.file.example is: build.properties
[echo]
BUILD SUCCESSFUL
Total time: 0 seconds
*/
<?xml version="1.0"?>
<project name="Apache Ant Properties Project" default="print-file" basedir=".">
<property file="build.properties"/>
<property file="build.properties.default"/>
<property name="property.example" value="Global"/>
<target name="print-global">
<echo message="In print-global"/>
<echo message="The value of property.example is: ${property.example}"/>
</target>
<target name="print-target" depends="print-global">
<property name="property.example" value="Target"/>
<echo message="In print-target"/>
<echo message="The value of property.example is: ${property.example}"/>
</target>
<target name="print-file" depends="print-target">
<property name="property.file.example" value="build.xml"/>
<echo message="In print-file"/>
<echo>
The value of property.file.example is: ${property.file.example}
</echo>
</target>
</project>
--------------------------------------------------------------------------------------
File: build.properties
property.example=Local File
property.file.example=build.properties
--------------------------------------------------------------------------------------
File: build.properties.default
property.example=Default File
property.file.example=build.properties.default
Reference ant.project.name
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="build" name="Agnotion">
<property name="JUNIT_HOME" value="eclipse/plugins/org.junit_3.8.1"/>
<path id="project.classpath">
<pathelement location="build/java"/>
<pathelement location="build/test"/>
<pathelement location="${JUNIT_HOME}/junit.jar"/>
</path>
<target name="init">
<mkdir dir="build/test"/>
<mkdir dir="build/java"/>
<mkdir dir="dist"/>
</target>
<target name="clean">
<delete dir="build/test"/>
<delete dir="build/java"/>
<delete dir="dist"/>
</target>
<target depends="init" name="build">
<javac destdir="build/java">
<src path="src/java"/>
<classpath refid="project.classpath"/>
</javac>
<javac destdir="build/test">
<src path="src/test"/>
<classpath refid="project.classpath"/>
</javac>
</target>
<target name="dist" depends="build">
<jar destfile="dist/${ant.project.name}.jar"
basedir="build/java" />
</target>
</project>
Referrence property: basedir
<?xml version="1.0"?>
<project name="Apache Ant Properties Project" default="build.path" basedir=".">
<target name="build.path">
<echo message="File: ${basedir}${file.separator}build.xml"/>
<echo message="Path: ${basedir}${file.separator}build.xml${path.separator}${basedir}${file.separator}build.properties"/>
</target>
</project>
Separate two values
<?xml version="1.0"?>
<project name="Apache Ant Properties Project" default="properties.custom" basedir=".">
<target name="properties.custom">
<!-- Windows users should leave this line uncommented -->
<property name="build.path"
value="${basedir}/build.xml:${basedir}/build.properties"/>
<!-- Unix users should remove the above line
and uncomment the below line -->
<!--
<property name="build.path"
value="${basedir}\build.xml;${basedir}\build.properties"/>
-->
<path id="build.path.id">
<pathelement path="${build.path}"/>
</path>
<property name="build.path.property" refid="build.path.id"/>
<!-- The converted string that Ant uses as a path -->
<echo message="Converted string: ${build.path.property}"/>
<!-- The unconverted string, which Ant treats as a string -->
<echo message="Path: ${build.path}"/>
</target>
</project>
Value in the properties file overwrite the value in the build.xml
<?xml version="1.0"?>
<project name="Apache Ant Properties Project" default="print-global" basedir=".">
<property file="build.properties"/>
<property file="build.properties.default"/>
<property name="property.example" value="Global"/>
<target name="print-global">
<echo message="In print-global"/>
<echo message="The value of property.example is: ${property.example}"/>
</target>
</project>
--------------------------------------------------------------------------------------
File: build.properties
property.example=Local File
property.file.example=build.properties
--------------------------------------------------------------------------------------
File: build.properties.default
property.example=Default File
property.file.example=build.properties.default