Error in running XSLT reports using ANT - java

I have my Eclipse selenium project with few classes in it,I want to view the test reports in XSLT, my project has been complied and ran successfully through ANT, however when I try to run makexsltreports (either through Eclipse or through ANT command prompt am getting the below error:
C:\Automation\Automation\build.xml:89: javax.xml.transform.TransformerFactoryConfigurationError: Provider net.sf.saxon.TransformerFactoryImpl not found
Below is my build.xml:
<path id="test.c">
<fileset dir="${ws.jars}" includes="*.jar"/>
</path>
<target name="makexsltreports">
<mkdir dir="${ws.home}/XSLT_Reports/output"/>
<xslt in="${ng.result}/testng-results.xml" style="src/xslt/testng-results.xsl"
out="${ws.home}/XSLT_Reports/output/index.html" classpathref="test.c" processor="SaxonLiaison">
<param name="testNgXslt.outputDir" expression="${ws.home}/XSLT_Reports/output/"/>
<param name="testNgXslt.showRuntimeTotals" expression="true"/>
</xslt>
</target>

This issue is because of missing JARs, Please add all required JAR files into project and class path and try again

Related

Specification of configuration file (log4j2.xml) for log4j as VM argument just working in eclipse

I'm using the new Log4j2 - Java Logging Framework. If I specify the path to the configuration file in eclipse as VM argument -Dlog4j.configurationFile=/home/../config.xml everything works fine. The configure file is loaded and the logging works as expected, i.e. all logs are written to files. If I additionally use the -Dlog4j.debug, I get the corresponding debug messages from the framework which confirms the correct loading of my configuration file.
However, when I use ANT with a build.xml file the logging framework seems to be initialized with the default configuration - the logs no longer written to the files but to the console. I start the created prog.jar file with the following statement from the console:
java -Dlog4j.configurationFile=/home/../config.xml -Dlog4j.debug -jar prog.jar
The only debug message I get from to Log4j2 framework is the following:
DEBUG StatusLogger org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good!
In the following, parts of the build.xml file which I use to create prog.jar:
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
</target>
<path id="master-classpath">
<fileset dir="${lib}/apache-log4j-2.11.1-bin/">
<include name="log4j-api-2.11.1.jar"/>
<include name="log4j-core-2.11.1.jar"/>
</fileset>
</path>
<target name="compile" depends="init" description="compile the source">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" includeantruntime="true">
<classpath refid="master-classpath"/>
</javac>
</target>
<target name="jar" depends="compile" description="generate the distribution">
<mkdir dir="${dist}"/>
<jar jarfile="${dist}/prog.jar" basedir="${build}" compress="true">
<fileset dir="${src}" includes="**/*.java"/>
<zipgroupfileset dir="${lib}/apache-log4j-2.11.1-bin/" includes="*.jar" />
<manifest>
<attribute name="Main-Class" value="ch.zwas.aks.Runner"/>
</manifest>
</jar>
</target>
I'm confused why it works using Eclipse but does apparently not work when I create the project with ANT and specify the configuration file in the console. Furthermore, I have no idea why there are no more debug messages when I run the jar from the console.
Thanks for your support.
Thanks to the hint of Vikas Sachdeva I was able to solve the problem. The problem was that I apparently packed too many jar files from the log4j library into prog.jar. When I include just the following jar files it works as expected with ANT / console as well.
junit-X.jar
log4j-api-X.ja
log4j-core-X.jar
slf4j-api-X.jar
slf4j-simple-X.jar
The other jars which came with the log4j library I left out. It seems that Eclipse ignores these, but when included with the build.xml into to finally jar then they cause trouble.

Javac inexplicably failing on Hudson

Our application is built my Hudson from Ant scripts. In my latest work I've recently included Maven. Now Hudson's build process fails when it reaches the compilation task without any explanation.
Hudson build:
[echo] Using Maven Repo at ../.maven/repo/
[javac] Compiling 134 source files to C:\Users\administrator1\.hudson\jobs\SAP_RC\workspace\current\classes
BUILD FAILED
C:\Users\administrator1\.hudson\jobs\SAP_RC\workspace\current\build.xml:68: Error running C:\Program Files (x86)\Java\jdk1.8.0_66\bin\javac.exe compiler
Ant script:
<javac classpathref="lib" destdir="${classesDir}" debug="true" debuglevel="lines,vars,source" verbose="true" fork="true" nowarn="true" memoryMaximumSize="320m" srcdir="${srcDir}" includeantruntime="false" />
I specifically included the Maven repo into the classpath, and that's when it fails inexplicably. When I remove the Maven repo the build throws a compilation error complaining that it can't find Maven dependencies. This makes me suspect that I'm hitting some classpath length limit.
I did it this way because I'm not sure what is the correct way to include Maven dependencies into the classpath. If there is a better way please tell me and I'll open a new question specifically for that.
It seems as though my wildcard in my fileset was the culprit.
<path id="lib">
<fileset dir="${env.maven_repo}">
<include name="*/**"/>
</fileset>
</path>
I changed the ** to *.jar and the build process is no longer (inexplicably) failing.
<path id="lib">
<fileset dir="${env.maven_repo}">
<include name="*/*.jar"/>
</fileset>
</path>

Taskdef class com.temp.install.common.action.UserInstallDirRule cannot be found using the classloader AntClassLoader

I have created a jar with the .class files and the dependency libraries which are required for executing the class files using the below jar code
<target name="jar" depends="clean">
<jar destfile="${basedir}/lib/HelloWorld.jar">
<zipgroupfileset dir="${basedir}/lib" includes="*.jar" />
<fileset dir="${basedir}" includes="/com/temp/**" />
<fileset dir="${basedir}" includes="build.properties"/>
<manifest>
<attribute name="Class-Path" value="./HelloWorld.jar"/>
</manifest>
</jar>
</target>
Now i have written other build.xml to run taskdef actions using this jar but the following error occurs when i try to invoke the class files using the taskdef actions even though the class files and their dependencies are present in the same jar.
BUILD FAILED
C:\Users\kh2139\Desktop\New folder\build.xml:4: taskdef class com.temp.install.common.action.UserInstallDirRule cannot be found
using the classloader AntClassLoader[C:\Users\kh2139\Desktop\New folder\HelloWorld.jar]
Attaching my build.xml code below which is used to run taskdef actions on the HelloWorld.jar
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MyTask" basedir="." default="use">
<target name="use" description="Use the Task" >
<taskdef name="helloworld1" classname="com.temp.install.common.action.UserInstallDirRule" classpath="HelloWorld.jar"/>
<helloworld1/>
<taskdef name="helloworld" classname="com.temp.install.common.action.EncryptionGUID" classpath="HelloWorld.jar"/>
<helloworld/>
</target>
</project>
PS: I could able to run the build.xml file successfully without errors when i specify the lib folder in the location where i place HelloWorld.jar and give the classpath to the lib folder in the taskdef actions.
But my issue is i want to use the same jar to contain the dependencies that are used while executing the classes.
The error indicates that Java cannot find UserInstallDirRule.class in HelloWorld.jar. To determine if HelloWorld.jar contains the class, try running the jar.exe program included with the JDK.
Here's an example of running jar.exe in a Windows Command Prompt:
C:\>jar.exe tf "C:\Users\kh2139\Desktop\New folder\HelloWorld.jar"
The output will show whether UserInstallDirRule.class is in the JAR file.

Why combine all jars together?

I have create RESTful web service based on the JAX-RS and used Jersey embedded web server. My ant script compiles code successfully while it gives me error ClassNotFoundException when I run my main class. So after doing research I came up with solution & here it goes java build ant file with external jar files . What I did was created a bundled jar file try to execute that & it works perfectly fine. I want to know the reason behind :
why this solution works ?
Why I should combine all jar file ?
Is it similar to war file which we create following J2EE architecture otherwise war will not be extracted by server ( say TOMCAT ) & in my case jar file for Jersey embedded HTTP server?
EDIT:
Here is my ant build.xml file
<property name="lib.dir" value="${user.dir}/lib"/>
<property name="build.dir" value="${user.dir}/build"/>
<property name="build.lib.dir" value="${build.dir}/lib"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
<property name="src.dir" value="${user.dir}/src/main/java"/>
<property name="main.class" value="com.assignment.ConsoleServer"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="init" depends="clean">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes.dir}"/>
</target>
<target name="copy_jars" depends="init" >
<copy todir="${build.lib.dir}" >
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</copy>
</target>
<target name="compile" depends="copy_jars">
<javac srcdir="${src.dir}" destdir="${build.classes.dir}" classpathref="classpath" includeantruntime="false"/>
</target>
<target name="jar" depends="compile">
<jar destfile="${build.dir}/${ant.project.name}.jar" basedir="${build.classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
<zipgroupfileset dir="${lib.dir}" includes="*.jar"/>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${build.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
Here is my folder structure
P.S. I am not java expert so pardon me if this question is stupid.
Why this solution works?
In your particular case, you probably didn't include all of the necessary dependencies in your deployment in your previous. (It is not clear from your question how you were originally doing the deployment.)
Now you have put all of the application and dependent class files, etc into one JAR file, and presumably you are deploying / running that file. It works because now it has everything that it needs to run ... which it didn't before.
Why I should combine all jar file?
In your case I suspect that it was not strictly necessary. There was probably a way to "deploy" all of the dependencies without combining them into a single JAR file.
However, there is one case where a "uber-jar" has advantages. That is when the JAR is intended to be an "executable" JAR, and you want to be able to distribute / install it as a single file. (And executable JAR
file can refer to external JARs, etc, but the way that you have to do
it is "fragile".)
Is it similar to war file ... ?
Sort of, though a WAR file contains JAR files ... and typically other kinds of resources that the web-container understands.
The solution works because you packed all you service classes and depending libraries in one jar. That jar and everything inside will be in the class path and visible to your execution virtual machines class loader.
If you leave your depending libraries out your Jersey Web server needs to have them on it's class path, then you wouldn't get ClassNotFoundExcpetion
You shouldn't pack web application in single jar. You should crate war file where you dependencies will be placed inside WEB-INF/lib. You would easily then deploy that war on any application server. Switching to Maven instead of Ant can help a lot.
EDIT: After you added more details to description and ant
If you don't want to use fat-jar you can either
modify your antjava task to specify classpath that will reference
all external libraries (basically telling ant how to build
-classpath parameter for java -jar command
even better, modify your javac ant task by making complete Manifest file that specifies Class-Path correctly, take a better
look at the solution (at the bottom) of the answer you linked (java build ant file with external jar files)
For completness reference on Manifest here

Why is my Ant classpath ok in Eclipse but empty on Jenkins?

I am running my Ant build.xml file both locally and on a server running Jenkins.
Locally, inside Eclipse, the build works wonderfully. I set the classpath using:
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar" />
</path>
and then I use the following when I run a target:
<javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false">
<classpath refid="classpath" />
</javac>
When I debug using echo the classpath shows all the available jars which are in my project/lib folder.
However, when I Jenkins fetches this build.xml file and runs it remotely, it prints an empty classpath (using the same echo target).
Why do my class paths differ based on Eclipse versus Jenkins?
Your build environment on you Jenkins server is going to look a bit different from your desktop dev env.
Sanity check: where/how is ${lib.dir} provided with a value?
Usually it is set with a property in the build file - can you maybe post that here also?

Categories

Resources