[Buildfile] Apache Ant

02/23/2015 14:30 Lee Ki-Hwan#1
Hey Leute,
ich versuch mich das erste mal an Apache Ant und scheitere beim erstellen einer ausführbaren Jar datei.
Also er erstellt zwar die Jar, aber die is 150kb groß, statt wie wenn IntelliJ es macht 7MB, die Jar funktioniert auch, mein Programm geht auf, aber irgendwie Funktionieren die Funktionen nicht.

Hier mal meine Build.xml
Code:
<project xmlns:ivy="antlib:org.apache.ivy.ant" 
       name="transfer-mainline" default="main" basedir=".">
	<description>
		Build XML Validation for Okto-tranfser-mainline
	</description>
 
	<property name="projectName" value="XMLValidation" />
	<property name="src.dir" location="src" />
	<property name="build.dir" location="bin" />
	<property name="dist.dir" location="dist" />
	<property name="dist.lib.dir" location="dist/lib" />
	<property name="lib.dir" value="C:\Users\xxxx\IdeaProjects\Libraries" />
	<property name="main-class" value="net.oktopos.transfer.Main" />
 
	<target name="init">
		<mkdir dir="${build.dir}" />
	</target>
 
	<path id="classpath">
		<fileset dir="${basedir}/">
			<include name="${lib.dir}/*.jar" />
		</fileset>
		 <pathelement location="${lib.dir}/commons-codec-1.6.jar" />
		 <pathelement location="${lib.dir}/commons-logging-1.1.3.jar" />
		 <pathelement location="${lib.dir}/fluent-hc-4.3.6.jar" />
		 <pathelement location="${lib.dir}/guava-18.0.jar" />
		 <pathelement location="${lib.dir}/hamcrest-all-1.3.jar" />
		 <pathelement location="${lib.dir}/httpclient-4.3.6.jar" />
		 <pathelement location="${lib.dir}/httpclient-cache-4.3.6.jar" />
	     <pathelement location="${lib.dir}/httpcore-4.3.3.jar" />
		 <pathelement location="${lib.dir}/httpmime-4.3.6.jar" />
	     <pathelement location="${lib.dir}/ini4j-0.5.2-SNAPSHOT.jar" />
	     <pathelement location="${lib.dir}/ini4j-0.5.2-SNAPSHOT-jdk14.jar" />
	     <pathelement location="${lib.dir}/jcalendar-1.4.jar" />
		 <pathelement location="${lib.dir}/jgoodies-common-1.2.0.jar" />
		 <pathelement location="${lib.dir}/jgoodies-looks-2.4.1.jar" />
	     <pathelement location="${lib.dir}/joda-time-2.4.jar" />
		 <pathelement location="${lib.dir}/json-simple-1.1.1.jar" />
		 <pathelement location="${lib.dir}/junit-4.6.jar" />
		 <pathelement location="${lib.dir}/junit-4.10.jar" />
		 <pathelement location="${lib.dir}/mockito-all-1.9.5.jar" />
	</path>
 
	<target name="compile" depends="init" description="compile the source ">
		<echo message="${lib.dir}" />
		<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" />
	</target>
 
	<pathconvert property="classpath.name" pathsep=" ">
		<path refid="classpath" />
		<mapper>
			<chainedmapper>
				<flattenmapper />
				<globmapper from="*.jar" to="lib/*.jar" />
			</chainedmapper>
		</mapper>
	</pathconvert>
 
	<target name="copy-dependencies">
		<copy todir="${dist.lib.dir}">
			<fileset dir="${lib.dir}" includes="**/*.jar" excludes="**/*javadoc.jar" />
		</copy>
	</target>
 
	<target name="jar" depends="compile, copy-dependencies" description="package, output to JAR">
 
		<echo message="classpath.name : ${classpath.name} " />
 
		<mkdir dir="${dist.dir}" />
		<mkdir dir="${dist.lib.dir}" />
 
		<jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
			<manifest>
				<attribute name="Main-Class" value="${main-class}" />
				<attribute name="Class-Path" value="${classpath.name}" />
			</manifest>
		</jar>
	</target>
 
	<target name="clean" description="clean up">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
	</target>
 
	<target name="main" depends="clean, compile, jar" />
 
</project>
02/23/2015 15:24 XxharCs#2
Hmm, imo siehts auf den ersten Blick richtig aus..
Packt er alle Klassen, depencies usw in die jar?

Ich poste dir mal eine Vorlage von mir die ich benutze und dann immer weiter ausbaue:
Kannst es dann abgleichen:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<project name="ANT Bsp" default="compile" basedir=".">

	
	<property name="src.dir" value="src" />
	<property name="lib.dir" value="lib" />
	<property name="classes.dir" value="build/classes" />
	<property name="build.dir" value="build" />
	<property name="doc.dir" value="build/doc" />
	<property name="test.dir" value="build/tests" />
	
	<path id="main_classpath">
		<pathelement location="${lib.dir}/junit-4.12.jar" />
		<pathelement location="${lib.dir}/hamcrest-core-1.3.jar" />
		<pathelement path="${java.class.path}" />
	</path>
	
	<!-- Source files kompilieren -->
	<target name="compile" depends="clean" description="Compile project.">
		<mkdir dir="${classes.dir}" />
		<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="main_classpath" />
	</target>
	
	<!-- build Ordner leeren/loeschen -->
	<target name="clean" description="Clean build products.">
		<delete dir="${build.dir}" />
	</target>
	
	<!-- Javadoc erstellen -->
	<target name="create-doc" description="Create javadoc.">
		<javadoc sourcepath="${src.dir}" destdir="${doc.dir}" />
	</target>
	
	<!-- Javadoc loeschen -->
	<target name="clean-doc" description="Delete javadoc.">
		<delete dir="${doc.dir}"/>
	</target>
	
	<target name="test" depends="run-tests" description="JUnit test.">
	    <mkdir dir="build/report"/>
		<junitreport todir="build/report">
		    <fileset dir="build/report">
    			<include name="TEST-*.xml"/>
  			</fileset>
	  		<report format="frames" todir="build/report/html"/>
		</junitreport>
	</target>
	
	<target name="run-tests">
		<junit printsummary="yes" haltonfailure="false" haltonerror="false" fork="yes">
			<classpath>
				<pathelement location="${lib.dir}/junit-4.12.jar" />
				<pathelement location="${lib.dir}/hamcrest-core-1.3.jar" />
				<pathelement path="${classes.dir}" />
			</classpath>
			<batchtest fork="yes" todir="build/report">
				<formatter type="xml" />
				<fileset dir="${classes.dir}" >
					<include name="**/*Test*.class" />
				</fileset>
			</batchtest>
		</junit>
	</target>
	
	<!-- jar erzeugen -->
	<target name="jar" depends="clean, clean-doc, compile, create-doc" description="Creates jar file.">
	    <mkdir dir="build/jar"/>
	    <jar destfile="build/jar/${ant.project.name}.jar" basedir="${classes.dir}">
	        <manifest>
	            <attribute name="Main-Class" value="package.ClassName"/>
	        </manifest>
	        <fileset dir="./build" includes="doc/**" />
	    </jar>
	</target>
</project>
(JUnit tests sind hier mit inkludiert)
02/23/2015 15:32 Lee Ki-Hwan#3
Quote:
Originally Posted by XxharCs View Post
Hmm, imo siehts auf den ersten Blick richtig aus..
Packt er alle Klassen, depencies usw in die jar?

Ich poste dir mal eine Vorlage von mir die ich benutze und dann immer weiter ausbaue:
Kannst es dann abgleichen:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<project name="ANT Bsp" default="compile" basedir=".">

	
	<property name="src.dir" value="src" />
	<property name="lib.dir" value="lib" />
	<property name="classes.dir" value="build/classes" />
	<property name="build.dir" value="build" />
	<property name="doc.dir" value="build/doc" />
	<property name="test.dir" value="build/tests" />
	
	<path id="main_classpath">
		<pathelement location="${lib.dir}/junit-4.12.jar" />
		<pathelement location="${lib.dir}/hamcrest-core-1.3.jar" />
		<pathelement path="${java.class.path}" />
	</path>
	
	<!-- Source files kompilieren -->
	<target name="compile" depends="clean" description="Compile project.">
		<mkdir dir="${classes.dir}" />
		<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="main_classpath" />
	</target>
	
	<!-- build Ordner leeren/loeschen -->
	<target name="clean" description="Clean build products.">
		<delete dir="${build.dir}" />
	</target>
	
	<!-- Javadoc erstellen -->
	<target name="create-doc" description="Create javadoc.">
		<javadoc sourcepath="${src.dir}" destdir="${doc.dir}" />
	</target>
	
	<!-- Javadoc loeschen -->
	<target name="clean-doc" description="Delete javadoc.">
		<delete dir="${doc.dir}"/>
	</target>
	
	<target name="test" depends="run-tests" description="JUnit test.">
	    <mkdir dir="build/report"/>
		<junitreport todir="build/report">
		    <fileset dir="build/report">
    			<include name="TEST-*.xml"/>
  			</fileset>
	  		<report format="frames" todir="build/report/html"/>
		</junitreport>
	</target>
	
	<target name="run-tests">
		<junit printsummary="yes" haltonfailure="false" haltonerror="false" fork="yes">
			<classpath>
				<pathelement location="${lib.dir}/junit-4.12.jar" />
				<pathelement location="${lib.dir}/hamcrest-core-1.3.jar" />
				<pathelement path="${classes.dir}" />
			</classpath>
			<batchtest fork="yes" todir="build/report">
				<formatter type="xml" />
				<fileset dir="${classes.dir}" >
					<include name="**/*Test*.class" />
				</fileset>
			</batchtest>
		</junit>
	</target>
	
	<!-- jar erzeugen -->
	<target name="jar" depends="clean, clean-doc, compile, create-doc" description="Creates jar file.">
	    <mkdir dir="build/jar"/>
	    <jar destfile="build/jar/${ant.project.name}.jar" basedir="${classes.dir}">
	        <manifest>
	            <attribute name="Main-Class" value="package.ClassName"/>
	        </manifest>
	        <fileset dir="./build" includes="doc/**" />
	    </jar>
	</target>
</project>
(JUnit tests sind hier mit inkludiert)
Dank erstmal.
Habs verglichen und es ist auch richtig, ist halt komisch IntelliJ generiert die Jar mit knapp über 7MB und das Ant Build machts nur mit 150KB.
Ich denke das irgendwo ein Include/Verweiss oder sowas fehlt, aber dann müsste der Compiler sich ja beschweren..

Und die .class Files die erstellt werden sind auch kleiner als meine.
15,9KB von Ant Build und IntelliJ 25KB.
02/23/2015 15:41 XxharCs#4
Quote:
Originally Posted by The Notorious B.I.G View Post
Und die .class Files die erstellt werden sind auch kleiner als meine.
15,9KB von Ant Build und IntelliJ 25KB.
Hm, das ist komisch, wenn du die Main-Class mal manuell ausführen würdest(nach dem compilen mit ant), funktioniert dann das Programm richtig?
Wenn es funktioniert dann wird das jar File nicht richtig erstellt^^
02/23/2015 15:48 Lee Ki-Hwan#5
Also habe den Bug gefunden es lag nicht an dem Ant Build sondern dass ich im Source Code eine Resource nicht geladen werden kann aus irgendeinen Grund.
Main.class.getResource(""); <- war der übeltäter.
02/23/2015 15:51 XxharCs#6
Oh^^ Ja so ein Problem hatte ich auch beim SecurityManager als ich das Policy file geladen hab. Musste dann mit System.class.getResource().toString() arbeiten, und dann ging das jar file auch.

Aber um ehrlich zu sein, zu dem Moment hat bei mir das Build-Script beim compilen gemekert, konnte es also schnell beheben, in deinem Fall hat er nicht gemekert hm :confused: ^^
02/23/2015 16:07 Lee Ki-Hwan#7
Ja war schon komisch, was man noch erwähnen kann bezüglich der größe ist, dass die per Ant-Build extern sind, also meine Jar includiert quasi den Ordner "lib".
Falls irgendjemand auch mal sich fragt wo die Jar dann so klein ist.. :p
03/22/2015 07:02 kiinqof0wnz1#8
i need help for android