Modification of NetBeans Ant build script - netbeans

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"

Related

Eclipse: Why is it eating my log4j.properties file?

Windows 7 Professional
Eclipse 3.7.2
I am migrating over to Eclipse from another IDE. I am compiling my project with the same ANT build.xml file I was using before. I do it by
highlighting my project in the project view
expanding the tree node
finding my build.xml file there
right clicking my build.xml file
choosing Run as > Ant Build
Works great, but every few builds Eclpise eats my log4j.properties file located at:
C:\AllProjects\Workspace\acme\war\WEB-INF\classes\log4j.properties
Eclipse basically eats all files in WEB-INF\classes that are not *.class files.
What am I doing to make this happen and how can I stop it?
Thanks
Since you're using ant instead of Eclipse to build, you could try turning off the setting to build the project automatically.
Select your project, then in the menu bar at the top of the screen, select Project and uncheck the Build Automatically option.
Edit: Second opinion
I would add an ant task to copy the log4j property file into your WEB-INF/classes folder every time you do a build. One nice advantage of this is that you can have a different property file for different build types (debug, release, etc.) and not worry about manually making changes to it. Also, this should make it easier to manage if you're using version control.
Copy your log4j.properties file to your source directory and add something like this to your ant build file:
<target name="copy-log4j-property-file">
<copy file="src/log4j.properties" todir="WEB-INF/classes" />
</target>
<target name="build" depends="copy-log4j-property-file">
<!-- the rest of your build things here -->
</target>
After selecting in Project explorer:
Properties > Java Compiler > Building > Output Folder
Uncheck "Scrub output folders when cleaning projects"
It is a solution for me ( though maybe not everyone ) because I use a time test ANT build.xml to do my compiling, building and cleaning for me. I just leave Eclipse's auto builds on so I get those nice error notifications as a I type, before I compile.

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.

Netbeans custom files in project

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.

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>

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,