Eclipse automation with ant - java

In Eclipse (Neon) a runnable .jar can be created via File > Export > Runnable jar > Next and interacting with the dialog that appears. I wanted to eliminate those steps. I automated the export of my .jar file by creating the following ant file which is called makeJar.xml and resides in the project directory.
<project name="makeJar" default="makeJarTarget" basedir=".">
<property name="jar" value="../export/ohana1/ohana1.jar"/>
<target name="makeJarTarget">
<jar destfile="${jar}" basedir="bin"/>
</target>
<target name="cleanTarget">
<delete file="${jar}"/>
</target>
</project>
This ant file is visible in the Project > Properties > Builders dialog. At the top of the list of builders is the default builder called Java Builder and next in the list is this additional builder which is the aforementioned ant file and it appears as makeJar.
Assume that at this point an edit to the Java source code happens. The first step of saving any code changes will cause .class files to be updated. The next step of clicking Build All will update the .jar file.
It would be more ideal if saving any code changes (without having to click Build All) had that same effect. Unfortunately it does not. Saving code changes results in only .class files being updated; the .jar will be unchanged (out-of-date).
It is possible to reason why this is the case. In the Eclipse Project menu, Build All is a choice that is distinct from Build Automatically. So if code changes are saved and Build Automatically was previously set (this menu item can "remember" a check mark setting) the .class files are updated but nothing more than that. There is no menu item to perform something like Build All Automatically. Is there any way to further automate so that saving code changes is sufficient to generate not only .class files but also the .jar?
EDIT: Caution: "Build All" has the effect of building all projects that are open. Before clicking "Build All" you might want to close any projects that you are merely browsing.

In the Navigator view double-click the file .project to open it and remove the line:
<triggers>full,incremental,</triggers>
After the Builder configuration is edited, this has to be done again.

Related

Launch4J - how to attach dependent jars to generated exe

I have a simple java project, which requires external jars.
I build this with netbeans and after Clean and Build command, I can find in dist directory the following structure:
-myApp.jar
-lib/
library1.jar
library2.jar
typical, I would say.
Now, I'd like to distribute myApp.jar with dependent libraries as one exe.
Is this possible? I am trying to use Launch4J. In the GUI I create the config file, there are some options in cp section
<cp>lib/swing-layout-1.0.4.jar</cp>
but it seems to be classpath, and it is the only place I can refer to my extra jars.
After exe file is created, I can't find dependend libs in the exe (exe can be opened with winrar) and thus my application crashes.
How can I make the exe file properly then?
Thanks for your help.
As it often happens being unable to solve the problem I published it on StackOverflow ... and pretty soon after publishing the question I got an idea.
So the answer to my question is:
Put all the dependent jars into one main jar.
It took me some time to find info how can I do that.
To help people I decided to publish detailed instruction here - they are based on Netbeans 7.4.
Following article from http://mavistechchannel.wordpress.com/2010/08/17/how-to-build-a-single-jar-file-with-external-libs/ I created the ant script that build one-jar-app for me.
I could then manually create exe via Launch4J
I then decided that I want more automated task, and I did that, Ant builds exe for me (via Launch4J)
Then I realized that I must do "clean and build" before my automated task (in point 2)/ I decided that I want clean and build to be done automatically before the exe build
Putting all together I am attaching my ant build script consisting of points 1,2,3:
It is required to edit build.xml and put the content found below before "project" end tag
<target name="package-for-launch4j" depends="clean,compile,jar">
<property name="launch4jexe.dir" location="C:\Program Files (x86)\Launch4j" />
<taskdef name="launch4j"
classname="net.sf.launch4j.ant.Launch4jTask"
classpath="${launch4jexe.dir}/launch4j.jar
:${launch4jexe.dir}/lib/xstream.jar" />
<property name="launch4j.jar.name" value="MyAppJarName"/>
<property name="launch4j.dir" value="exe"/>
<property name="launch4j.jar" value="${launch4j.dir}/${launch4j.jar.name}.jar"/>
<echo message="Packaging ${application.title} into a single JAR at ${launch4j.jar}"/>
<delete dir="${launch4j.dir}"/>
<mkdir dir="${launch4j.dir}"/>
<jar destfile="${launch4j.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="${launch4j.jar}">
<zipfileset src="${launch4j.dir}/temp_final.jar"
excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete file="${launch4j.dir}/temp_final.jar"/>
<launch4j configFile="misc/l4j-myapp.xml" />
</target>
then in Netbeans rightclick on the build.xml and choose:
Run Target / Other Targets / package-for-launch4j
exe file is ready in exe folder :-)
When you are converting your .jar file
Go to classpath tab
Check custom classpath
On main class select your .jar from your dist folder after building the project
On the classpath textarea add your libraries, you add them right below that
textarea writting the full path to the lib (of course including the
lib, ie "C:\folder\lib\file.jar")
I have spent hours on this issue. So here is my contribution.
The problem here: how to sucessfully put your external jars that your .JAR program needs INSIDE the .exe that you generate.
We assume that you already, and correctly, configured the external jars on eclipse/netbeans and ALL WORK FINE with the command: java -jar yourprogram.jar.
So the real problem is how to ensure that this .EXE file will contain the external jars, otherwise it will not work properly.
1) First of all, you need to forget Launch4J and anyother program.
2) Install JSmooth, I recommend that you use the windows version.
3) On the left menu there is a button "Application". Click on it.
4) You will see a tab "Classpath" panel. Click on the plus (+) and add your external .jar's files. And that is it!!
Don't forget to put your .jar application marking checkbox "use am embedded jar" and choose the main class properly. It will work.
I also faced the same issue while migrating my .jar to exe. I also had many dependent libraries as well. So These were the steps I performed :
Download and Install launch4j.
Open your project in netbeans. Clean and build the project.
Make sure you have a folder named 'dist' in the project directory. It will have your jar files with lib folder(containing the dependent libraries).
Open launch 4j.
Create output file in the dist folder. For example : OutputFile : D:******\My_App\dist\my_application.exe
Browse your jar file in the next row. For example : Jar : D:******\My_App\dist\my_application.jar
Go to classpath tab. Tick CustomClasspath. Press browse icon, and browse to your jar file which is located in the dist folder.
Specify Min Jre version in the JRE tab.
Save the configration.
Build the wrapper(by clicking the settings icon)
Your exe file will be generated in the dist folder.
Thats it :)
Putting different links of places which had helped me
How to include all dependent Jars within a single non-executable jar?
How can I create an executable JAR with dependencies using Maven?
http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
and most importantly
http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html
quick tut
http://www.mkyong.com/java/how-to-add-your-manifest-into-a-jar-file/
To include external libraries with Launch4j you have to have the ".jar" files of the external libraries near your .exe (mine are just in the same folder) then in "Classpath" you put in the path to the .jar files into "Edit Item: "
In launch4j go to the classpath tab. Tick the custom classpath field. In the edit field, enter the full path of each jar you want included and press accept. When finished type just the name of the main class into the separate field (ie MyProg). All the jars will now be included in the exe.
PS I have all the jars in the same directory. I'm using version 3.12

Some clarifications about how ant copy some files into a folder? [duplicate]

This question already has an answer here:
Some clarification about how ant copy some files into a folder?
(1 answer)
Closed 8 years ago.
I am pretty new to ant (I came from Maven and Ant is a nightmare for me !!!)
I have this target:
<target name="linuxdriver"
description="Linux Driver">
<copy file="${deps.linuxdriver.dir}/${deps.linuxdriver.name}"
tofile="${project.datadir}/${deps.linuxdriver.name}"/>
<copy file="${deps.linuxdriver.dir}/${deps.linuxdriver.name}"
tofile="${project.deploy}/data/${deps.linuxdriver.name}"/>
<chmod perm="+x" file="${project.datadir}/${deps.linuxdriver.name}"/>
<chmod perm="+x" file="${project.deploy}/data/${deps.linuxdriver.name}"/>
</target>
and I have also a property file in which there is definied the "variable" (are named variable?) used in the previous ant code, specifically I have:
project.datadir = ${basedir}/data
project.deploy.dir = Release
project.deploy = ${basedir}/../${project.deploy.dir}
deps.linuxdriver.name = atmosfs
And now I have some doubts:
1) What represents ${basedir}? A specific directory? What? Reading on the ant manual (http://ant.apache.org/manual/properties.html) say that this is: the absolute path of the project's basedir (as set with the basedir attribute of ).
So, is it the absolute path of my project in the Eclipse workspace?
2) Using the previous information what exactly are the two destination folder in which the files are copied (using the "copy file...to file" tag)?
1) What represents ${basedir}? A specific directory?
Yes. ${basedir} is the directory where you either started Ant, or the directory specified in the <project> entity on the top of your Ant file. Normally, it is set to "." which makes it the same directory as the directory that contains your Ant build file.
2) Using the previous information what exactly are the two destination folder in which the files are copied (using the "copy file...to file" tag)?
You didn't list your whole Ant file, and your whole properties file. I'm not even sure if your properties file is read in (You need a <property file="xxxx.properties"/> near the top of your Ant file).
Assuming that you are executing this in the same directory as your Ant file, and your ${basedir} is the same directory as your Ant file:
<copy file="${basedir}/atmosfs/atmostfs"
verbose="true"
tofile="${basedir}/Release/atmosfs"/>
<copy file="${basedir}/atmofs/atmofs"
verbose="true"
tofile="${basedir}/../Release/data/atmofs"/>
Again, I am assuming ${basedir} is the directory where your Antfile is stored, and that you are executing the script from that directory.
Notice I have verbose="true" in the <copy>. I recommend you make that change. This will show you what file is being copied and where when <copy> is executed. It's probably the best way to handle this.
By the way, one rule I have is that all action takes place in the project tree. Your last tofile is being written outside of the project directory (where I assume your Ant file is located). Imagine someone checking out the project, and finding out that the build process wrote a file outside of the checked out directory and onto his computer in a random place. Doing this is just considered impolite.
Even more polite is to write all files and do all build processing under a subdirectory. Some people use build, I prefer target because that's a Maven standard. THe idea is that I can clean up the entire build process by simply deleting that one directory.

Create automatically JAR with ANT and Eclipse

I want to make the file build automatically using Eclipse. I need this file creates the JAR of the application. And if it were possible, I would like to insert the command to pass some test of JUnit. How can I do all of this automatically?
Generally to create jar files in Eclipse I do this things:
create an ant file with the necessary code to create the jar file I need
configure the ant file to be processed when something change in my project files: and to do this I open the project properties, I choose Builders, "New..." and I add a Ant builder that use my ant file
In the ant files I put for example something similar:
<project name="My Project" default="createjar">
<property name="projectHome" location="." />
<target name="createjar">
<jar destfile="${projectHome}/file.jar" basedir="${projectHome}/bin" />
</target>
</project>
You can add other instructions to the ant file and process whatever you need after the jar creation. But my suggestion is to not launch JUnit test on very file change, can be very ugly.

Updating files in a WAR/JAR reguardless of their date

I have an ANT task that uses the jar task to update a few files inside of a previously built war. [The files are processed between compilation of a WAR and deliverable.] How can I get ANT to update all of the files I've specified to be updated? There is an attribute for jar called update = "[...]" that will allow you to either force a new creation of the Jar file or (add new files/"update existing ones if deemed necessary").
An example:
Java class(es) are compiled
Jars are created
A .war is created
A script runs to modify the Jars
The .war needs to update the Jars that were modified [the Jars are a subset of all of the files in the war]
The problem I'm running into is that the Jars that are being instructed to be updated aren't getting updated in the final step. The log claims that the just processed Jars are "out of date." Is there a way I can force the update to happen? If I switch "jar [...] update" back to false (which its by default) the correct Jar files get placed there, but the rest of the files in the war don't.
Note I realize that this could be moved to produce the WAR after everything is done. But this is not an option for me.
The ant task in question:
<target name="(the 'Rewaring task')" depends="step-3">
<echo>Adding modifed jars to war</echo>
<jar destfile="${output.war.dir}/existing.war" update="true">
<zipfileset dir="${output.jar.dir}/modded-jars" prefix="folder" />
</jar>
</target>
More specifically, the problem I'm having is with the "update" behavior.
It looks like the Jar task is correct.
Are you sure the files being updated have the correct Date modified time - that is what is actually compared in the zipfileset/update option?
I've seen this issue when signing Jars [and had the preservelastmodified option set to "true" (default : false)].
To fix it, you should only need to change the signing option.

Build project into a JAR automatically in Eclipse

I have an Eclipse project where I want to keep my Java project built into a JAR automatically. I know I have an option to export the project into a JAR; if I do a right click; but what I am really looking for is, that like Eclipse automatically builds a project's .class files and put them in target folder; it should also build a JAR automatically and copy the latest JAR at some or a specific location.
Is there a option to configure Eclipse in such a way, to build JARs automatically?
Just to make it clear for guys, patient enough to answer my question; I am not looking at ANT as solution; as I already use it, but what I would like it something that gets initiated automatically either with a time based trigger or immediate build with change.
You want a .jardesc file. They do not kick off automatically, but it's within 2 clicks.
Right click on your project
Choose Export > Java > JAR file
Choose included files and name output JAR, then click Next
Check "Save the description of this JAR in the workspace" and choose a name for the new .jardesc file
Now, all you have to do is right click on your .jardesc file and choose Create JAR and it will export it in the same spot.
Create an Ant file and tell Eclipse to build it. There are only two steps and each is easy with the step-by-step instructions below.
Step 1
Create a build.xml file and add to package explorer:
<?xml version="1.0" ?>
<!-- Configuration of the Ant build system to generate a Jar file -->
<project name="TestMain" default="CreateJar">
<target name="CreateJar" description="Create Jar file">
<jar jarfile="Test.jar" basedir="." includes="*.class" />
</target>
</project>
Eclipse should looks something like the screenshot below. Note the Ant icon on build.xml.
Step 2
Right-click on the root node in the project.
- Select Properties
- Select Builders
- Select New
- Select Ant Build
- In the Main tab, complete the path to the build.xml file in the bin folder.
Check the Output
The Eclipse output window (named Console) should show the following after a build:
Buildfile: /home/<user>/src/Test/build.xml
CreateJar:
[jar] Building jar: /home/<user>/src/Test/Test.jar
BUILD SUCCESSFUL
Total time: 152 milliseconds
EDIT: Some helpful comments by #yeoman and #betlista
#yeoman I think the correct include would be /.class, not *.class, as most
people use packages and thus recursive search for class files makes
more sense than flat inclusion
#betlista I would recomment to not to have build.xml in src folder
Check out Apache Ant
It's possible to use Ant for automatic builds with eclipse, here's how
This is possible by defining a custom Builder in eclipse (see the link in Peter's answer). However, unless your project is very small, it may slow down your workspace unacceptably. Autobuild for class files happens incrementally, i.e. only those classes affected by a change are recompiled, but the JAR file will have to be rebuilt and copied completely, every time you save a change.
Regarding to Peter's answer and Micheal's addition to it you may find How Do I Automatically Generate A .jar File In An Eclipse Java Project useful. Because even you have "*.jardesc" file on your project you have to run it manually. It may cools down your "eclipse click hassle" a bit.
Using Thomas Bratt's answer above, just make sure your build.xml is configured properly :
<?xml version="1.0" ?>
<!-- Configuration of the Ant build system to generate a Jar file -->
<project name="TestMain" default="CreateJar">
<target name="CreateJar" description="Create Jar file">
<jar jarfile="Test.jar" basedir="bin/" includes="**/*.class" />
</target>
</project>
(Notice the double asterisk - it will tell build to look for .class files in all sub-directories.)
Creating a builder launcher is an issue since 2 projects cannot have the same external tool build name. Each name has to be unique. I am currently facing this issue to automate my build and copy the JAR to an external location.
I am using IBM's Zip Builder, but that is just a help but not doing the real.
People can try using IBM ZIP Creation plugin.
http://www.ibm.com/developerworks/websphere/library/techarticles/0112_deboer/deboer2.html#download

Categories