I'm building an EAR with ant which needs to include the lib folder (including jars) from my EAR project. I've tried this but although a lib folder is created in the ear file no jars are included. Only the war files are copied into the ear.
<ear destfile="${ear.file}" appxml="META-INF/application.xml">
<dirset dir=".">
<include name="lib" />
</dirset>
<fileset dir="${temp.dir}">
<include name="*.war" />
</fileset>
</ear>
I used the zipfileset task instead, which does the trick:
<ear destfile="${ear.file}" appxml="META-INF/application.xml">
<zipfileset dir="lib" prefix="lib"/>
<fileset dir="${temp.dir}">
<include name="*.war" />
</fileset>
</ear>
Related
I am trying to compile an adroid project using ant which contains multiple sources and external 3rd party jars.
It works fine with eclipse but i require it to run through command line using ant.
my custom_rules.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?> <project name="CustomRules">
<target name="-pre-build" >
<copy todir="tmp-src" >
<fileset dir="src" includes="**" />
<fileset dir="talkAndroid" includes="**" />
<fileset dir="commonAndroid" includes="**" />
<fileset dir="cocos2dAndroid" includes="**" />
</copy>
<classpath id="classpath" description="The default classpath.">
<pathelement path="${classpath}"/>
<fileset dir="external_libs">
<include name="*.jar"/>
</fileset>
</classpath>
</target>
<target name="-post-build" >
<delete dir="tmp-src" />
</target> </project>
it isn't working for external_libs and unable to find external libraries, if I copy these jars from external_libs to libs folder ... duplicate class found errors are generated!
What should I do?
I built a jar file using the following build.xml below:
<project name="sampleproject">
<!--Source Directory-->
<property name="src.dir" value="src" />
<property name="deploy.name" value="samplejar.jar" />
<property name="lib.dir" value="./lib" />
<property name="deploy.dir" value="target" />
<property name="compile.dir" value="target/classes" />
<!--Define directory name for third party libraries-->
<property name="jar-all" location="${lib.dir}" />
<!--Fileset is a group of files, here **/* matches all jar files across all directory levels-->
<fileset id="jars" dir="${jar-all}">
</fileset>
<!--Setting the classpath-->
<path id="classpath">
<fileset refid="jars" />
<pathelement location="." />
<pathelement location="${src.dir}" />
<pathelement location="${compile.dir}" />
</path>
<path id="cp">
<fileset refid="jars" />
</path>
<pathconvert property="classpath.path" refid="cp" pathsep=" " dirsep="/">
<map from="${jar-all}" to="lib" />
</pathconvert>
<echo>${classpath}</echo>
<!--Clean-->
<target name="clean">
<delete dir="${deploy.dir}" />
</target>
<!--Jar-->
<target name="jar" depends="compile">
<jar destfile="${deploy.dir}/${deploy.name}.jar" basedir="${compile.dir}">
<manifest>
<attribute name="Main-Class" value="main.java.Main" />
<attribute name="Class-Path" value="${classpath.path}" />
</manifest>
<include name="${src.dir}/META-INF/persistence.xml" />
<restrict>
<not>
<or>
<name name="**/*.RSA" />
<name name="**/*.SF" />
<name name="**/*.DSA" />
</or>
</not>
<archives>
<zips>
<fileset dir="lib" includes="**/*.jar" />
</zips>
</archives>
</restrict>
</jar>
</target>
<!--Compile-->
<target name="compile">
<mkdir dir="${compile.dir}" />
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${compile.dir}">
<classpath refid="classpath" />
</javac>
</target>
</project>
My folder structure is like this:
.
./.settings
./bin
./bin/main
./bin/main/java
./bin/META-INF
./lib
./lib/aws
./lib/hibernate
./src
./src/main
./src/main/java
./src/META-INF
./target
./target/classes
./target/classes/main
./target/classes/main/java
My MANIFEST.MF
Created-By: 1.8.0_45-b14 (Oracle Corporation)
Built-Date: ${TODAY}
Main-Class: main.java.Main
Class-Path: lib/activation.jar lib/aws/aws-java-sdk-1.3.2.jar lib/aws/
httpclient-4.1.1.jar lib/aws/httpcore-4.4.1.jar lib/aws/mail-1.4.3.ja
r lib/aws/stax-1.2.0.jar lib/aws/stax-api-1.0.1.jar lib/commons-beanu
tils-1.8.3.jar lib/commons-codec-1.4.jar lib/commons-configuration-1.
8.jar lib/commons-httpclient-3.0.1.jar lib/commons-io-2.1.jar lib/com
mons-lang-2.4.jar lib/commons-logging-1.1.1.jar lib/hibernate/antlr-2
.7.7.jar lib/hibernate/c3p0-0.9.1.jar lib/hibernate/commons-collectio
ns-3.2.1.jar lib/hibernate/dom4j-1.6.1.jar lib/hibernate/hibernate-c3
p0-4.0.1.Final.jar lib/hibernate/hibernate-commons-annotations-4.0.1.
Final.jar lib/hibernate/hibernate-core-4.0.1.Final.jar lib/hibernate/
hibernate-entitymanager-4.0.1.Final.jar lib/hibernate/hibernate-jpa-2
.0-api-1.0.1.Final.jar lib/hibernate/javassist-3.15.0-GA.jar lib/hibe
rnate/jboss-logging-3.1.0.CR2.jar lib/hibernate/jboss-transaction-api
_1.1_spec-1.0.0.Final.jar lib/jackson-all-1.9.5.jar lib/log4j-1.2.16.
jar lib/mail.jar lib/mongo-2.8.0.jar lib/morphia-0.99.jar lib/mysql-c
onnector-java-5.1.12-bin.jar lib/mysql-connector-java-5.1.25-bin.jar
lib/org.json.jar lib/ymmi-utilities-2.0.jar
Whenever I run the jar produced it gives me, Error: Could not find or load main class main.java.Main.
Also, I want to include lib folder & it's subfolders as well since it contains the libraries required to run the jar.
Before it gave me, No Persistence provider for EntityManager named but I solved that by using <include name="${src.dir}/META-INF/persistence.xml" />. Any where I am might wrong?
Your problem is the <include> element in the jar task.
I executed your script and the Main.class file is not added to the jar.
Replace the
<include
by
<fileset dir="${src.dir}" includes="META-INF/persistence.xml" />
This way it will still add all files located in your ${compile.dir}.
Are you new to java ? Normally you put your source files in src/main/java and resources in src/main/resources.
These folders are the root folder of your classpath meaning: If your project classes would be in package org.myproject, than you would put the Main.java file in src/main/java/org/myproject/Main.java
In your build script you than set the property "src.dir" to "src/main/java".
With all resources (=files which do not need to be compiled but must be available in the jar) in src/main/resources the fileset element in the jar task could just be:
<fileset dir="scr/main/resources"/>
This way all resource files you plan to add to your project will automatically added to your jar.
Why do I tell you this ? If one day you would think of using maven instead of ant, maven expects the sources to be in src/main/java and resources in src/main/resources. (https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html)
Regarding you other dependant jars:
Now you include all jars twice: you add the content of them using the <archives> element within the jar and you define them in the Class-Path attribute of the manifest.mf file.
If you really like to build a single jar containing all dependant files, you don't need to specify the manifest attribute Class-Path.
If you want your jar file containing only your files and deliver the dependant jars separately: remove the <archives> element within the jar task.
I'm building an ear file using ANT build.xml and need to include .java files in my ear package created. Any pointers?
Below is the war section from my build.xml
<target name="war" depends="compile">
<war destfile="${dist}/${ant.project.name}.war" webxml="${warbasepath}/WebContent/WEB-INF/web.xml">
<lib dir="${lib}" />
<classes dir="${build}"/>
<fileset dir="${warbasepath}/WebContent" excludes="**/*.class **/*.jar" />
</war>
</target>
Take a look at the EAR task manual. It is just an extension of the JAR task, so you can include whatever you like.
You can include arbitrary filesets:
<ear destfile="${build.dir}/myapp.ear" appxml="${src.dir}/metadata/application.xml">
<fileset dir="${build.dir}" includes="*.jar,*.war"/>
<fileset dir="${src.dir}" includes="**/*.java"/>
</ear>
However an EAR should be just a container for wars and ejb jars, so you probably should add your source code rather in those.
I have defined a jar task using ant build.xml. I need to bundle all the dependencies into my jar. I don't understant why ant extracts the jars and includes both .jars and .class files into my jar. It unnecessarily increases the size of my jar file. Here is my jar task.
<jar destfile="build/main/ERS2SupportingUtilities.jar">
<fileset dir="target/classes">
<exclude name="*.sh"/>
</fileset>
<restrict>
<name name="**/*.class"/>
<archives>
<zips>
<fileset dir="./src/main/resources/lib" includes="**/*.jar"/>
</zips>
</archives>
</restrict>
<manifest>
<attribute name="Main-Class" value="com.cibc.ers2.invoker.JobTest"/>
<attribute name="Class-Path" value="./lib/log4j-1.2.16.jar
./lib/org.springframework.context-3.0.3RELEASE.jar
./lib/org.springframework.asm-3.0.3.RELEASE.jar
./lib/junit-4.7.jar
./lib/org.springframework.orm-3.0.3.RELEASE.jar
./lib/org.springframework.transaction-3.0.3.RELEASE.jar
./lib/org.springframework.aspects-3.0.3.RELEASE.jar
./lib/commons-pool-1.5.4.jar
./lib/org.springframework.core-3.0.3.RELEASE.jar
./lib/commons-logging-1.1.1.jar
./lib/HashUtility.jar
./lib/org.springframework.expression-3.0.3.RELEASE.jar
./lib/commons-lang-2.6.jar
./lib/org.springframework.instrument-3.0.3.RELEASE.jar
./lib/mockito-all-1.9.5.jar
./lib/com.springsource.org.aopalliance-1.0.0.jar
./lib/ojdbc14.jar
./lib/commons-io-2.4.jar
./lib/commons-collections-3.1.jar
./lib/org.springframework.jdbc-3.0.3.RELEASE.jar
./lib/spring-batch-infrastructure-2.1.9.RELEASE.jar
./lib/org.springframework.context.support-3.0.3.RELEASE.jar
./lib/commons-dbcp-1.4.jar
./lib/spring-batch-test-2.1.9.RELEASE.jar
./lib/org.springframework.beans-3.0.3.RELEASE.jar
./lib/org.springframework.oxm-3.0.3.RELEASE.jar
./lib/org.springframework.aop-3.0.3.RELEASE.jar
./lib/commons-beanutils.jar
./lib/org.springframework.binding-2.1.1.RELEASE.jar
./lib/spring-batch-core-2.1.9.RELEASE.jar
./lib/org.springframework.test-3.0.3.RELEASE.jar
./launch-context.xml
./log4j.xml"
/>
</manifest>
</jar>
I have also tried zipgroupfileset but that also is giving the same problem.
EDIT
Apologies for not giving enough info about what I am trying to achieve. I have got an application which I am compilint # target/classes. I need to package this application including my class files and dependecies into one jar.
<fileset dir="target/classes">
<exclude name="*.sh"/>
</fileset>
includes all files from target/classes which are not shell scripts.
<restrict>
<name name="**/*.class"/>
<archives>
<zips>
<fileset dir="./src/main/resources/lib" includes="**/*.jar"/>
</zips>
</archives>
</restrict>
It seems like you again include classes and jars here.
If you want only jar change all above to :
<zipfileset dir="./src/main/resources/lib" includes="*.jar"/>
In my build.xml, below works fine :-
<path id="build.classpath">
<fileset dir="lib [myUtils]" includes="*.jar" />
</path>
if lib [myUtils] is of folder type, but don't works, if it's of Linked Folder type.
Also, I found this when googled :-
https://bugs.eclipse.org/bugs/show_bug.cgi?id=265960
https://bugs.eclipse.org/bugs/show_bug.cgi?id=43081
https://bugs.eclipse.org/bugs/show_bug.cgi?id=265960
Is there any trick to achieve this, without copying the dependencies in work folder??
Note that ant should work outside of eclipse as well. So you can't reply in IDE abstractions. You can use symbolic links (if your OS supports them).
If not, you can use the FileSync plugin to synchronize eclipse project folders with external folders. Or you can simply use the <copy> ant task.
I resolved this using copy task:
<copy todir="target/web/linked1">
<fileset dir="../linkdProject/source1" />
</copy>
<copy todir="target/web/linked2">
<fileset dir="../linkedProject/source2" />
</copy>
....
<war destfile="target/webApp.war">
<fileset dir="WebContent" />
<fileset dir="target/web" /> <!-- copy linked resources -->
...
</war>
<delete dir="target"/>