Ant using antlib with namespace - java

I am using the following demonstration script:
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" basedir="." xmlns:deploy="antlib:net.sf.antcontrib">
<target name="default">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<deploy:if>
<isset property="defaultprop"/>
<then>
<echo message="it's set!"/>
</then>
</deploy:if>
</target>
</project>
When I run this build script (with target default), the error is
build.xml:9: Problem: failed to create task or type antlib:net.sf.antcontrib:if
The pathelement lib/ant-contrib-1.0b3.jar exists, and ant is picking it up. I'm thinking the problem is how I'm using the xmlns. I'm taking this from another example that I have that also doesn't work for me (it works on a particular server, though!), and trying to figure out what the magic sauce is.

Your taskdef where you're adding ant-contrib needs to declare a URI the same as the namespace you're defining and prefixing in the project. Similar to how the taskdef over here works.
<project name="test" basedir="." xmlns:deploy="antlib:net.sf.antcontrib">
<target name="default">
<taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="lib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<deploy:if>
<isset property="defaultprop"/>
<then>
<echo message="it's set!"/>
</then>
</deploy:if>
</target>
</project>

Well, the error has if on the end, and it's talking about line 9. I think it's a problem with the syntax of this tag:
<deploy:if>
I can't find any documentation on a "deploy:if" tag, or even a "deploy" tag. I think there is no 'deploy' task in Ant - you need to make a 'deploy' target.
How about trying this:
<if>
<isset property="defaultprop"/>
<then>
<antcall target="deploy" />
</then>
</if>
As I read it, this will check the isset, then call the "deploy" target if it's set.
Of course, you need to make the 'deploy' target now :)

Related

include maven-ant build libs in eclipse java project

I am struggling with maven-ant build with eclipse.
I did work like below steps.
[GUI] new java project
add build.xml in project top folder
run ant file and SUCCEED!
trying to code, but somehow auto completion does not work.(guessing eclipse can not read maven-ant dependency.path)
So I tried.
add ~/.m2/repository in build path as a External class folder - does not work - It looks weird to me to include whole this folder. My current project, I need little libraries, but it has whole libraries that I uses in other projects.
add builders with build.xml like Want an eclipse java project to run ant build files automatically - does not work neither.
How can I add this maven-ant libraries properly? Thanks for sharing your experiences and answers XD
=========== Extra Information ====================
This is my build.xml.
<?xml version="1.0" encoding="UTF-8"?>
<project name="HibernateEx2" default="db" basedir="."
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<property name="source.root" value="src"/>
<property name="class.root" value="classes"/>
<property name="data.dir" value="data"/>
<artifact:dependencies pathId="dependency.classpath">
<dependency groupId="hsqldb" artifactId="hsqldb" version="1.8.0.10"/>
<dependency groupId="org.hibernate" artifactId="hibernate-core" version="4.3.10.Final">
<exclusion groupId="javax.transaction" artifactId="jta"/>
</dependency>
<!-- 3.2.4.GA - After hibernate4 need upgrade hibernate-tools -->
<dependency groupId="org.hibernate" artifactId="hibernate-tools" version="4.3.1.CR1"/>
<dependency groupId="org.apache.geronimo.specs" artifactId="geronimo-jta_1.1_spec" version="1.1.1"/>
<!-- java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory -->
<dependency groupId="commons-logging" artifactId="commons-logging" version="1.2"/>
<dependency groupId="log4j" artifactId="log4j" version="1.2.17"/>
<!-- java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder -->
<dependency groupId="org.slf4j" artifactId="slf4j-log4j12" version="1.7.12"/>
</artifact:dependencies>
<path id="project.class.path">
<pathelement location="${class.root}"/>
<path refid="dependency.classpath" />
</path>
<!-- Explaining how to use hibernate -->
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="project.class.path"/>
<target name="db" description="Run HSQLDB database management UI against the database file -- use when application is not running">
<java classname="org.hsqldb.util.DatabaseManager" fork="yes">
<classpath refid="project.class.path"/>
<arg value="-driver"/>
<arg value="org.hsqldb.jdbcDriver"/>
<arg value="-url"/>
<arg value="jdbc:hsqldb:${data.dir}/music/"/>
<arg value="-user"/>
<arg value="sa"/>
</java>
</target>
<target name="print-classpath" description="Show the dependency class path">
<property name="class.path" refid="dependency.classpath"/>
<echo>${class.path}</echo>
</target>
<!-- Generate java code -->
<target name="codegen" description="Generate Java source from the OR mapping files">
<hibernatetool destdir="${source.root}">
<configuration configurationfile="${source.root}/hibernate.cfg.xml"/>
<hbm2java/>
</hibernatetool>
</target>
<!-- Creating Sub drectories -->
<target name="prepare" description="Set up build structures">
<mkdir dir="${class.root}"/>
<copy todir="${class.root}">
<fileset dir="${source.root}">
<include name="**/*.properties"/>
<include name="**/*.xml"/>
</fileset>
</copy>
</target>
<!-- Creating Schema for mapping files -->
<target name="schema" depends="prepare" description="Generate DB schema from the OR mappinf files">
<hibernatetool destdir="${source.root}">
<configuration configurationfile="${source.root}/hibernate.cfg.xml"/>
<hbm2ddl drop="yes"/>
</hibernatetool>
</target>
<!-- Compile Java -->
<!-- added includeantruntime="false" to javac, since terminal compile warning -->
<target name="compile" depends="prepare">
<javac srcdir="${source.root}" destdir="${class.root}"
debug="on" optimize="off" deprecation="on" includeantruntime="false">
<classpath refid="project.class.path"/>
</javac>
</target>
<target name="ctest" depends="compile">
<java classname="org.owls.ht.CreateTest" fork="true">
<classpath refid="project.class.path"/>
</java>
</target>
</project>
and This is what my project looks like.
src
-- source codes (includes hibernate.cfg.xml)
classes
-- compiled classes
data
-- logs and queries
build.xml
FYI, I am doing this with a book named [[Harness Hibernate]] written by James Elliot from O'reilly.
Thanks again b
For what you are trying to do, you need the filesetId and versionsId="dependency.versions" in your declaration of:
<artifact:dependencies filesetId="dependency.fileset" versionsId="dependency.versions"
Then add a copy task like so:
<copy todir="${lib.dir}">
<fileset refid="dependency.fileset" />
<mapper classpathref="maven-ant-tasks.classpath"
classname="org.apache.maven.artifact.ant.VersionMapper"
from="${dependency.versions}" to="flatten" />
</copy>
The to="flatten" will flaten your dependencies into a single folder, then you can include that folder on the classpath of eclipse project or wherever you need it.

Refresh doesn't work before creating a war with Ant on Eclipse

On Eclipse I create war files by using ant.
The issue is that in the war file isn't included the right mypropfile.properties.
The file is properly copied, but also if I use <eclipse.refreshLocal resource="projectdir" depth="infinite"/> the old file is included. I have to refresh manually the project.
For Ant I use the "Run in the same JRE as the workspace" option.
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" basedir=".">
<description>
My Project
</description>
<property name="workspace.dir" value="${basedir}/../../"/>
<property name="src" value="${basedir}/../src"/>
<property name="build" value="${basedir}/../build"/>
<property name="build.classes" value="${basedir}/../build/classes"/>
<property name="lib.dir" value="${basedir}/WEB-INF/lib"/>
<property name="web.dir" value="${basedir}/WEB-INF"/>
<property environment="env"/>
<property name="real.dir" value="${basedir}/real"/>
<property name="real2.dir" value="${basedir}/real2"/>
<path id="classpath.server">
<fileset dir="${env.CATALINA_HOME}/lib" includes="*.jar"/>
<pathelement path="${build.classes}"/>
</path>
<path id="classpath.app">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
<target name="refreshResource" if="eclipse.refreshLocal">
<eclipse.refreshLocal resource="projectdir" depth="infinite"/>
</target>
<target name="clean">
<delete dir="${build}/classes"/>
<delete dir="${build}"/>
</target>
<target name="init" depends="clean, refreshResource">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${build}/classes"/>
</target>
<target name="compile" depends="init">
<javac encoding="UTF8" srcdir="${src}" destdir="${build}/classes" includeantruntime="false">
<compilerarg value="-Xlint:unchecked"/>
<classpath>
<path refid="classpath.server.bin"/>
</classpath>
<classpath>
<path refid="classpath.server"/>
</classpath>
<classpath>
<path refid="classpath.app"/>
<fileset dir="${lib.dir}" includes="*.jar"/>
</classpath>
</javac>
</target>
<target name="deleteConfig">
<delete file="${src}/mypropfile.properties"/>
</target>
<target name="real" depends="deleteConfig">
<copy file="${real.dir}/realprop.properties" tofile="${src}/mypropfile.properties"/>
</target>
<target name="real2" depends="deleteConfig">
<copy file="${real2.dir}/real2prop.properties" tofile="${src}/mypropfile.properties"/>
</target>
<target name="war-real" depends="real, compile">
<input message="Warname (without .war):" addproperty="warname"/>
<war destfile="${workspace.dir}/${warname}.war" webxml="${web.dir}/web.xml">
<fileset dir="${basedir}">
<include name="**/*.*"/>
</fileset>
<classes dir="${build.classes}"/>
</war>
</target>
<target name="war-real2" depends="real2, compile">
<input message="Warname (without .war):" addproperty="warname"/>
<war destfile="${workspace.dir}/${warname}.war" webxml="${web.dir}/web.xml">
<fileset dir="${basedir}">
<include name="**/*.*"/>
</fileset>
<classes dir="${build.classes}"/>
</war>
</target>
EDIT
The target clean was wrong, so I've corrected it, but now build failed with error
BUILD FAILED ... Reference classpath.server.bin not found.
Ant doesn't care if Eclipse has refreshed the file or not. eclipse.refreshLocal is only relevant for editors and compilers inside of the IDE.
When you run the Ant build.xml, Ant copies the file in question in the real target into the source folder and compile copies it into ${build}/classes (at least it should do that). So before you create the WAR, you must make sure the compile step has done its work (i.e. look into each file to make sure that a change is visible in each copy).
What worries my is that you use different ways to access the classes:
${build}/classes
${build.classes}
${basedir}/../build/classes
So the first step should be to define a single way to locate the folder and then use this pattern everywhere.
If that doesn't solve your problem, you need to make sure Ant notices that the file has changed. Old filesystems like FAT support only timestamps which have second resolution. If you use an USB stick for your sources, it's possible to change the file and run Ant so fast that Ant thinks the file hasn't changed.
Lastly, you need to check your classpath. If one of the JAR dependencies also contains a file called mypropfile.properties, then Java resource loading can find either version.
This and other problems made me use a different solution to configure WAR files: I pass a system property with the absolute path of the config file. That way, the WAR file doesn't change when the config changes and I have full control over which config file is loaded.

How do I run a pitest ant script

I'm trying to create an ant script to run pitest to be able to automate my mutation testing. I am getting the error:
Could not find or load main class org.pitest.mutationtest.commandline.MutationCoverageReport
This is my MutationTest.xml ant script
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="mutationCoverage" name="PhoneBook">
<property name="ECLIPSE_HOME" value="C:/Program Files/eclipse/"/>
<path id="JUnit 4.libraryclasspath">
<pathelement location="${ECLIPSE_HOME}plugins/org.junit_4.11.0.v201303080030/junit.jar"/>
<pathelement location="${ECLIPSE_HOME}plugins/org.hamcrest.core_1.3.0.v201303031735.jar"/>
</path>
<path id="PhoneBook.classpath">
<pathelement location="bin"/>
<path refid="JUnit 4.libraryclasspath"/>
</path>
<path id="pit.path">
<pathelement location="lib/pitest-1.1.4.jar" />
<pathelement location="lib/pitest-ant-1.1.4.jar" />
</path>
<taskdef name="pitest" classname="org.pitest.ant.PitestTask" classpathref="pit.path" />
<target name="mutationCoverage">
<pitest
pitClasspath="PhoneBook.path"
classPath="PhoneBook.path"
targetClasses="pbook.*"
targetTests="pbook.*"
reportDir="MutationReports"
sourceDir="src"/>
</target>
</project>
What is causing this error, and how can I fix it?
Edit: I changed pitClasspath="PhoneBook.path" to pitClasspath="pit.path" and now I have a new error:
[pitest] Exception in thread "main" org.pitest.util.PitError: Unable to load class content for org.pitest.boot.HotSwapAgent
[pitest] Please copy and paste the information and the complete stacktrace below when reporting an issue
[pitest] VM : Java HotSpot(TM) 64-Bit Server VM
[pitest] Vendor : Oracle Corporation
[pitest] Version : 25.25-b02
[pitest] Uptime : 370
[pitest] Input ->
[pitest] BootClassPathSupported : true
[pitest] at org.pitest.mutationtest.tooling.JarCreatingJarFinder.classBytes(JarCreatingJarFinder.java:124)
[pitest] at org.pitest.mutationtest.tooling.JarCreatingJarFinder.addClass(JarCreatingJarFinder.java:113)
[pitest] at org.pitest.mutationtest.tooling.JarCreatingJarFinder.createJarFromClassPathResources(JarCreatingJarFinder.java:98)
[pitest] at org.pitest.mutationtest.tooling.JarCreatingJarFinder.createJar(JarCreatingJarFinder.java:74)
[pitest] at org.pitest.mutationtest.tooling.JarCreatingJarFinder.getJarLocation(JarCreatingJarFinder.java:63)
[pitest] at org.pitest.mutationtest.tooling.EntryPoint.execute(EntryPoint.java:70)
[pitest] at org.pitest.mutationtest.tooling.EntryPoint.execute(EntryPoint.java:43)
[pitest] at org.pitest.mutationtest.commandline.MutationCoverageReport.runReport(MutationCoverageReport.java:72)
[pitest] at org.pitest.mutationtest.commandline.MutationCoverageReport.main(MutationCoverageReport.java:43)
I don't know if that is better or worse, but hopefully it will be helpful in finding the problem.
A working example of a pitest ant build is provided at
https://github.com/hcoles/pitest-ant-example
I suggest you start with this and edit it until you have a working build for your codebase.
One difference I can see is that you have not included junit on the pitest path.
Your build looks a little strange as it seems to be tied to eclipse. If you are running from the IDE why not use the eclipse plugin?
Also, if you are not tied to Ant, you might want to consider maven as an alternative.
I believe much of your problem is that you're trying to use the Eclipse generated build.xml file, which doesn't contain the mutation testing target, and the target which you've added to remedy this has some errors.
I would suggest starting with the project here and attempting to understand how it works, and then changing their build.xml file to fit your needs.
However if this doesn't work, judging from your other question, the following build.xml should work if:
You divide your files into two source directories "src" and "test"
Both src and test folders contain the package "pbook"
You change the name of your tests to end in "Test" rather than begin in it
<?xml version="1.0" encoding="UTF-8"?>
<project name="Phonebook">
<property name="classOutputDir" value="build" />
<!-- classpath for pitest and any plugins -->
<path id="pitest.path">
<!-- must currently include the test library on the tool classpath. this will be fixed in a future version-->
<pathelement location="lib/junit-4.9.jar" />
<pathelement location="lib/pitest-0.33.jar" />
<pathelement location="lib/pitest-ant-0.33.jar" />
</path>
<taskdef name="pitest" classname="org.pitest.ant.PitestTask" classpathref="pitest.path" />
<target name="clean">
<delete dir="${classOutputDir}" />
</target>
<target name="compile" depends="clean">
<mkdir dir="${classOutputDir}/classes" />
<!-- Essential that line numbers and filenames are included in order for PIT to work -->
<javac srcdir="src" includeantruntime="false" debug="true" debuglevel="source,lines" destdir="${classOutputDir}/classes" />
</target>
<!-- classpath for compiling and testing the code. Note it does not include pitest and it's dependencies -->
<path id="test.path">
<pathelement location="${classOutputDir}/classes" />
<pathelement location="${classOutputDir}/test-classes" />
<pathelement location="lib/junit-4.9.jar" />
</path>
<target name="test" depends="compile">
<mkdir dir="${classOutputDir}/test-result" />
<mkdir dir="${classOutputDir}/test-classes" />
<javac includeantruntime="false" srcdir="test" destdir="${classOutputDir}/test-classes">
<classpath refid="test.path" />
</javac>
<junit>
<classpath refid="test.path" />
<batchtest todir="${classOutputDir}/test-result">
<!-- set test classes -->
<fileset dir="test">
<include name="**/*Test.java" />
</fileset>
<formatter type="xml" />
</batchtest>
</junit>
<junitreport todir="${classOutputDir}/test-result">
<fileset dir="${classOutputDir}/test-result">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${classOutputDir}/test-result/report" />
</junitreport>
</target>
<!-- run pitest. note that the filters for tests and classes refer to package/class names, not source file named -->
<target name="pit" depends="test">
<path id="mutation.path">
<path refid="pitest.path"/>
<path refid="test.path"/>
</path>
<pitest pitClasspath="pitest.path" threads="2" classPath="mutation.path" targetTests="pbook.*" targetClasses="pbook.*" reportDir="pitReports" sourceDir="src" />
</target>
</project>

Getting ant to run java file

I am having trouble just creating something simple, and it does not seem that any website is clear on how to do this, and honestly I think it should be simple.
I have a bunch of java files for a project. I want to compile all of them, and then run each file with specific arguments.
Basically I want the order of operations to be something like this
javac prob1.java
javac prob2.java
java prob1 parameter
java prob2 parameter
But I want that in ant (build.xml).
I can do the compile part just fine with
<project default="compile">
<target name="compile">
<javac srcdir="." />
</target>
</project>
I just can not get it to run say prob1 with an argument. I imagine this is extremely easy, but every solution I have found, does not seem to work. Also note prob1.class and prob2.class are in the same directory.
This should work:
<target name="run">
<java classname="prob1">
<classpath>
<pathelement location="."/>
</classpath>
<arg value="parameter" />
</java>
<java classname="prob2">
<classpath>
<pathelement location="."/>
</classpath>
<arg value="parameter" />
</java>
</target>

JSP compilation and Weblogic 10.3.4

We are in the process of upgrading from BEA Weblogic and ALSB to Oracle Weblogic and OSB 10.3.4. One of the things that are failing is our ant task for compiling jsp pages for our WAR.
This is the part of the ant build file that is failing now:
<!-- Compile the JSP files -->
<path id="workshop.classpath">
<fileset dir="${env.WL_HOME}/workshop/lib">
<include name="*.jar"/>
</fileset>
<pathelement location="${env.ORACLE_HOME}/tools/eclipse_pkgs/1.1/pkgs/eclipse/plugins/com.bea.workshop.wls.ant_1.0.20.200802230117/workshop-wls-antlib.jar"/>
</path>
<taskdef
name="jspc"
classname="com.bea.workshop.wls.antlib.tasks.JspcTask" classpathref="workshop.classpath"/>
<path id="jspc.classpath">
<path refid="java.classpath"/>
<pathelement location="${env.ORACLE_HOME}/jdk150_11/lib/tools.jar" />
<pathelement location="${env.WL_HOME}/server/lib/weblogic.jar" />
</path>
<jspc source="#{war.staging.dir}" classpathref="jspc.classpath" failonerror="true" />
It cannot resolve the jar file with JspcTask since Workshop does not exist in 10.3.4. So the question is, how should this ant script be changed to work under 10.3.4?
The taskdef with the classname below works with WLS 10.3.3
<taskdef name="jspc"
classname="weblogic.ant.taskdefs.j2ee.Jspc" classpath="{ORACLE_MIDDLEWARE_HOME}\wlserver_10.3\server\lib\weblogic.jar">
</taskdef>
Check your weblogic.jar with winzip/winrar to see if the above classname exists in 10.3.4, and it should be fine.
Update:
You're getting the NullPointer because jspc does not allow a directory in the source attribute. It should be srcdir as per the docs, in fact I dont see a source attribute at all.
The jspc task is deprecated, so I think it's better you use the weblogic.appc like below
<target name="compileapp">
<java classname="weblogic.appc" fork="yes">
<arg line="-webapp ${src.gui}" />
<arg value="-compiler javac" />
<arg line="-classpath D:\Oracle\Middleware_Jdev11_1_1_4\wlserver_10.3\server\lib\weblogic.jar" />
<arg value="-depend" />
<arg value="-nowarn" />
</java>
</target>
Choose the attributes you want from this list, I havent tried this myself yet.

Categories

Resources