Netbeans custom files in project - netbeans

I need to add some custom files into my Java project in Netbeald, so after each build it will copy them automatically into output folder.
How can I achieve this?

You may tweak your build.xml like this:
<target name="-post-jar">
<copy file="my/dir/file.txt" todir="build/classes/my/package"/>
</target>

If you don't store those files together with the Java files, then add the directory where they are located to the project as a "Source Package Folder"
Everything in the "Source Package Folders" will automatically be copied to the build directory.

Related

How Exclude some files from Eclipse Java EE Web Application project Deployment Assembly

As described into question title, I've an Eclipse Java EE Web Application project. Into WebContent folder I've some files and subfolder in which there are .DS_store OsX system files that cause some problem because they are always included when I export into war file.
I know that there is a way for excluding a file or folder but only into src folder, not into WebContent!
Any ideas?
Regards
Why not remove the from (delete or just move to another folder) if they shouldnt be in your webcontent folder?
Otherwise you could use the Deployment Assembly settings. Open it from your project properties, and remove the top WebContent and add what you really want to get packaged.
Also you can exclude the files/folder by setting a resource filter. Add a filter... as shown below:
you can use war task of ant. say you located your ant build file directly inside your web-project then the following work for you which excludes folder .DS_store and all its contents from exported war file.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="war" name="TestWeb">
<target description="export-war" name="war">
<war destfile="/home/guest/Desktop/TestWeb1.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent">
<exclude name="**/.DS_store/**"/>
</fileset>
<classes dir="build" />
</war>
</target>
</project>

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>

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"

How to include a resource file in the Jar file generated by 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,