Include java libraries into own library

别等时光非礼了梦想. 提交于 2019-12-20 06:17:28

问题


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?


回答1:


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.




回答2:


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.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/8538245/include-java-libraries-into-own-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!