Java/Ant/Shell Command

Материал из Java эксперт
Версия от 06:37, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Apply executable

 
//COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 
<project name="ajax4jsf" default="distribute">
  <property name="commonLibs.dir" value="${basedir}/commonLibs"></property>
  
  <property name="distrib.dir" value="${basedir}/distribution"/>
  <property name="work.dir" value="${basedir}/build"/>
  
  <property name="src.root.dir" value="${basedir}/src"></property>
  
  <property name="test.src.dir" value="${src.root.dir}/testCase"></property>
  <property name="framework.src.dir" value="${src.root.dir}/framework"></property>
  <property name="framework.java.src.dir" value="${framework.src.dir}/java"></property>
  <property name="framework.js.src.dir" value="${framework.src.dir}/JavaScript"></property>
  <property name="framework.generate.src.dir" value="${framework.src.dir}/generate"></property>
  <property name="framework.build.dir" value="${work.dir}/framework"></property>
  <property name="cdk.src.dir" value="${src.root.dir}/cdk"></property>
  <property name="cdk.build.dir" value="${work.dir}/cdk"></property>
    <property name="test.src.dir" value="${src.root.dir}/test"></property>
    <property name="test.build.dir" value="${work.dir}/test"></property>
  <property name="doc.root.dir" value="${distrib.dir}/docs"></property>
  <property name="tlddoc.dir" value="${doc.root.dir}/tldDoc"></property>
  <property name="javadoc.dir" value="${doc.root.dir}/javaAPI"></property>
  <property name="tlddoc.jar" value="${commonLibs.dir}/tlddoc.jar"></property>
  
  <property name="framework-distribution-name" value="ajax4jsf"></property>
  <property name="cdk-distribution-name" value="ajax4jsf-cdk" />
    <property name="test-distribution-name" value="ajax4jsf-test" />
  
  <property name="generated-source.dir" value="${work.dir}/gen-src"></property>
  
  <fileset id="commonLibs" dir="${commonLibs.dir}">
    <include name="*.jar"/>
  </fileset>
  
  <path id="class-path">
    <fileset refid="commonLibs"></fileset>
  </path>
  <target name="prepare">
    <mkdir dir="${distrib.dir}"/>
    <mkdir dir="${work.dir}"/>
  </target>
  
  <target name="generate-framework" depends="compile-framework,compile-cdk">
    <ant antfile="${framework.generate.src.dir}/build.xml" inheritall="true" dir="${basedir}" target="xml-merge">
    </ant>
  </target>

  <target name="prepare-framework">
    <mkdir dir="${framework.build.dir}" />
  </target>
  <target name="prepare-cdk">
    <mkdir dir="${cdk.build.dir}" />
  </target>
    <target name="prepare-test">
        <mkdir dir="${test.build.dir}" />
    </target>
  
  <target name="compile-framework" depends="prepare-framework">
    <javac 
      destdir="${framework.build.dir}"
      compiler="javac1.4" 
      source="1.4" 
      target="1.4" 
      debug="true" 
            failonerror="false"         
      debuglevel="lines,vars,source">
      <classpath refid="class-path"></classpath>
      <src path="${framework.src.dir}"></src>
    </javac>
    <copy todir="${framework.build.dir}">
      <fileset dir="${framework.java.src.dir}">
        <include name="**/*.*"/>
        <exclude name="**/*.java"/>
        <exclude name="META-INF/*faces-config.xml"/>
      </fileset>
    </copy>
  </target>
  <target name="compile-cdk" depends="prepare-cdk">
    <javac 
      destdir="${cdk.build.dir}"
      compiler="javac1.5" 
      source="1.5" 
      target="1.5" 
      debug="true" 
      debuglevel="lines,vars,source">
        <classpath refid="class-path"></classpath>
      <src path="${cdk.src.dir}"></src>
    </javac>
    <copy todir="${cdk.build.dir}/META-INF">
      <fileset dir="${cdk.src.dir}/META-INF">
        <include name="**/*.*"/>
      </fileset>        
    </copy>    
  </target>
  
    <target name="compile-test" depends="prepare-test">
        <javac 
            destdir="${test.build.dir}"
            compiler="javac1.4" 
            source="1.4" 
            target="1.4" 
            debug="true" 
            debuglevel="lines,vars,source"
            includes="org/ajax4jsf/tests/*.*" >
                <classpath refid="class-path"></classpath>
            <classpath path="${framework.build.dir}"></classpath>   
            <src path="${test.src.dir}">
            </src>
        </javac>
        <copy todir="${test.build.dir}/META-INF">
            <fileset dir="${test.src.dir}/META-INF">
                <include name="**/*.*"/>
            </fileset>              
        </copy>     
        <copy todir="${test.build.dir}/WEB-INF">
            <fileset dir="${test.src.dir}/WEB-INF">
                <include name="**/*.*"/>
            </fileset>              
        </copy>     
    </target>
    <target name="compile-framework-phase2" depends="generate-framework">
    <javac 
      destdir="${framework.build.dir}"
      compiler="javac1.4" 
      source="1.4" 
      target="1.4" 
      debug="true" 
      debuglevel="lines,vars,source">
      <classpath>
        <path>
        <path refid="class-path"></path>
        <pathelement path="${framework.build.dir}"/>
        </path>
      </classpath>
      <src path="${generated-source.dir}"></src>
    </javac>
    <copy todir="${framework.build.dir}">
      <fileset dir="${generated-source.dir}">
        <include name="**/*.*"/>
        <exclude name="**/*.java"/>
        <exclude name="META-INF/*-faces-config.xml"/>
      </fileset>
    </copy>
  </target>
  
  <target name="assemble-javascripts" depends="prepare-framework">
    <ant antfile="${framework.js.src.dir}/build.xml" dir="${framework.js.src.dir}">
      <property name="target-dir" value="${framework.build.dir}"></property>
    </ant>
  </target>
  
  <target name="build-framework" depends="compile-framework,assemble-javascripts,compile-framework-phase2">
  </target>
  <target name="build-cdk" depends="compile-cdk">
  </target>
  
    <target name="build-test" depends="compile-test">
    </target>
        <target name="prepare-distribution-dir">
    <mkdir dir="${distrib.dir}/lib"/>
  </target>
  
  <target name="package-framework" depends="build-framework, prepare-distribution-dir">
    <jar destfile="${distrib.dir}/lib/${framework-distribution-name}.jar">
      <fileset dir="${framework.build.dir}">
        <include name="**/*.*"/>
      </fileset>
    </jar>
  </target>
  
  <target name="package-cdk" depends="prepare-distribution-dir,build-cdk">
    <jar destfile="${distrib.dir}/lib/${cdk-distribution-name}.jar">
      <fileset dir="${cdk.build.dir}">
        <include name="**/*.*"/>
      </fileset>
    </jar>
  </target>
    <target name="package-test" depends="prepare-distribution-dir,build-test">
        <jar destfile="${distrib.dir}/lib/${test-distribution-name}.jar">
            <fileset dir="${test.build.dir}">
                <include name="**/*.*"/>
            </fileset>
        </jar>
    </target>
  <target name="prepare-docs">
    <mkdir dir="${doc.root.dir}"/>
  </target>
  
  <target name="docs" depends="prepare-docs, javadoc, tlddoc, copy-doc">
    
  </target>
  
  <target name="copy-doc" depends="prepare-docs">
    <copy todir="${doc.root.dir}" failonerror="false">
      <fileset dir="${basedir}/doc">
        <include name="**/*"/>
      </fileset>
    </copy>
  </target>
  
  <target name="javadoc">
    <mkdir dir="${javadoc.dir}"/>
    <javadoc packagenames="org.*" destdir="${javadoc.dir}">
      <sourcepath>
        <pathelement path="${framework.java.src.dir}"/>
      </sourcepath>
      <classpath refid="class-path"></classpath>
    </javadoc>
  </target>
  
  <target name="tlddoc">
    <mkdir dir="${tlddoc.dir}"/>
    <echo message="${tlddoc.jar}"></echo>
    <apply executable="java" parallel="true" verbose="true">
      <!-- dir="${tlddoc.dir}">-->
      <arg line="-jar ${tlddoc.jar}"/>
      <arg line="-doctitle "AJAX4JSF""/>
      <!--arg line="-xslt ${tlddocxsl.dir}"/-->
      <arg line="-d ${tlddoc.dir}"/>
      <fileset dir="${framework.build.dir}/META-INF" includes="**.tld"/>
    </apply>
  </target>
  
  <target name="copy-dependencies" depends="prepare-distribution-dir">
    <copy todir="${distrib.dir}/lib">
      <fileset dir="${commonLibs.dir}">
        <include name="oscache*.jar"/>
      </fileset>
    </copy>
  </target>
  
  <target name="copy-src">
    <mkdir dir="${distrib.dir}/src"/>
    <copy todir="${distrib.dir}/src">
      <fileset dir="${framework.src.dir}">
        <include name="**/*"/>
      </fileset>
    </copy>
  </target>
  
  <target name="copy-demo">
    <mkdir dir="${distrib.dir}/demo"/>
    <copy todir="${distrib.dir}/demo" failonerror="false">
      <fileset dir="${basedir}/demo">
        <include name="**/*"/>
      </fileset>
    </copy>
  </target>
  
  <target name="copy-txt">
    <copy todir="${distrib.dir}">
      <fileset dir="${basedir}">
        <include name="*.txt"/>
        <include name="*.TXT"/>
      </fileset>
    </copy>
  </target>
  
  <target name="build-test-framework">
    <javac 
      destdir="${framework.build.dir}"
      compiler="javac1.4" 
      source="1.4" 
      target="1.4" 
      debug="true" 
      debuglevel="lines,vars,source">
      <classpath refid="class-path"></classpath>
      <src path="${test.src.dir}"></src>
    </javac>
    <copy todir="${framework.build.dir}">
      <fileset dir="${test.src.dir}">
        <include name="**/*.*"/>
        <exclude name="**/*.java"/>
        <exclude name="META-INF/*"/>
        <exclude name="WEB-INF/*"/>
      </fileset>
    </copy>
  </target>
  
  <target name="distribute" depends="clean,build-framework,package-framework, copy-dependencies, docs, copy-src, copy-txt, copy-demo, package-cdk, package-test">
    <!--delete dir="${work.dir}"></delete-->
    <delete file="${basedir}/velocity.log" failonerror="false" verbose="true"></delete>
  </target>
  
  <target name="clean">
    <delete dir="${work.dir}"></delete>
    <delete includeemptydirs="true" failonerror="false">
      <fileset dir="${distrib.dir}">
        <include name="**/*"/>
      </fileset>
    </delete>
  </target>
</project>





Execute shell command

 
<?xml version="1.0"?>
<project name="Demo ant junit" default="compile" basedir=".">
  <property name="dir.build" value="build"/>
  <property name="dir.src" value="src"/>
  <property environment="env"/>
  <path id="classpath.project">
    <pathelement path="${dir.build}"/>
  </path>
  <target name="install.junit">
    <fail unless="env.JUNIT_HOME">
      The JUNIT_HOME environment variable must be set.
    </fail>
    <available property="junit.already.installed"
               file="${ant.home}/lib/junit.jar"/>
    <copy file="${env.JUNIT_HOME}/junit.jar"
          todir="${ant.home}/lib"
          failonerror="true"/>
   
    <fail unless="junit.already.installed">
      junit.jar was not found in ANT_HOME/lib prior to this 
      build, so it was copied for you. Please try your build again.
    </fail>
  </target>
  <target name="prepare" depends="install.junit">
    <mkdir dir="${dir.build}"/>
  </target>
  <target name="clean"
          description="Remove all generated files.">
    <delete dir="${dir.build}"/>
  </target>
  <target name="compile" depends="prepare"
          description="Compile all source code.">
    <javac srcdir="${dir.src}" destdir="${dir.build}">
      <classpath refid="classpath.project"/>
    </javac>
  </target>
  <target name="junit1" depends="compile">
    <junit printsummary="on" 
           fork="false" 
           haltonfailure="false"
           failureproperty="tests.failed"
           showoutput="true">
      <classpath refid="classpath.project"/>
      <formatter type="brief" usefile="false"/>
      <batchtest>
        <fileset dir="${dir.src}">
          <include name="**/Test*.java"/>
          <exclude name="**/AllTests.java"/>
        </fileset>
      </batchtest>
    </junit>
    <fail if="tests.failed">
    *******************************************************
    *******************************************************
    One or more tests failed. Check the output...
    *******************************************************
    *******************************************************
    </fail>
  </target>
  <target name="junit2" depends="compile">
    <!-- you may override this on the command line:
         ant -Dtestcase=com/jexp/javaxp/junit/TestGame junit2 -->
    <property name="testcase" 
              value="com/jexp/javaxp/junit/TestPerson"/>
    <junit fork="false">
      <classpath refid="classpath.project"/>
      <formatter type="plain" usefile="false"/>
      <test name="${testcase}"/>
    </junit>
  </target>

  <target name="junit3" depends="compile">
    <junit printsummary="on" fork="false" haltonfailure="false">
      <classpath refid="classpath.project"/>
      <formatter type="xml"/>
      <batchtest todir="${dir.build}">
        <fileset dir="${dir.src}">
          <include name="**/Test*.java"/>
          <exclude name="**/AllTests.java"/>
        </fileset>
      </batchtest>
    </junit>
    <junitreport todir="${dir.build}">
      <fileset dir="${dir.build}">
        <include name="TEST-*.xml"/>
      </fileset>
      <report format="frames" todir="${dir.build}"/>
    </junitreport>
    <pathconvert dirsep="/" property="reportUrl">
      <path>
        <pathelement location="${dir.build}/index.html"/>
      </path>
    </pathconvert>
    <exec executable="cmd" os="Windows XP">
      <arg value="/C"/>
      <arg value="${reportUrl}"/>
    </exec>
  </target>
</project>





Use exec executable to call system shell command

 
//https://amateur.dev.java.net/
//GNU General Public License (GPL v. 2.0)
<?xml version="1.0" encoding="UTF-8"?>
<project name="Amateur" default="compile" basedir=".">
    
    <!-- ???? need a help target -->
    
    <property name="JUNIT_HOME" value="/Applications/eclipse/plugins/org.junit_3.8.1"/>
    <property name="QTJava" value="/System/Library/Java/Extensions/QTJava.zip"/>
    <taskdef name="jarbundler"
             classpath="lib/jarbundler-1.8.1.jar"
             classname="net.sourceforge.jarbundler.JarBundler" /> 
    
    <path id="project.classpath">
        <pathelement location="bin"/>
        <pathelement location="${QTJava}"/>
    </path>
    
    <path id="test.classpath">
        <path refid="project.classpath"/>
        <pathelement location="${JUNIT_HOME}/junit.jar"/>
    </path>
    
    <target name="init">
        <tstamp/>
        <property name="Name"    value="Amateur"/>
        <property name="name"    value="amateur"/>
        <property name="version" value="1.0d6"/>
        <property name="year"    value="2006"/>
        <mkdir dir="bin"/>
        <mkdir dir="dist"/>
    </target>
    
    <target name="clean">
        <delete dir="bin"/>
        <delete dir="dist"/>
    </target>
    
    <target name="compile" depends="init">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac destdir="bin">
            <src path="src"/>
            <classpath refid="project.classpath"/>
        </javac>
    </target>
    
    <target name="test">
        <junit fork="yes" printsummary="withOutAndErr">
            <formatter type="plain"/>
            <test name="com.elharo.quicktime.tests.AmateurTests"/>
            <classpath refid="test.classpath"/>
        </junit>
    </target>
    
    <target name="run">
        <java fork="yes" classname="com.elharo.quicktime.Main" failonerror="true">
            <classpath refid="project.classpath"/>
        </java>
    </target>
    
    <target name="jar" depends="compile">
       <jar jarfile="dist/${name}-${version}.jar"
             basedir="bin"
             index="no"
             compress="yes"
             includes="com/elharo/quicktime/*"
             excludes="com/elharo/quicktime/tests/* com/elharo/quicktime/pantry/*">
          <manifest>
            <attribute name="Built-By" value="${user.name}"/>
            <attribute name="Specification-Title"    value="Amateur"/>
            <attribute name="Specification-Version"  value="${version}"/>
            <attribute name="Specification-Vendor"   value="Elliotte Rusty Harold"/>
            <attribute name="Implementation-Title"   value="Amateur"/>
            <attribute name="Implementation-Version" value="${version}"/>
            <attribute name="Implementation-Vendor"  value="Elliotte Rusty Harold"/>          
            <attribute name="Main-Class" value="com.elharo.quicktime.Main"/>
            <section name="com/elharo/quicktime/">
              <attribute name="Sealed" value="true"/>
              <attribute name="Specification-Title"   value="Amateur core classes"/>
              <attribute name="Implementation-Title"  value="com.elharo.quicktime"/>
            <attribute name="Implementation-Version" value="${version}"/>
            <attribute name="Implementation-Vendor"  value="Elliotte Rusty Harold"/>          
           </section>
          </manifest>
        </jar>        
    </target>
    
    <target name="app" depends="jar">
      <delete dir="dist/Amateur.app"/>
        <jarbundler dir="dist" jars="dist/${name}-${version}.jar" 
                    name="Amateur" 
                    mainclass="com.elharo.quicktime.Main" 
                    infostring=" ${version}"
                    version=" ${version}"
                    screenmenu="true"
                    signature="QTAM"
                    bundleid="com.elharo.quicktime"
                    icon="Amateur.icns"
        >
          <documenttype name="Amateur Media"
                    mimetypes="image/jpeg image/png image/gif video/mpeg video/quicktime audio/mpeg" 
                    role="Viewer"/>
          <documenttype name="Amateur Media"
                    extensions="jpg mov mpg mp3 gif png pdf" 
                    role="Viewer"/>
        </jarbundler>
    </target>
    
    <target name="image" depends="app">
      <exec executable="hdiutil" os="Mac OS X">
          <arg value="create"/>
          <arg value="-srcfolder"/>
          <arg value="dist/${Name}.app"/>
          <arg value="-ov"/>
          <arg value="dist/${Name}-${version}.dmg"/>
      </exec>
    </target>

    <taskdef classpath="lib/cobertura.jar" resource="tasks.properties" />
    
    <target name="instrument">
      <cobertura-instrument todir="bin/instrumented-classes">
        <fileset dir="bin/classes">
          <include name="**/*.class"/>
        </fileset>
      </cobertura-instrument>
    </target>
    
    <target name="cover-test" depends="instrument">
      <mkdir dir="${testreportdir}" />
      <junit dir="./" failureproperty="test.failure" printSummary="yes" 
             fork="true" haltonerror="true">
        <!-- Normally you can create this task by copying your existing JUnit
             target, changing its name, and adding these next two lines.
             You may need to change the locations to point to wherever 
             you"ve put the cobertura.jar file and the instrumented classes. -->
        <classpath location="lib/cobertura.jar"/>
        <classpath location="bin/instrumented-classes"/>
        <classpath>
          <fileset dir="${libdir}">
            <include name="*.jar" />
          </fileset>
          <pathelement path="${testclassesdir}" />
          <pathelement path="${classesdir}" />
        </classpath>
        <batchtest todir="${testreportdir}">
          <fileset dir="src">
            <include name="**/*Test.java" />
          </fileset>
        </batchtest>
      </junit>
    </target>
    
</project>