specifying class directory in ant manifest - ant

The manifest stuff in my build.xml looks like this
<manifest file="MANIFEST.MF">
<attribute name="Built-By" value="Me" />
<attribute name="Main-Class" value="LogReporter"/>
</manifest>
However my .class files are in another directory basedir/src. Is there a way to specify the directory of the class files?
Running the current jar gives me a classdefnotfound error.
Also, I'm not looking for the classpath attribute because all the class files are in the same project/jar.

You class files should be in a directory corresponding to their package. For example if the package name is com.some.package all class files should be located in a directory com/some/package. Consecutively you should refer to your main class with its fully qualified name - for example com.some.package.MainClass.
The Java Tutorials contain a good example: http://download.oracle.com/javase/tutorial/deployment/jar/appman.html.

Related

executable jar using ant and ivy - CLASSPATH issues

I'm trying to build an executable jar file using ant managed by ivy but am stuck. Our original build-script assembles the jar file more or less okay. The dependencies are in the manifest.mf but not under Class-Path but rather Compile-Class-Path entry.
I can simply set the Main-Class entry in the menifest file but having an impossible foe in trying to get the ivy dependencies in the Class-Path. While this seems simple enough using gradle I can't find any solution for ivy dependencies.
Is there a way of getting the resolves ivy dependencies and put them in the manifest? These dependencies are just paths to a network location where the jar files are.
I'm giving a standard way to do this. If you can provide your actual build file, I can be more specific in the answer.
You can do this in the ant target for jar creation. For ex:
<!-- create a classpath variable with all the jars needed for runtime -->
<path id="cls.path">
<!-- declare all the paths that you need. For ex: all resolved jars in "runtime" conf -->
</path>
<!-- If your path has folder prefix, you'll have to do <pathconvert> -->
<jar jarfile="${jar_name}" basedir="${classes.dir}">
<manifest>
<attribute name="Class-Path" value="${cls.path}"/>
...
<!-- You can add standard jar properties and any custom property here -->
</manifest>
</jar>

want to move files in jar during merging

I have several external jar files in /lib directory
like
/lib
/lib/A.jar
/lib/B.jar
/lib/C.jar
and each JAR file has same named file in their jar file. the path is META-INF/license.txt
so if I use zipgroupfileset task with dir attibute like dir="/lib/" include *.jar in jar task
META-INF/license.txt file is duplicated
XML code is below
<jar jarfile="${dest}/merged_jar.jar">
<zipgroupfileset dir="${basedir}/lib" />
<include name="**/*.jar"/>
</zipgroupfileset>
</jar>
after this works, merged_jar.jar is created but in this file, /META-INF/license.txt is duplicated
I don't want to exclude license.txt file but want to move each file's license.txt file to another folder to gather and add suffix or prefix(eg. /license/license_A.txt) during merging task
thanks to your help.
Use the prefix attribute to put some prefix path on the destination directory.
<zipfileset dir="license" includes="*.txt" prefix="license/licence"/>
This will copy all text files inside license directory to the license/licence directory

adding jars dynamically from build.xml ant

I created a jar file with dependencies(i.e wlclient.jar, wljmxclient.jar) added to manifest file Class-Path attribute. I get error saying "Unsupported protocol: t3", But if I place these jars in C:\apache-ant-1.8.3\lib folder, this executes with no errors.
I am new to ant please help.
The ant task for creating a jar file is given below.
<jar destfile="projectpoc.jar" basedir="bin" excludes="**/Test.class">
<manifest>
<attribute name="Class-Path"
value=" lib/mysql-connector.jar lib/log4j-1.2.14.jar lib/ojdbc6.jar lib/wlclient.jar lib/wljmxclient.jar " />
</manifest>
</jar>
I'd recommend use the manifestclasspath ANT task. It will correctly resolve paths relative to the jar file.
See the following answer: Ant + Class-path Issue

Ant read a property file from within a zip file

Is there a way in ant to load properties from inside a zip file?
I have a project ant file which needs to use some properties that are in a file that is inside a zip file. The zip file is stored in a known location on our CI server.
/known location/file.zip
|
+--- properties/details.properties
The following doesn't work
<project name="test" basedir="." >
<property file="/known location/file.zip/properties/details.properties"/>
....
</project>
Since zip files and jar files are basically the same, you can use the url form of the property task, with a jar url.
<property url="jar:file:/known location/file.zip!/properties/details.properties" />
Note the jar:file: at the front of the url, and the !/ separating the zip file location from the path of the properties file within the zip.
See the JarURLConnection docs for more info on the syntax of a jar: url.
you could unzip the file to a temp location and then load the unzipped properties file
<target name="load-zipped-props">
<unzip src="${propfile-name}.zip" dest="${unzip-destination}" />
<property file="${unzip-destination}/${propfile-name}.properties"/>
</target>

How to include directory structure in an ant jar file?

I am a bit of an ant newbie, and I'm having trouble making a jar correctly. As an example, say I want to make a jar with my StringUtil class. Using the following ant directive, I can create the jar, but the problem is that the directory structure is lost. It simply puts StringUtil.class in the base directory of the jar. How can I correct this ant directive so that StringUtil.class is inside the com/test directory in the jar?
<jar destfile="myjar.jar" >
<fileset file="${build}/com/test/StringUtil.class"/>
</jar>
Thanks!
You need to tell Ant to build the jar from the base directory, then tell it to include only the desired file. Like so:
<jar destfile="myjar.jar" >
<fileset dir="${build}" includes="com/test/StringUtil.class"/>
</jar>
Here's the doc for <fileset> tags.

Resources