Ant creates wrong buildnumber - java

I have a build.xml file that has taks like, release, clean, debug, compress and buildnumber,
All tasks work fine but buildnumber task is not right, I think.
This is how my buildnumber task looks like in the build.xml file:
<target name="buildnumber">
<echo>Creating buildnumber</echo>
<propertyfile file="${basedir}/build/version.properties" >
<entry key="Build" type="int" default="0" operation="+"/>
</propertyfile>
</target>
This task gets only called when I do release:
<target name="release" description="compile for deployment">
<antcall target="clean" />
<antcall target="buildnumber">
<param name="increment" value="Build"/>
</antcall>
If I call this ant from Eclipse it creates a minified js file like this
mygame-0.0.0-min.js
so the buildnumber that is the last number in my case (0.0.0) does not get incremented.
If I call ant from Webstorm I got this minified file:
mygame-0.0.71-min.js.
I mean how is this possible? Buildnumber should not increment because this task is not called by ant because I do no release just "clean" , "debug" and "compress" .
Here is how my version.properties file looks like:
## Version properties
Version=1
Patch=0
Build=0
What is going wrong with my build.xml file? What am I missing?

Related

Can not compile project with ANT

I'm trying to compile project using:
ant compile
I've receive following error in terminal:
taskdef class org.testng.TESTNGNGAntTask cannot be found
using the classloader AntClassLoader[]
Here is my teskdef from build.xml file
<taskdef name="testng" classpath="${test.classpath}"
classname="org.testng.TESTNGNGAntTask" />
Could you include the ant file (by default is named build.xml) ?
Ensure you had installed Apache Ant in your computer: open a command line and execute ant -v. If appears a version message It's works.
If you find the ant file, include a compile target (or if you have one, modify it) like this:
<property name="src" location="src" />
<property name="build" location="build" />
<target name="compile">
<mkdir dir="${build}" />
<javac srcdir="${src}" destdir="${build}" />
</target>
The propierties are just constants.
Good luck!
The name of the class is TestNGAntTask (with one NG and with only the T of Test capitalized), not TESTNGNGAntTask (with two NG's and an entirely capitalized TEST).
Try the following instead:
<taskdef name="testng" classpath="${test.classpath}"
classname="org.testng.TestNGAntTask" />

Jacoco: unable to read execution data file, ant task

Info:
java version: 1.8.0_66
ant version: 1.9.6
What I want to do:
Provide a code coverage report for the server's code that is running on AWS windows 2k12 server.
What I did:
Stop the server completely.
Put jacocoagent.jar into server's bin folder. Note: this is inside Program Files folder
Append -javaagent settings to JAVA_OPTS that is used during server start up.
Start the server.
Run my sample test from my local laptop.
Stop the server completely. This produced a 184kb jacoco.exec.
Copied my build.xml to the same directory where the jacoco.exec is located. C:/path/to/exec/jacoco.exec
Copied jacocoant.jar to C:/path/to/jacocoant.jar
cd into C:/path/to/exec/ and run command "ant"
Result:
Got error unable to read execution data file C:/path/to/exec/jacoco.exec
Build.xml:
<project name="Example" default="rebuild" xmlns:jacoco="antlib:org.jacoco.ant">
<description>
Example Ant build file that demonstrates how a JaCoCo coverage report
can be itegrated into an existing build in three simple steps.
</description>
<property name="result.dir" location="." />
<property name="result.classes.dir" location="${result.dir}/path/to/classes" />
<property name="result.report.dir" location="${result.dir}/report" />
<property name="result.exec.file" location="${result.dir}/jacoco.exec" />
<!-- Step 1: Import JaCoCo Ant tasks -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="../jacocoant.jar" />
</taskdef>
<target name="clean">
<delete dir="${result.report.dir}" />
</target>
<target name="report">
<!-- Step 3: Create coverage report -->
<jacoco:report>
<!-- This task needs the collected execution data and ... -->
<executiondata>
<file file="${result.exec.file}" />
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Code Coverage Report">
<classfiles>
<fileset dir="${result.classes.dir}" >
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}" />
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}" />
<csv destfile="${result.report.dir}/report.csv" />
<xml destfile="${result.report.dir}/report.xml" />
</jacoco:report>
</target>
I am not sure if the problem is with exec file (it is corrupted maybe) or is with my entire setup.
Any help to identify and help solving the problem is appreciated!!!
Thanks!
I got this when using gradle and jaCoCo.
I deleted the build/ directory and reran ./gradlew jacocoTestReport, this time passing.
Similar to #sofia's solution but for gradle:
I removed the version after tool version. Instead of
allprojects {
jacoco {
toolVersion = '0.7.1.201405082137'
}
}
I used the following
allprojects {
jacoco {
toolVersion = '0.7.1'
}
}
I have the same problem recently, it took me long time to figure this out. I hope how I fix this will help you or someone else.
Use ant -verbose report to see the detailed information. I used "ant -verbose report" and got this message: "java.io.IOException: Incompatible version 1007".
As I am using maven, I added the following lines to my pom.xml. Note: version is the same as your javaagent's version.
<dependency> <groupId>org.jacoco</groupId> <artifactId>org.jacoco.ant</artifactId> <version>0.7.4.201502262128</version> </dependency>
In the end, the report is successfully generated.

ANT - How to append compiled classes to classpath dynamically

Setup:
Project A - Pure Java project with no dependencies.
Project B - Pure Java project depends on project A.
Process:
I have a build project script in each of the project root directory and a master script to run them both, in the correct order, first Project A and then Project B. The script output is relative to each project's path.
The script works just fine for Project A, but when it comes to Project B it misses the classes output of Project A.
Using ANT, is there a way to add "dynamically" to the compile classpath the output of a previously compile project?
Or, is there any action I can take except explicitly provide Project B with the classes output path of Project A?
OK, so this took a bunch of hacking.
First use add ant-contrib to your ant you can download it from here.
Then I declared a var instead of a property in my main ant script.
In the compile macro I've passed it to the javac as the classpath.
After the compile is done, I've appended the new classes output folder to the classpath var and called the next compile.
Good luck.
The compile script:
<?xml version="1.0"?>
<project name="PDF Test Client" default="main" basedir=".">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<macrodef name="Compile">
<attribute name="ProjectName" default=" -- set aname to a 'ProjectName' property --" />
<attribute name="ProjectRootDir" default="." />
<attribute name="SourceDir" default="#{ProjectRootDir}/src" />
<attribute name="OutputDir" default="#{ProjectRootDir}/output" />
<attribute name="BuildDir" default="#{OutputDir}/bin" />
<attribute name="ClassesDir" default="#{BuildDir}/classes" />
<sequential>
<echo message="Compiling... #{ProjectName}" />
<mkdir dir="#{ClassesDir}" />
<javac srcdir="#{SourceDir}" destdir="#{ClassesDir}" classpath="${ClassPathFolders}" includeantruntime="true" />
<var name="ClassPathFolders" value="${ClassPathFolders}; #{ClassesDir}" />
<echo message="-" />
</sequential>
</macrodef>
<target name="main">
</target>
</project>

pass an file into antfile when using ANT task

For example, I have two build.xml: build.xml and subbuild.xml. In the build.xml, it will use an ant task to call and run the subbuild.xml like this:
<target name="antCall1" >
<ant antfile="${basedir}/cache/subBuildTarget/subbuild.xml" />
</target>
My question is like this: I have an "test.xml" next to the build.xml. How can I modified the codes above so that I can pass the "test.xml" into the subbuild.xml and do some operations in the subbuild.xml?
Actually, I want to pass the "test.xml" as a of a task in subbuild.xml. I am just confused about how to let the subbuild.xml get accessed to the "test.xml" in the build.xml. Thanks!
Just set a property containing the name of the file in your build.xml before calling the subbuild.xml. An Ant subproject invoked using the ant task automatically inherits all properties set in their parent unless the attribute inheritAll is set to false. Alternatively you can pass the property to the subproject with a nested property element as suggested by CAustin in the comments.
First method:
<target name="antCall1" >
<!-- since the file is in the same directory as the build.xml -->
<property name="input.file" value="test.xml" />
<ant antfile="${basedir}/cache/subBuildTarget/subbuild.xml" />
</target>
Second method:
<target name="antCall1" >
<ant antfile="${basedir}/cache/subBuildTarget/subbuild.xml">
<property name="input.file" value="test.xml" />
</ant>
</target>
In subbuild.xml:
<loadfile property="file.contents" srcFile="${input.file}"/>
<echo message="${file.contents}" />

How do I create a Jar file from my program

I am using eclipse, and I am having difficulty in creating jar files.
So I have codes like getClass().getResource("/imagesfolder/dog.jpg").
How would I create Jar files such that the folder containing my images will also be included. Because error occurs if my Jar file is not in my bin folder with the class files and the imagesfolder.
I tried File>Export>Java>Executable Jar>Save in desktop but when I double click it, it does not start. I tried cmd and it worked but with errors that it can't find imagesfolder.
How will I do a jar file in a separate directory that executes with a double click
I have a class TreeIcon; it uses two images, and I store them in a folder 'images' which is within the package of TreeIcon. For whatever reason, I made the package of TreeIcon spacecheck.images (it could just as easily have been com.mycompany.images). Therefore I used following code to access my images:
expandedIcon = new ImageIcon(TreeIcon.class.getResource("images/Expanded.GIF"));
where the 'images' here is the name of the folder containing just the images, not the one that is part of the package. I.E., in my tree structure for the program source, the images are in a folder named spacecheck.images.images.
Note that there's no slash at the start of my string; this means it references a path relative to that of the class. Putting the slash in front of the spec causes getResource to regard the path as absolute within your jar, so I could also have used the string "/spacecheck/images/images/Expanded.GIF".
ANT is The Way
In eclipse you can use Ant to build your .jar file.
From ant.apache.org
Apache Ant is a Java library and command-line tool whose mission is to
drive processes described in build files as targets and extension
points dependent upon each other. The main known usage of Ant is the
build of Java applications. Ant supplies a number of built-in tasks
allowing to compile, assemble, test and run Java applications. Ant can
also be used effectively to build non Java applications, for instance
C or C++ applications. More generally, Ant can be used to pilot any
type of process which can be described in terms of targets and tasks.
Ant is written in Java. Users of Ant can develop their own "antlibs"
containing Ant tasks and types, and are offered a large number of
ready-made commercial or open-source "antlibs".
Ant is extremely flexible and does not impose coding conventions or
directory layouts to the Java projects which adopt it as a build tool.
Software development projects looking for a solution combining build
tool and dependency management can use Ant in combination with Apache
Ivy.
The Apache Ant project is part of the Apache Software Foundation.
Search with google and you will find many documentation, I will show the basic way to do it.
The Build.xml file
First of all create a new file xml, for example "Build.xml" this will be the file that Ant will read.
The you start writing inside it this:
<?xml version="1.0" encoding="UTF-8"?>
This is the basic line you have always to include.
<project name="NameOfYourProject" default="try_jar" basedir=".">
This (with its closing tag </project> at the end of the file, is the main tag, declaring the name of the project and the first task (default) that will be executed, each task is something Ant will do, and is called "Target", you can create a single target that do everything or various target that do few things each, in this case you can create different "flow-chart" that ant will follow. For example I usually create 3 route for Ant: try_jar that is used just to try if all is working in the jar without doing many things, new_version_jar that is the same of try_jar but will update version number, will sign the jar and some other stuff, and javadoc that creates the javadoc for the project. Il will show you the basic try_jar.
<description>
This buildfile is used to build the jar of the game.
</description>
No need to explanation.
<!-- ================= File and Directory Names ==================== -->
<property name="src" location="${basedir}/src" />
<property name="conf" location="${basedir}/conf" />
<property name="build" location="${basedir}/build" />
<property name="dist" location="${basedir}/dist" />
<property name="app.name" value="MyAppName" />
<property name="dist.jarHome" value="${user.home}/MyApplicationMainFolder" />
<property name="app.version" value="0.2" />
<tstamp />
<property name="jar.name" value="${app.name}_${app.version}.${DSTAMP}.jar" />
<property name="jar.completePath" value="${dist.jarHome}/${jar.name}" />
Here we declare the base properties of the jar, we tell it where the source code is, where the build folder should be and so on. We also choose to put all the app in a folder in the base user home (in mac this is /user/UserName/) and create the name for the file that will include the name (obviously) the version and the time when this jar is created. This avoid duplicated or overriding of files that we may want to keep.
<property name="shared.lib" value="${basedir}/lib" />
Here we must specify the directory in which jar files needed by this plugin to run are stored
<!-- =============== Custom Ant Task Definitions =================== -->
<property name="compile.debug" value="true" />
<property name="compile.deprecation" value="false" />
<property name="compile.optimize" value="true" />
This are configuration params for ant
<!-- ================== External Dependencies ======================= -->
<property name="LWJGL" value="lwjgl.jar" />
<property name="Timer" value="timer.jar" />
<property name="Database" value="hsqldb.jar" />
<property name="Splice" value="jarsplice-0.25.jar" />
Here you must specify your external dependencies (something like easymock or powermock if you want to create a test target.
<!-- ================== Compilation Classpath ======================= -->
<path id="compile.classpath">
<fileset dir="${src}">
<include name="**/*.java" />
<exclude name="**/server/*.java"/>
</fileset>
<fileset dir="${shared.lib}">
<include name="**/*.jar" />
</fileset>
</path>
This is what And (with javac command) will build, you have to specify all the folders you want to build and to add (with <fileset>) any jar that is in the buildpath
<!-- =================== All Target ================================ -->
<!-- ================== Try_jar Target ============================ -->
<target name="try_jar" depends="compile, dist, clean_class_files, run" description="Clean build and dist directories, then compile, create jar and finally run" />
This is our target, as specified in "default" the first line, and will run this. Depends tells Ant what it should do before this target. A you can read it will compile, create the jar (dist), remove the class files, and run it.
<!-- ================== Clean Target ============================== -->
<target name="clean" description="Delete old build and dist directories">
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
This is very clear, before to compile a new version we want to remove any old class file to avoid problems. You may think that this is never called, but pay attention to the dependencies of each target.
<!-- ================== Prepare Target ============================= -->
<target name="prepare" depends="clean">
<mkdir dir="${build}" />
<mkdir dir="${build}/classes" />
<mkdir dir="${build}/lib" />
<copy todir="${build}/lib">
<fileset dir="${shared.lib}" includes="${Timer}, ${LWJGL}, ${Database}" />
</copy>
</target>
This prepare the path, creating new needed folders (like build and build/classes) and adding the external dependencies jars.
<!-- ================== Compile Target =========================== -->
<target name="compile" depends="prepare" description="Compile Java sources">
<mkdir dir="${build}/classes" />
<javac srcdir="${src}" destdir="${build}/classes" encoding="8859_1" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}" source="1.6" target="1.6">
<classpath refid="compile.classpath" />
</javac>
</target>
This is the main compiling target, as you can see it depends on prepare (that depends on clean) so until now we are using all <target> tags.
Ant compile .java files using <javac> tag, that needs to know where the source files are, where to put .class files, the encoding, and the three params we specified earlier.
<!-- =================== Dist Target ================================ -->
<target name="dist" description="Creates Jar archive">
<!-- Create the time stamp -->
<tstamp>
<format property="compile.timestamp" pattern="yyyyMMddHHmm" />
</tstamp>
<!-- update version in manifest -->
<replaceregexp file="${basedir}/manifestClient" match="Implementation-Version: .*" replace="Implementation-Version: ${app.version}.${compile.timestamp}" />
<!-- Create Jar file -->
<jar destfile="${jar.completePath}" manifest="${basedir}/manifest">
<fileset dir="${build}/classes" excludes="**/*.bak" />
<fileset dir="${src}/" excludes="mh/" />
<fileset dir="${shared.lib}/native/macosx" />
<zipfileset src="${shared.lib}/${Timer}" />
<zipfileset src="${shared.lib}/${LWJGL}" />
<zipfileset src="${shared.lib}/${Database}" />
</jar>
</target>
this creates the real jar. <tstamp> and <replaceregexp> are used to update the version in the manifest, you can remove them.
Jar tag will create the .jar file, we specified what files to add in the jar that will be avaible to my classes inside. We have also to specify a manifest that will discuss later.
<!-- =================== Delete .class Target===================== -->
<target name="clean_class_files" description="Delete .class files stored inside build directory and dist folder">
<delete dir="${build}" />
<delete dir="${dist}" />
</target>
This target deletes the two folder used to store .class files (and obviously all the files inside).
<!-- ================== Run Target =============================== -->
<target name="run" description="Run MagicHogwarts">
<java jar="${jar.completePath}" fork="true">
</java>
</target>
The end of our build.xml file, that is the run target that runs the jar.
This is almost what you need to compile and and the correct resources to a jar, if something is not like you are expecting, simply try few times and all will go right.
This is the manifest:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: package.to.class.with.main
Built-by: Gianmarco
Implementation-Vendor: Gianmarco
Implementation-Title: Title
I hope this will be useful to you.
I am editing few things to make the post better, but no contents will be different.

Categories

Resources