简体   繁体   English

常春藤,蚂蚁和启动脚本

[英]Ivy, ant and start scripts

I have a project that uses ant to build and ivy for dependencies. 我有一个项目使用ant构建和依赖的常春藤。 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. 我想基于Ivy中配置的依赖项,使用类路径为我的项目生成启动脚本,特别是因为依赖项的顺序可能很重要,需要从常春藤配置中的顺序中保留。

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)? 但是,如何生成适合启动脚本的类路径,尤其是相对路径(相对于我的bin目录)?

eg 例如

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. 所以在我的bin/start.sh我需要在每个jar引用前面都有../lib/而不是完整的绝对路径。

Thanks. 谢谢。

Since many years (2000?), we had this small script in path ("make_cp") 多年(2000年?),我们在路径中有这个小脚本(“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. 由于Ivy驱逐了重叠的依赖关系,并试图找到所有项目的最佳公共依赖关系,我并不真正理解依赖关系的顺序如何重要。

However you should make a standard JAR/WAR/other with Ant for your project and include Ivy dependencies inside that JAR. 但是,您应该为项目创建一个标准的JAR / WAR /其他Ant,并在该JAR中包含Ivy依赖项。 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. 基本上你需要做的就是让Ivy的Ant任务解决文件夹的依赖关系,然后使用这些依赖关系构建类,然后构造JAR,以便将库JAR:s包含到新创建的JAR的/ lib /文件夹中。

Like Esko said, you should create a JAR including all required JAR archives: 像Esko说的那样,你应该创建一个JAR,包括所有必需的JAR档案:

<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 (ie java -cp "../lib/*"). 如果你使用的是java 1.6,你可以使用文件globs(即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. 如果您使用的是早期版本的java并且不想使用Vladimir的解决方案,则需要编写一个脚本来确定类路径应该是什么。

So launch.sh looks something like: 所以launch.sh看起来像:

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" 和set_classpath.sh将有一些Linux魔法,将CLASSPATH设置为“../lib/abc.jar:../lib/def.jar”之类的东西

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM