Java/Ant/Jar

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

Add attribute to jar file manifest

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





Ant jar file setting the Main-Class

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





Ant task: jar

 
<?xml version="1.0"?>
<project name="sample" default="test" basedir=".">
    <!--  NOTE: THE FOLLOWING LINES SHOULD BE EDITED FOR YOUR PROJECT:  -->
    <property name="root.project.name" value="demo2"/>
    <property name="test.class" value="test.TestSample"/>
   <!-- NOTE: THE REST OF THIS FILE CAN PROBABLY REMAIN UNCHANGED FOR YOUR NEW PROJECTS -->
   <property name="jar.module" value="${root.project.name}.jar"/>
    <path id="test.classpath">
        <pathelement location="dist/${jar.module}"/>
        <pathelement location="dist/test.jar"/>
    </path>
   <target name="compile">
      <mkdir dir="build"/>
      <javac destdir="build"
             debug="on"
             optimize="on">
         <src path="src"/>
      </javac>
   </target>
   <target name="makejar" depends="compile">
      <mkdir dir="dist"/>
      <jar jarfile="dist/${jar.module}">
         <fileset dir="build">
             <include name="**/*.class"/>
       <exclude name="test/*.class"/>
         </fileset>
       </jar>
      <jar jarfile="dist/test.jar">
         <fileset dir="build">
             <include name="test/*.class"/>
         </fileset>
       </jar>
      <delete dir="build"/>
   </target>
   <target name="test" depends="makejar">
      <java fork="no" failonerror="yes"
            classname="${test.class}">
          <classpath refid="test.classpath"/> 
          <arg line=""/>
      </java>
   </target>
   <target name="clean">
      <delete dir="build"/>
      <delete dir="dist"/>
   </target>
</project>





Generates java2s.jar

 
<?xml version="1.0"?>
<project name="Template Buildfile" default="compile" basedir=".">
  <property name="dir.src" value="src"/>
  <property name="dir.build" value="build"/>
  <property name="dir.dist" value="dist"/>
  <!-- Creates the output directories -->
  <target name="prepare">
    <mkdir dir="${dir.build}"/>
    <mkdir dir="${dir.dist}"/>
  </target>
  <target name="clean"
          description="Remove all generated files.">
    <delete dir="${dir.build}"/>
    <delete dir="${dir.dist}"/>
  </target>
  <target name="compile" depends="prepare"
          description="Compile all source code.">
    <javac srcdir="${dir.src}" destdir="${dir.build}"/>
  </target>
  <target name="jar" depends="compile"
          description="Generates jexp.jar in the "dist" directory.">
    <jar jarfile="${dir.dist}/jexp.jar" 
         basedir="${dir.build}"/>
  </target>
</project>





Jar file: exclude files

 
<?xml version="1.0"?>
<project name="yourname" basedir=".." default="all">
  <property name="dist" location="dist/"/> 
  <property name="lib" location="lib/"/> 
  <property name="src" location="src/"/> 
  <path id="class.path">
    <pathelement path="${src}"/> 
    <fileset dir="${lib}">
      <include name="**/*.jar"/>
      <include name="**/*.zip"/>
    </fileset> 
    <fileset dir="/dev">
      <include name="**/*.jar"/>
      <include name="**/*.zip"/>
    </fileset> 
  </path>
  
  
  <target name="clean">
    <delete>
      <fileset dir="${src}" includes="**/*.class"/>
    </delete>
    <delete dir="${dist}"/>
  </target>
  
  <target name="zip" depends="clean">
    <tstamp/>
    <mkdir dir="${dist}"/>
    <zip destfile="${dist}\actionServlet-${DSTAMP}${TSTAMP}.zip">
      <zipfileset dir=".">
        <exclude name="${dist}"/>
      </zipfileset>
    </zip>
  </target>
  <target name="compile">
    <javac>
      <src path="${src}" />
      <classpath refid="class.path"/> 
      <include name = "*/**" />
    </javac>
   </target>
  <target name="jar" depends="compile">
    <mkdir dir="${dist}"/>
    <tstamp/>
    <jar
      basedir="src"
      jarfile="${dist}/actionServlet.jar"
      excludes="**/*.java, *.mdb"
    />
  </target>
  <target name="all" depends="jar"/>
</project>





Jar file with fileset and exclude

 
<?xml version="1.0"?>
<!--
 -  build.xml file for LBS logics aMorph build process (for Ant Version 1.6)
 -
 -  (c) LBS logics MBT GmbH,
 -      Austria
 -      www.lbs-logics.ru
 -
 - $Id: build.xml,v 1.1 2005/01/18 07:26:05 hanzz Exp $
 -->
<!--
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
-->
<project name="amorph"  default="build"  basedir=".">
  <!-- PH20050118: introduced ant build script, targets to come... -->
  
  <!-- The classpath to be used to compile the project -->
  <path id="base.classpath">
    <fileset dir="lib">
      <include name="**/*.jar"/>
    </fileset>
  </path>
  
  <target name="init">
    <mkdir dir="build" />
    <mkdir dir="build/classes"/>
  </target>
  
  <target name="compile">
    <javac srcdir="src" destdir="build/classes">
      <classpath refid="base.classpath" />
    </javac>
  </target>
  
  <target name="build" description="compiles and packs the project" depends="init,compile">
    <jar destfile="build/amorph.jar"
           basedir="build/classes"  >
      <fileset dir=".">
        <exclude name="build/**/*.*"/>
        <exclude name=".classpath"/>
        <exclude name=".project"/>
        <exclude name="bin/**/*.*"/>
      </fileset>
    </jar>  
  </target>
</project>





Jar with includes and excludes

 
<?xml version="1.0"?>
<project name="JAR Demos" default="jar1" basedir=".">
  <property name="dir.src" value="src"/>
  <property name="dir.build" value="build"/>
  <property name="dir.dist" value="dist"/>
  <!-- Creates the output directories -->
  <target name="prepare">
    <mkdir dir="${dir.build}"/>
    <mkdir dir="${dir.dist}"/>
  </target>
  <target name="clean"
          description="Remove all generated files.">
    <delete dir="${dir.build}"/>
    <delete dir="${dir.dist}"/>
  </target>
  <target name="compile" depends="prepare"
          description="Compile all source code.">
    <javac srcdir="${dir.src}" destdir="${dir.build}"/>
  </target>
  <target name="jar1" depends="compile"
          description="Basic jar task demo">
    <jar jarfile="${dir.dist}/jexp.jar" basedir="${dir.build}"/>
  </target>
  <target name="jar2" depends="compile"
          description="Shows includes and excludes">
    <jar jarfile="${dir.dist}/jexp.jar" 
         basedir="${dir.build}"
         includes="**/*.class"
         excludes="**/UnitTest*.class"/>
  </target>
  <target name="jar3" depends="compile"
          description="Same as jar2, but uses filesets">
    <jar jarfile="${dir.dist}/jexp.jar">
      <fileset dir="${dir.build}" 
               includes="**/*.class"
               excludes="**/UnitTest*.class"/>
      <fileset dir="${dir.src}"
               includes="**/*.properties"/>
    </jar>
  </target>
</project>





Jar with includes and excludes using filesets

 
<?xml version="1.0"?>
<project name="JAR Demos" default="jar1" basedir=".">
  <property name="dir.src" value="src"/>
  <property name="dir.build" value="build"/>
  <property name="dir.dist" value="dist"/>
  <!-- Creates the output directories -->
  <target name="prepare">
    <mkdir dir="${dir.build}"/>
    <mkdir dir="${dir.dist}"/>
  </target>
  <target name="clean"
          description="Remove all generated files.">
    <delete dir="${dir.build}"/>
    <delete dir="${dir.dist}"/>
  </target>
  <target name="compile" depends="prepare"
          description="Compile all source code.">
    <javac srcdir="${dir.src}" destdir="${dir.build}"/>
  </target>
  <target name="jar1" depends="compile"
          description="Basic jar task demo">
    <jar jarfile="${dir.dist}/jexp.jar" basedir="${dir.build}"/>
  </target>
  <target name="jar2" depends="compile"
          description="Shows includes and excludes">
    <jar jarfile="${dir.dist}/jexp.jar" 
         basedir="${dir.build}"
         includes="**/*.class"
         excludes="**/UnitTest*.class"/>
  </target>
  <target name="jar3" depends="compile"
          description="Same as jar2, but uses filesets">
    <jar jarfile="${dir.dist}/jexp.jar">
      <fileset dir="${dir.build}" 
               includes="**/*.class"
               excludes="**/UnitTest*.class"/>
      <fileset dir="${dir.src}"
               includes="**/*.properties"/>
    </jar>
  </target>
</project>





More than one filesets for jar

 
<!--
  About this file (build.xml):
  Last modified 2001-11-30
  Author        Matthias Beil
  Contributor   Guillaume Desnoix
  License       GNU General Public License 2 (GPL2)
                Copyright (c) 2001 Matthias Beil
                All rights reserved.
-->
<!--  -->
<project name="alma" default="main" basedir="." >

<!--  -->
<!-- Set up application values -->
<property name="app.name"    value="alma" />
<property name="app.package" value="com/memoire" />
<property name="app.version" value="0.36" />
<property name="app.year"    value="2001" />

<!--  -->
<!-- directory locations -->
<property name="dir.classes"        location="classes" />
<property name="dir.dist"           location="dist" />
<property name="dir.javadocs"       location="docs" />
<property name="dir.project"        location="." />
<property name="dir.src"            location="." />
<property name="dir.package" value="com/memoire" />
<!-- debug setting -->
<property name="debug" value="on"/>
<!-- javadoc package -->
<property name="packages" value="com.memoire.*"/>

<!--  -->
<!-- Show System JVM -->
<target name="showsystem" >
   <!-- might not work!? -->
   <showsystem />
</target>

<!-- Create necessary directories -->
<target name="prepare">
   <tstamp />
   <mkdir dir="${dir.classes}" />
   <mkdir dir="${dir.dist}" />
</target>

<!-- Compiles the java source files -->
<target name="compile" depends="prepare">
   <javac
      srcdir="${dir.src}"
      destdir="${dir.classes}"
      classpath="${dir.src}"
      debug="${debug}"
      failonerror="no" >
      <include name="com/memoire/acme/Acme*.java"/>
      <include name="com/memoire/re/RE*.java"/>
      <include name="com/memoire/mst/Mst*.java"/>
      <include name="com/memoire/pbc/Pbc*.java"/>
      <include name="com/memoire/fu/Fu*.java"/>
      <include name="com/memoire/dnd/Dnd*.java"/>
      <include name="com/memoire/xml/Xml*.java"/>
      <include name="com/memoire/yapod/Yapod*.java"/>
      <include name="com/memoire/bu/Bu*.java"/>
      <include name="com/memoire/dja/Dja*.java"/>
      <include name="com/memoire/agl/Agl*.java"/>
      <include name="com/memoire/alma/Alma*.java"/>
      <include name="com/memoire/jedit/JEdit*.java"/>
      <include name="com/memoire/editor/Editor*.java"/>
      <include name="com/memoire/foo/Foo*.java"/>
      <include name="com/memoire/silk/Silk*.java"/>
      <include name="com/memoire/script/Script*.java"/>
      <exclude name="com/memoire/ant/*.java"/>
      <exclude name="com/memoire/editor/EditorJext.java"/>
      <exclude name="com/memoire/script/ScriptInterpreterBeanshell.java"/>
      <exclude name="com/memoire/script/ScriptInterpreterDawn.java"/>
      <exclude name="com/memoire/script/ScriptInterpreterFiji.java"/>
      <exclude name="com/memoire/script/ScriptInterpreterFoo.java"/>
      <exclude name="com/memoire/script/ScriptInterpreterPnuts.java"/>
      <exclude name="com/memoire/script/ScriptInterpreterPython.java"/>
      <exclude name="com/memoire/script/ScriptInterpreterRhino.java"/>
      <exclude name="com/memoire/script/ScriptInterpreterRuby.java"/>
   </javac>
</target>

<!-- Creates the jar file for distribution -->
<target name="dist" depends="compile">
   <jar jarfile="${dir.dist}/${app.name}.jar"
        manifest="manifest-alma.txt" >
      <fileset dir="${dir.classes}" >
         <patternset>
            <exclude name="**/dependency cache"/>
         </patternset>
      </fileset>
      <fileset dir="${dir.src}" >
         <patternset>
            <include name="**/*.dja"/>
            <include name="**/*.foo"/>
            <include name="**/*.gif"/>
            <include name="**/*.txt"/>
         </patternset>
      </fileset>
   </jar>
</target>

<!-- Compile and build a distribution -->
<target name="main" depends="compile, dist" >
   <echo>Alma application package created</echo>
</target>

<!-- Create the API documentation -->
<target name="javadocs" depends="prepare">
   <mkdir dir="${dir.javadocs}"/>
   <javadoc packagenames="${packages}"
            sourcepath="${dir.src}"
            destdir="${dir.javadocs}"
            classpath="${dir.src}"
            author="true"
            version="true"
            use="true"
            windowtitle="${app.name} API"
            doctitle="&lt;h1&gt;${app.name}&lt;/h1&gt;"
   />
</target>

<!-- Deletes the build and dist directories -->
<target name="clean" >
   <delete dir="${dir.dist}" />
   <delete dir="${dir.classes}" />
</target>


<target name="all" depends="clean,main,javadocs" >
   <echo>Alma application package cleaned and created</echo>
</target>

<!-- run application -->
<target name="app-run" depends="main" >
   <java classname="com.memoire.alma.Alma"
      classpath="${dir.dist}/${app.name}.jar"
      fork="yes">
   </java>
</target>
<target name="run-alma" depends="main" >
   <java classname="com.memoire.alma.Alma"
      classpath="${dir.dist}/${app.name}.jar"
      fork="yes">
   </java>
</target>
<target name="run-dja" depends="main" >
   <java classname="com.memoire.dja.Dja"
      classpath="${dir.dist}/${app.name}.jar"
      fork="yes">
   </java>
</target>
<target name="run-agl" depends="main" >
   <java classname="com.memoire.agl.Agl"
      classpath="${dir.dist}/${app.name}.jar"
      fork="no">
   </java>
</target>
<target name="run-foo" depends="main" >
   <java classname="com.memoire.foo.Foo"
      classpath="${dir.dist}/${app.name}.jar"
      fork="no">
   </java>
</target>
<target name="run-reformatage" depends="main" >
   <java classname="com.memoire.agl.AglOutilReformatage"
      classpath="${dir.dist}/${app.name}.jar"
      fork="no">
   </java>
</target>
<target name="run-coloration" depends="main" >
   <java classname="com.memoire.agl.AglOutilColoration"
      classpath="${dir.dist}/${app.name}.jar"
      fork="no">
   </java>
</target>

</project>