Java Tutorial/Ant/File Directory

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

Accepts a selection if it is acceptable to both of two FilenameFilters

import java.io.File;
import java.io.FilenameFilter;
/**
 * Accepts a selection if it is acceptable to both of two {@link FilenameFilter}s.
 * This takes two {@link FilenameFilter}s as input.
 *
 * <p>Eg., to print all files beginning with <code>A</code> and ending with <code>.java</code>:</p>
 *
 * <pre>
 * File dir = new File(".");
 * String[] files = dir.list( new AndFileFilter(
 *         new PrefixFileFilter("A"),
 *         new ExtensionFileFilter(".java")
 *         )
 *     );
 * for ( int i=0; i&lt;files.length; i++ )
 * {
 *     System.out.println(files[i]);
 * }
 * </pre>
 *
 * @author 
 * @version $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
 * @since 4.0
 */
public class AndFileFilter
    implements FilenameFilter
{
    private final FilenameFilter m_filter1;
    private final FilenameFilter m_filter2;
    public AndFileFilter( final FilenameFilter filter1, final FilenameFilter filter2 )
    {
        m_filter1 = filter1;
        m_filter2 = filter2;
    }
    public boolean accept( final File file, final String name )
    {
        return m_filter1.accept( file, name ) && m_filter2.accept( file, name );
    }
}





Ant task copy file

<project name="foo" default="deploy" basedir=".">
  
  <target name="init">
    <tstamp/>
    <property name="src" value="src" />
    <property name="build" value="build" />
    <property name="classes" value="classes" />
    <property name="deploy" value="deploy" />
    <property name="config" value="config" />
    <property name="runDir" value="." />
    <property name="local" value="local" />
    <property name="remote" value="remote" />
    <property name="lib" value="lib" />
  </target>
  
  <target name="clean" depends="init">
    <deltree dir="${classes}" />
    <deltree dir="${remote}" />
    <deltree dir="${deploy}" />
    <deltree dir="${lib}" />
  </target>
  
  <target name="prepare" depends="clean">
    <mkdir dir="${classes}" />
    <mkdir dir="${deploy}" />
    <mkdir dir="${lib}" />
  </target>    
  
  <target name="compile" depends="prepare">
    <javac srcdir="${src}" destdir="${classes}" />
    <copyfile src="${lib}/app.jar" dest="${deploy}/app.jar" />
    <copyfile src="${config}/remote.properties" dest="${runDir}\remote.properties" />
    <jar jarfile="${lib}/app.jar" basedir="${classes}" />
  </target>
 
  <target name="prepareDeploy" depends="compile">  
     <copyfile src="${lib}/app.jar" dest="${deploy}/app.jar" />
     <copyfile src="${build}/remotebuild.xml" dest="${deploy}/build.xml" />
     <mkdir dir="${remote}" />   
  </target>
  
  <target name="deploy" depends="prepareDeploy">
     <ant antfile="${build}/deploy.xml" dir="." />
  </target>
 
</project>





Ant task delete tree

<project name="foo" default="deploy" basedir=".">
  
  <target name="init">
    <tstamp/>
    <property name="src" value="src" />
    <property name="build" value="build" />
    <property name="classes" value="classes" />
    <property name="deploy" value="deploy" />
    <property name="config" value="config" />
    <property name="runDir" value="." />
    <property name="local" value="local" />
    <property name="remote" value="remote" />
    <property name="lib" value="lib" />
  </target>
  
  <target name="clean" depends="init">
    <deltree dir="${classes}" />
    <deltree dir="${remote}" />
    <deltree dir="${deploy}" />
    <deltree dir="${lib}" />
  </target>
  
  <target name="prepare" depends="clean">
    <mkdir dir="${classes}" />
    <mkdir dir="${deploy}" />
    <mkdir dir="${lib}" />
  </target>    
  
  <target name="compile" depends="prepare">
    <javac srcdir="${src}" destdir="${classes}" />
    <copyfile src="${lib}/app.jar" dest="${deploy}/app.jar" />
    <copyfile src="${config}/remote.properties" dest="${runDir}\remote.properties" />
    <jar jarfile="${lib}/app.jar" basedir="${classes}" />
  </target>
 
  <target name="prepareDeploy" depends="compile">  
     <copyfile src="${lib}/app.jar" dest="${deploy}/app.jar" />
     <copyfile src="${build}/remotebuild.xml" dest="${deploy}/build.xml" />
     <mkdir dir="${remote}" />   
  </target>
  
  <target name="deploy" depends="prepareDeploy">
     <ant antfile="${build}/deploy.xml" dir="." />
  </target>
 
</project>





Ant task make dir

<project name="foo" default="deploy" basedir=".">
  
  <target name="init">
    <tstamp/>
    <property name="src" value="src" />
    <property name="build" value="build" />
    <property name="classes" value="classes" />
    <property name="deploy" value="deploy" />
    <property name="config" value="config" />
    <property name="runDir" value="." />
    <property name="local" value="local" />
    <property name="remote" value="remote" />
    <property name="lib" value="lib" />
  </target>
  
  <target name="clean" depends="init">
    <deltree dir="${classes}" />
    <deltree dir="${remote}" />
    <deltree dir="${deploy}" />
    <deltree dir="${lib}" />
  </target>
  
  <target name="prepare" depends="clean">
    <mkdir dir="${classes}" />
    <mkdir dir="${deploy}" />
    <mkdir dir="${lib}" />
  </target>    
  
  <target name="compile" depends="prepare">
    <javac srcdir="${src}" destdir="${classes}" />
    <copyfile src="${lib}/app.jar" dest="${deploy}/app.jar" />
    <copyfile src="${config}/remote.properties" dest="${runDir}\remote.properties" />
    <jar jarfile="${lib}/app.jar" basedir="${classes}" />
  </target>
 
  <target name="prepareDeploy" depends="compile">  
     <copyfile src="${lib}/app.jar" dest="${deploy}/app.jar" />
     <copyfile src="${build}/remotebuild.xml" dest="${deploy}/build.xml" />
     <mkdir dir="${remote}" />   
  </target>
  
  <target name="deploy" depends="prepareDeploy">
     <ant antfile="${build}/deploy.xml" dir="." />
  </target>
 
</project>





Construct path in ant script

<?xml version="1.0"?>
<project name="Apache Ant Properties Project" 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>





Construct path under unix in ant script

<?xml version="1.0"?>
<project name="Apache Ant Properties Project" basedir=".">
  
  <target name="build.path.unix">
    <echo message="File: ${basedir}/build.xml"/>
    <echo message="Path: ${basedir}/build.xml;${basedir}/build.properties"/>
  </target> 

</project>





Converted path

<?xml version="1.0"?>
<project name="Apache Ant Properties Project" 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>





File set to include a file

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

  <fileset dir="." id="uptodate.id">
    <include name="src/jstl/One.java"/>
  </fileset>

</project>





Get base directory and file separator

<?xml version="1.0"?>
<project name="Apache Ant Properties Project" 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>