How to distribute junit tests to another machine - java

The build.xml we have today have many targets to compile and run unit tests. The build.xml refers to many property files with relative to itself. All this works fine when build and test is done on same machine.
It is pretty simple to do build get all jars and any test input files to a build_home(another machine). How to run junit on the new location? Should I create small build.xml files to running the tests? (There is no way to create ant build.xml dynamically) Any solutions?
( GridGain is possible solution. Not tried yet. )
Edit: Mode details on why this more complicated: The source code is around 3G, doing clearcase update and build takes considerable time (40 minute) against real test time of junit testing - 60 minutes. We have many machines to run the tests -- loading Clearcase on all systems not possible.

I understand your question as you want to only run the Junit tests on another machine without actually building on it? You can run something on the lines below as build scripts from Cruise control and probably Hudson too
If you're using the task via ant, then follow the same principles as a standard build. You can check out the code to all target machines from source control.
Externalize all root directories to a build.properties. These are properties which have to be set on each machine like so.
#Overall Project Name
project.name=myapp
# Top Level Root directory of the new working project
toplevel.project.dir=D:/proj/eComm
# Root directory of the source code
root.project.dir=D:/proj/eComm/Construction
# JDK home directory
jdk.home=C:/jdk1.5.0_11
build.properties will also have some static properties defined relative to the above. These need not be changed by any user on any local machine.
ear.dist.dir = ${root.project.dir}/target
src.dir = ${root.project.dir}/src
test.src.dir = ${root.project.dir}/test
Ensure your build.xml only refers to any further sub directories via these properties without any hardcoded values in it.
My junit are in a separate file which is imported into the build.xml by
<import file="${root.project.dir.buildscripts.dir}/junit.xml"/>
and some part of junit.xml is shown below
<target name="run.junit" depends="clean.junit, junit.info, prepare.junit"
description="Compiles and runs all JUnit Tests in the 'test' directory and produces a report of all failures">
<junit printsummary="yes" fork="true" haltonfailure="no" showoutput="yes" maxmemory="512m">
<jvmarg line="${junit.jvm.arg}"/>
<classpath>
<fileset dir="${src.dir}">
<include name="**/*.*"/>
</fileset>
<fileset dir="${ear.dist.dir}/build/classes">
<include name="**/*.*"/>
</fileset>
<pathelement path="${test.run.path}"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="true" todir="${ear.dist.dir}/build/junit">
<fileset dir="${test.src.dir}" includes="${test.pattern.include}"/>
</batchtest>
</junit>
</target>

Try Cruise Control. It's a great way to offload your build and unit test to another machine.

Related

Copy external docs (from xyz-javadoc.jar) into JavaDocs

I want to create a .jar file with Ant, which contains all JavaDocs of my library and its dependencies. I did a lot of searching yesterday afternoon/evening and this morning, but none of the solutions work for me.
My first solution:
<!-- Generate JavaDoc -->
<javadoc sourcepath="${src}" destdir="${doc}" windowtitle="${ant.project.name}">
<classpath path="${lib}/nv-websocket-client-2.9-javadoc.jar"/>
<classpath path="${lib}/gson-2.8.6-javadoc.jar"/>
</javadoc>
My second solution:
<!-- Generate JavaDoc -->
<javadoc sourcepath="${src}" destdir="${doc}">
<classpath>
<fileset dir="${lib}">
<include name="gson-2.8.6-javadoc.jar"/>
<include name="nv-websocket-client-2.9-javadoc.jar"/>
</fileset>
</classpath>
</javadoc>
In both cases, however, only the JavaDoc for my own code is produced. The libraries are completely ignored. In the log of the Ant-Task there are errors, that the classes from the libraries were not found.
I don't know if this is the best aproach, but for me it's easier to use "jar" from command line.
Then all you have to do is indicate where are your files located:
jar uf0 path_to_your_jar\your_jar_file.jar path_to_your_files\*.*
jar uf0 path_to_your_jar\your_jar_file.jar path_to_your_other_files\*.*
If the libraries you want to add are already packed in a jar file, I would extract them first in the root directory, so the path of every file is correct. If you execute the previous commands, you'll have all your files in the "your_jar_file.jar" file.
if you type jar --help from command line you'll see more options. I hope it helps.

Building a project and creating a war in different Environments using Ant

am working on an ant build script which builds and deploys the project, and stages the code to the staging environments, ie creates a war file in the staging environments. For example the staging environments in my case are as follows: Am giving these properties in the build.properties file.
Env1_Deploy
Env2_Deploy
Env3_Deploy
Env4_Deploy
Env5_Deploy
PROD_DEPLOY
If you notice the case, all the Env..... are in mixed case where as in PROD_DEPLOY is in upper case.
The way my ant script works is when I select the build.xml-->runas-->AntScript, It prompts a dialogue box where it asks me to select the environment to build. Then I can select one among the above and then it creates the war file in the selected env.
The issue am having is when I select the PROD_DEPLOY, it is creating a new folder PROD_DEPLOY_Deploy and then creating the war files there. Below is the build.properties file and the build.xml file , any help is appreciated.
******build.properties file*********
deploy.folder.suffix =_Deploy
deploy.folder.prod.suffix =_DEPLOY
*************War Task**********************
<war destfile="${selected.env}${deploy.folder.suffix}/${project.name}.war" needxmlfile="false">
<fileset dir="${webroot.dir}">
<include name="**/*.*"/>
</fileset>
<lib dir="${temp.dir}">
</lib>
<classes dir="${build.dir}"/>
</war>
Am not sure how to include the deploy.folder.prod.suffix if I want to select PROD_DEPLOY and create the war in PROD_DEPLOY

.POST_MODULE: Failed to process phase POST_MODULE of deployment

I'm trying to generate a war using an Ant script, but it seems that it won't run on jboss. Anyone knows the diference between this and create a war using Eclipse(File ->Export -> War)?
<war warfile="${docflow4-web-home}/deploy/${nome}.war" webxml="web/WEB-INF/web.xml">
<fileset dir="${docflow4-web-home}/web">
</fileset>
</war>
In Eclipse, optionally, there is a supply WAR export Options, such as whether or not to include Java™ source files in the WAR, and whether to overwrite any existing resources during the export process. Source files are not usually included in a WAR file, because they are not necessary for the server to run the web application. Otherwise, everything is similar to ant script.
In Ant script, your script seems alright, but it is aways nice using the tag , for example: <classes dir="${classes.dir}" /> because it defines a grouping to specify what goes into the WEB-INF\classes folder.
If you are using some third part jar, use the tag lib too, example:
<lib dir="thirdpartyjars">
<exclude name="portlet.jar"/>
</lib>

How do I load optional task sshexec into Ant in a no-configuration manner?

I am using sshexec, which depends on jsch-0.1.48.jar. I can't just put that into the ant/lib directory because other users wishing to use the same build script will have to make a configuration on their machine before they can do so.
What I want to do is to be able to reference jsch-0.1.48.jar as part of the project. Currently, I have it sitting in project/libs directory and I am trying something like:
<property name="lib" location="lib"/>
<taskdef name="sshexec" classname="org.apache.tools.ant.taskdefs.optional.ssh.SSHExec">
<classpath>
<pathelement location="${lib}/jsch-0.1.48.jar"/>
</classpath>
</taskdef>
<target name="sshcmd" description="ssh command">
<sshexec host="X.X.X.X" username="USER" password="PASS" command="cmd" trust="true"/>
</target>
But that's not working:
C:\dev\trunk\project:>ant sshcmd
Buildfile: C:\dev\trunk\project\build.xml
BUILD FAILED
C:\dev\trunk\project\build.xml:275: taskdef A class needed by class org.apache.tools.ant.taskdefs.optional.ssh.SSHExec cannot be found: com/jcraft/jsch/Logger
using the classloader AntClassLoader[C:\dev\trunk\project\lib\jsch-0.1.48.jar]
Total time: 0 seconds
The sshexec task is built into ANT, you do not need to invoke a taskdef operation to use it. All that's missing is the jsch jar.
This can installed using a bootstrap target as follows (from Maven Central):
<target name="bootstrap" description="Install missing jars">
<mkdir dir="${user.home}/.ant/lib"/>
<get src="http://search.maven.org/remotecontent?filepath=com/jcraft/jsch/0.1.48/jsch-0.1.48.jar" dest="${user.home}/.ant/lib/jsch.jar"/>
</target>
This target only needs to be run once, after which the ANT sshexec task will work as expected on the developer's PC.
Update
If you don't want to download jars another mechanism to pass the location of ANT libraries from the command line as follows:
ant -lib /path/to/project/lib/dir ...
For more details on ANT library management, I refer you to the ANT manual
The jsch jar is packaged with my project. So instead of downloading it, I am copying it into the ant library. The build will fail the first time it is run, which is fine for my purposes. It will succeed the second time because the jar will be in the library and would be loaded at start.
<target name="jschset" description="Set the jar">
<copy file="${lib}/jsch-0.1.48.jar" tofile="${ant.home}/lib/jsch-0.1.48.jar"/>
</target>

Java: How to compile a runnable jar from packages?

My Java application has got a package structure similar to this:
src/com/name/app
src/com/name/app/do
src/com/name/utils/db
How would I go about compiling Java files in these directories in to a runnable jar? I need to package required libraries into the generated JAR (jdbc).
I've always done these things in Eclipse but now I need to supply a couple of people with a way to compile the repository without the use of eclipse and I was thinking of making a makefile or a script that invokes the necessary javac pattern.
Take a look at Ant. It's a relatively simple build tool to understand, and provides everything that meets your requirements. Here's a quick skeleton build.xml to get you started:
<project name="my_app_name" default="jar">
<target name="compile">
<javac srcdir="src" destdir="bin">
<classpath>
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="jar">
<jar manifest="manifest_file" destfile="dist/my_app_name.jar">
<fileset dir="bin" />
<fileset dir="lib" />
</jar>
</target>
You need to create a manifest file that will tell the java process which class holds the "main" method. Here is a good place to start learning about manifests.
As an alternate that produces really cluttered Ant build files, you can right click on your Eclipse project and choose "Export...", then choose "General > Ant Buildfiles".
Anyway, that should get you started. You can ask more specific questions as you run into them.
First of all, consider using Ant for such a task.
But since you asked for a manual process, you need to first create a manifest file, like so:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Class-Path: lib/jdbc.jar lib/otherlib.jar
Main-Class: com.name.app.MainClass
Replace the contents of Class-Path with your libs, and Main-Class with the fully qualified name of your main class.
Then, you need to generate the actual .jar, using the following command:
jar cfm app.jar MANIFEST.MF src/com/name/app/*.class src/com/name/app/do/*.class
Where MANIFEST.MF is the previously mentioned manifest file, and the rest is the folders where your .java classes lie in.
Finally, to run your app, you simply execute: java -jar app.jar.
Consider using Ant to do this. http://ant.apache.org/
I recommend that you use Apache Ant to implement your build scripts.
If implemented correctly, Ant is easy to use and the build scripts can be run on any platform that you can install a JDK on. Indeed, with a little bit of work, you can even set up your project so that users don't even need to download / install Ant. (Hint: add the Ant JAR files and a wrapper script to your project distro)

Categories