How to include a resource file in the Jar file generated by Netbeans? - netbeans

I'm using NB6.7 to auto generate an executable jar file for my project, how and where to tell NB to include a certain resource into the result jar file ? I know if I put the resource in the "lib" directory, NB will put it into "dist/lib/", but what I want is to include the resource within the final executable jar, where in NB do I specify that ?
Frank

There is a way to add arbitrary file/folders to arbitrary location within the jar, without setting up source folders.
NetBeans will, by default, create build.xml at project root.
We can override the -pre-jar target to copy the files and -post-jar target to cleanup.
For example, to copy "res/resource.ext" into "myres/resource.ext" within the jar, add these target to build.xml:
<target name="-pre-jar">
<!-- Single file -->
<copy file="res/resoure.ext" todir="${build.dir}/classes/myres" />
<!-- Folder(s) -->
<copy todir="${build.dir}/classes/myres"><fileset dir="res"/></copy>
</target>
<target name="-post-jar">
<delete dir="${build.dir}/classes/myres/resource.ext" />
<delete dir="${build.dir}/classes/myres"/>
</target>
Since this is Ant, you have great control.
You can rename file, replace file content (e.g. update version or timestamp), convert charset, or run other operations that is not possible with simple inclusion.
I am using NetBeans version 8, but as long as the project is compatible it is likely to work.
Alternatively, use an automated project such as Maven or Gradle, that gives you even more control on the build process, at cost of IDE features such as automatic single file recompile.

I got it : put the resource file in the src dir.

you can do it by...
Go to your project name which is normally on the left hand side of the screen.
Whatever your project name is,
you have to right click on it and then go to the properties of that. [which is normally at the end in menu item]
Then one window will open in that go to the Libraries.
In that you will find
1)ADD PROJECT
2)ADD Libraries
3)ADD JAR FILE
then include the jar / Libraries / folder.
so that will add automatically to your project.
Which will include in lib folder automatically so you don't have to include always.
Hope this help you,

Related

Include documentation in a JAR file

With Eclipse it is possible to attach a documentation folder to a jar file in order to see the jar classes' documentation while programming. My question is: is it possible to directly include the documentation into the jar file, in order to avoid the programmer to link the two parts every time they need to use the jar file? (and so to let me to distribute just a jar file, rather than a zip file with the jar and its documentation).
EDIT
final result: it is impossible. :(
I didn't get what do you mean with "directly include the documentation", but you can put anything in the jar file. If you use ant (it can be inside Eclipse), just create a target for your distribution build and pass the documentation folder to Jar task.
<jar destfile="${dist}/lib/app.jar">
<fileset dir="${build}/classes"
excludes="**/Test.class"
/>
<fileset dir="${src}/documentation"/>
</jar>

Netbeans, how to build project deploy(war) file into custom folder?

Netbeans project, is it possible to build deploy(.war) file into domain's autodeploy folder automatically, not in dist folder?
So that, it would be VERY useful to deploy project .war file to local glassfish server automatically.
You can automate almost all kinds of things, from compilation files processed by ant stages.
In project.properties file must add a reference for your end folder
war.custom.dir=/path/to/end/folder/
"this is not mandatory but is a correct method"
Then, in your build.xml file should add this chunk of code
<target name="-post-dist">
<echo message="Copy .war file to my custom folder"/>
<copy file="${dist.war}" todir="${war.custom.dir}" />
</target>
And your file .war, will move wherever you want.
Could also be changed your file in the build-impl.xml target "do-ear-dist"
allowing directly effect the compression to your end folder.
Or you could also modify references in your project to work on your end folder, the mechanism is really very simple to customize as you think best.

Export Jar File in NetBeans

I am looking for a way to export a .jar file in Netbeans. I know that when I clean/build it automagically puts everything in a .war file in the dist directory. However, in Eclipse you can select Export -> Export -> Java -> JAR file and click next and select only the resources you want to export and select a location to export the .jar file to. Is there any equivalent to this in Netbeans?
you can enable the "Build JAR after Compiling" property in Netbeans
File > Project Properties > Build > Packaging
I ended up just building the project the usual way getting a .war file in the dist folder.
I then used a zip program called IZArc: http://www.izarc.org/ to open the .war and get out only what I needed to build the .jar. I then used IZArc again to create the .jar file.
If anyone knows of an easier way (preferably through Netbeans itself) please let me know.
If you know how to write Ant files, edit the build.xml and add a -post-dist target. Then the jar file will also be created when the war file gets created. Something like:
<target name="-post-dist" description="Create jar file.">
<!-- Copy your files to a temp directory if necessary. -->
<copy ...>
...
</copy>
<!-- 'basedir' should be the directory from which to jar the files -->
<jar destfile="${dist.dir}/app.jar" basedir="${build.dir}/myjarfiles"/>
</target>

Can i save comment info in my Jar file(generated by Ant)?

When I build my project to a Jar file with Ant, I generate javadoc the same time.
But when I use this Jar in a new Project, I can't see the Jar's comment in the Eclipse(pressing F2).
So I tried to add the javadoc files' location path in 'javadoc Location' of Properties of my new Project,it doesn't work, even if Eclipse tell me the 'Location is likely valid'.
When I open it in Browser,the Url is like file:///file:/C:/Users/topxebec/Desktop/doc/index.html, there's two prefix in the url,but in the eclipse I only have file:/C:/Users/topxebec/Desktop/doc/index.html,.
I tried to change it to C:/Users/tongxuebin/Desktop/doc/ but Eclipse said it was a Invalid URL.
So how to solve this problem? Or,can I simply save the comments in the Jar when it generated by Ant?
It seems, that you can't put the javadoc in the same jar as the class files, so that eclipse will automatically show the doc.
A solution is to include the source-code. This will also help, if you need to debug the classes in that jar.
This answer: How can I build my jar file so that users who use the library will be able to see the javadoc in Eclipse covers that case. But it just boils down to:
<target name="jar.noCompile.src">
<jar destfile="${ant.project.name}.jar">
<fileset dir="${classes}"/>
<fileset dir="${src}" includes="**/*.java"/>
</jar>
</target>

Modification of NetBeans Ant build script

I have an project in NetBeans. I made some preparations to make database migration easier, and I put all things related to migration in one folder in root of my project.
Now I want to make this whole directory included in the distribution jar or war.
I deducted that i have to modify the ="-pre-dist" target, but I not very familiar with and and I'm lost in all of these netbeans options, paramaters predefined in build script.
Assuming my migration data is in folder "migration", how should ant command look to include this folder in distrubution archive?
I understood from your question that you want to achieve the following:
You have a directory migration with some contents in the root of the project
You want this directory to be included in the JAR file created as a result of clean and build command in NetBeans IDE
To achieve this add the following ANT target in your build.xml file:
<target name="-pre-jar">
<mkdir dir="${build.classes.dir}/migration"/>
<copy todir="${build.classes.dir}/migration">
<fileset dir="${basedir}/migration"/>
</copy>
</target>
Now when you run the command clean and build the resulting JAR file will contain the folder migration with all the contents. Note: this target is not "-pre-dist" but is "-pre-jar"