Java/Ant/Properties

Материал из Java эксперт
Перейти к: навигация, поиск

Ant buildin properties

   <source lang="java">

<?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>


 </source>
   
  
 
  



Custom properties

   <source lang="java">

<?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>


 </source>
   
  
 
  



Define and reference property

   <source lang="java">

<?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>

 </source>
   
  
 
  



Define custom property based on existing properties

   <source lang="java">

<?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>


 </source>
   
  
 
  



In init target set the properties

   <source lang="java">

<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>

 </source>
   
  
 
  



Load file through URL

   <source lang="java">

<?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>


 </source>
   
  
 
  



Redefine property in the children target

   <source lang="java">

/* 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


 </source>
   
  
 
  



Redefine property in the children target 2

   <source lang="java">

/* 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


 </source>
   
  
 
  



Reference ant.project.name

   <source lang="java">

<?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>

 </source>
   
  
 
  



Referrence property: basedir

   <source lang="java">

<?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>


 </source>
   
  
 
  



Separate two values

   <source lang="java">

<?xml version="1.0"?> <project name="Apache Ant Properties Project" default="properties.custom" basedir=".">

 <target name="properties.custom">
   
   <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"/>
   
   <echo message="Converted string: ${build.path.property}"/>
   
   <echo message="Path: ${build.path}"/>
 </target>

</project>


 </source>
   
  
 
  



Value in the properties file overwrite the value in the build.xml

   <source lang="java">

<?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


 </source>