Change project dependency folder (lib)

Asked

Viewed 2,692 times

3

When compiling my project on Netbeans the files are generated in this way:

./dist
./dist/meuProjeto.jar
./dist/readme.txt
./dist/lib
./dist/lib/dependencia1.jar
./dist/lib/dependencia2.jar

And I want it to be generated without the folder lib, thus:

./dist
./dist/meuProjeto.jar
./dist/readme.txt
./dist/dependencia1.jar
./dist/dependencia2.jar

I know you have to change the build.xml, but I couldn’t find anything that referred to how to alter the fate of .jar dependent.

2 answers

2

I believe you are looking to do something like "Big Jar", packaging all dependencies within a single file. jar

For this you must use the "Ant".

In Netbeans, open the build.xml file and leave the "project" tag as below by replacing "NOME_DO_SEU_JAR" by the desired jar name:

<project name="NOME_DO_SEU_JAR" default="default" basedir=".">
<description>DESCRICAO_DO_SEU_PROJETO</description>
<import file="nbproject/build-impl.xml"/>

    <target name="-post-jar">  


    <property name="store.jar.name" value="NOME_DO_SEU_JAR"/>  

    <property name="store.dir" value="store"/>  
    <property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>  

    <echo message="Packaging ${store.jar.name} into a single JAR at ${store.jar}"/>  

    <delete dir="${store.dir}"/>  
    <mkdir dir="${store.dir}"/>  

    <jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">  
        <zipgroupfileset dir="dist" includes="*.jar"/>  
        <zipgroupfileset dir="dist/lib" includes="*.jar"/>  

        <manifest>  
            <attribute name="Main-Class" value="${main.class}"/>                  
        </manifest>  
    </jar>  

    <zip destfile="${store.jar}">  
        <zipfileset src="${store.dir}/temp_final.jar"  
        excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>  
    </zip>  

    <delete file="${store.dir}/temp_final.jar"/>  

</target> 

  • 1

    My intention is not to generate only one .jar, but generate the .jar and its dependency libraries in the same folder.

  • 1

    @Gleison I’ve thought about doing this too, but I’ve had a lot of problems accessing dependency [.jar], you’ll notice this if you work with files. To do exactly what you want I did not get a solution. I hope I helped in some way. Hug.

0


To achieve the desired result I had to make two adjustments.

first In the archive meuProjeto\nbproject\build-impl.xml I adjusted the line:

<globmapper from="*" to="lib/*"/>

To:

<globmapper from="*" to="*"/>

This line aims to define the path from where the .jar dependent, in relation to the .jar running. This path is generated by this script and is in the manifest.mf that is saved within the .jar compiled.

2nd In the archive meuProjeto\build.xml added the lines:

<target name="-post-jar">
    <echo message="Copiando as bibliotecas dependentes para o mesmo diretório do jar"/>
    <copy todir="${dist.dir}">
        <fileset dir="${dist.dir}\lib"/>
    </copy>
    <delete dir="${dist.dir}\lib"/>
</target>

PS: Remembering that this solution is extremely dependent on the IDE used, in this case the Netbeans.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.