0

I created a little framework for myself which I want to use in multiple projects. I also want the distributed jar-file to include all external libraries so that my projects just need to include my library to access all external libraries.

I need this to simplify updating the external libraries.

So I placed this in my build.xml which adds all libraries from dist/lib into my own jar-file.

<target name="-post-jar">  
    <!-- Include all java libraries -->
    <fileset dir="dist/lib" id="extern.libs">
        <include name="*.jar" />
    </fileset>

    <!-- Add the libraries from the fileset to the project.jar -->
    <jar jarfile="${dist.jar}" update="true">  
        <zipgroupfileset refid="extern.libs"/>
    </jar>  
</target>  

But when I try to use external libraries like "org.zkoss.zk.ui.Component" I get the error that this library could not be found.

Is there a better way to include the external libraries into my own library so that my project can use them?

mw88
  • 489
  • 2
  • 7
  • 17

4 Answers4

2

You can publish a Maven artifact, which users of your framework can then use without having to include the dependencies yourself--your pom is enough.

If you want to create an "all-in-one" artifact, consider something like OneJar or jarjar or Maven's Shade plugin to create a jar that has no external dependencies.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I think using maven would be overkill for my needs. I'm trying out OneJar at the moment but I couln't get it to work until now... – mw88 Dec 16 '11 at 20:06
1

The standard classloader can't find class files inside a jar that is itself inside a jar. You must add every jar to the classpath, and not nest jars.

BTW, it would probably be a bad idea to allow nesting jars: you would end up with 6 or seven versions of commons-lang or log4j into every project, because many libraries depend on them.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • That is a good point and I have to think about it. I just want to be able to add one library to my projects and this library should drag in all the dependancies. – mw88 Dec 16 '11 at 20:10
  • That's what Maven (and Ivy) are all about: managing dependencies. I personally find it often harder to use Maven than to handle the dependencies myself, but YMMV. – JB Nizet Dec 16 '11 at 20:13
1

You can use One-jar or Fat Jar. If you use maven you can use maven-assembly plugin.

Andrea Colleoni
  • 5,919
  • 3
  • 30
  • 49
  • I'm currently trying OneJar, Fat Jar seems to be an Eclipse Plugin but I use NetBeans ;-) – mw88 Dec 16 '11 at 20:07
0

Depends on IDE...If you are using Eclipse then it is very easy...go to Properties->Build Path and then add library...

sum2000
  • 1,363
  • 5
  • 21
  • 36
  • I use NetBeans and there is a similar property for adding libraries but I want my projects to be able to access all libraries that are included in my own library project. – mw88 Dec 16 '11 at 18:09
  • I believe the OP is asking about including the dependent libraries in the built jar. Adding jars to the build path in the IDE will not automatically include them built jar. – Andy Dec 16 '11 at 18:14
  • Yes that is the point. But I want to be able to access the libraries in my project too. – mw88 Dec 16 '11 at 20:11