English 中文(简体)
Ivy, ant and start scripts
原标题:

I have a project that uses ant to build and ivy for dependencies. I would like to generate the start scripts for my project, with the classpath, based on the dependencies configured in Ivy, especially as the order of dependencies may be important and needs to be preserved from the order in the ivy config.

Has anyone done this before? I also need to generate relative paths in the classpath so I can t use absolute paths as this will only work for the machine on which the build is done.

EDIT: Based on feedback if we cut Ivy out the equation (do the resolve to a directory of my choice) I can then probably resolve the list of libs ok. But how would I generate a classpath suitable for a start script, especially with the relative paths (relative to my bin directory)?

e.g.

install
    /bin <-- scripts here
    /lib <-- jars here

So in my bin/start.sh I need to have ../lib/ in front of every jar reference rather than a full absolute path.

Thanks.

问题回答

Since many years (2000?), we had this small script in path ("make_cp")

#!/usr/bin/perl

my $CLASSPATH="";
my $DIR=shift;
$DIR||="lib";

opendir(LIBDIR, $DIR);
while ($file = readdir(LIBDIR)) {
    $CLASSPATH.=":$DIR/$file" if ($file =~ /.jar$|.zip$/);
}
closedir(LIBDIR);
$CLASSPATH=~ s/^://g;
print "$CLASSPATH";

Used like this:

export CLASSPATH=`make_cp lib`:`make_cp external-lib`

Since Ivy evicts overlapping dependencies and tries to find the best common dependency for all the projects I don t really understand how the order of dependencies would matter at all.

However you should make a standard JAR/WAR/other with Ant for your project and include Ivy dependencies inside that JAR. Basically all you should need to do is to make Ivy s Ant task to resolve the dependencies to a folder, then build tha classes using those dependencies and then consruct the JAR so that you include the library JAR:s to newly created JAR s /lib/ folder.

Like Esko said, you should create a JAR including all required JAR archives:

<zip destfile="abc.jar">
    <zipgroupfileset dir="lib/distributed" includes="*.jar"/>
    <manifest>
        <attribute name="Main-Class" value="com.acme.MyClass"/>
    </manifest>
</zip>

After that, your start script is simply:

java -jar abc.jar

If you re using java 1.6 you can use file globs (i.e. java -cp "../lib/*"). If you re using an earlier version of java and you don t want to use Vladimir s solution, you ll need to write a script that figures out what the classpath should be.

So launch.sh looks something like:

cd dirname %0 # change to the bin directory, use %0/.. instead and you can replace ../lib with just /lib
sh set_classpath.sh  # set the classpath
java -cp $CLASSPATH some.package.Main 

and set_classpath.sh will have some linux magic that sets CLASSPATH equal to something like "../lib/abc.jar:../lib/def.jar"

export CLASSPATH=`ls *.jar | sed  s/[^.jar].jar/../lib/:/ `




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签