Stopping only a portion of build script - java

I have run into a problem regarding the build script. Presently I have a main build.xml file that calls each internal build.xml files from a directory. The internal build.xml has 2 stages to execute wherein at the end i get a jar file.
My requirement is that if anything foes wrong in the 1st step of the internal build.xml file, i should not get the jar file, but the main build should continue execution and go to the next internal build.xml file.
In the internal build.xml based on some condition i want to stop that internal build.What changes should i make in my code to stop that particular build?
Thanks in advance.

Set failonerror="false" on your subant tasks you're using to call the other build files.

If you can afford to add ant-contrib to your build, then it is easy. In your main task, do:
<target name="xxx">
<var name="firstStepOK" value="true"/>
<trycatch>
<try>
<!-- call first sub build -->
</try>
<catch>
<var name="firstStepOK" value="false"/>
</catch>
</trycatch>
<!-- call second sub build, pass value of firstStepOK -->
</target>
Unfortunately, ant-contrib has never made it into ant proper, which is a pity. It contains very useful tasks.

Related

include text file in dist folder

Is there any way that in the dist folder (when doing clean and built) I include a text file necessary for the program to work in netbeans? It's because I'm forced to copy it by hand every time I generate the dist folder when building. I want the text file to be added to the root of the project. I am using "apache netbeans ide 14" and using ant. And this is the code of the build.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="pesca1.6" default="default" basedir=".">
<description>Builds, tests, and runs the project pesca1.6.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="pesca1.6-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>
I need the text file, necessary for the program to work, to be automatically added to the root of the dist folder when I build the project.
Text file in dist folder looks very strange, IMHO.
I would recommend you to check nbproject folder, especially build-impl.xml file. This file describes how exactly build process is going.

Custom manifest added multiple times in -post-jar in a common dependency

I have a multi-module native Netbeans Java EE project. In it I have a Java Class Library project that is used by multiple other projects which in turn are packaged into the root .ear project.
I'm adding the "build timestamp" and the "build user" attributes to a custom manifest using the library's build.xml:
<target name="-post-jar">
<jar destfile="${dist.jar}" update="true">
<manifest>
When I "clean and build" the root project, each project that refers the library calls:
<ant antfile="${call.script}" target="jar">
And my -post-jar target is called multiple times. This wouldn't be a problem, but sometimes the second invocation of the <jar> task fails with Unable to rename old file (probably due to Netbeans scanning the files in background, but I can't tell for sure).
There are repeating pairs of Building jar and Updating jar messages in Ant's output. However, if I remove my -post-jar target, the second invocation of the jar target does nothing, because it thinks that the jar is up to date and I see only one Building jar message.
How do I mark the updated jar up to date, so the second invocation of the jar target does nothing?
There's a github repo that demonstrates the problem.
I haven't found a way to not re-generate the manifest every time, but I found a way to make the generated file look the same as the zipped file (and we know that the <jar> task doesn't repack when the contents are the same).
Instead of updating the zipped manifest in -post-jar I now update the source file in -pre-jar. This way the final version of the manifest is zipped and since its contents don't change during build, subsequent <jar> invocations update nothing.
It worth mentioning that before adding the attributes Main-Class, Profile, etc. the build-impl.xml of Netbeans creates an empy manifest template, if the user doesn't provide a valid path in the manifest.file= property. The addition happens after -pre-jar, however the existence of the user-provided manifest is checked much earlier, during the init target and the result is saved to the manifest.available property.
My manifest template is not a static file. It contains the "build timestamp" and the "build user" attributes. Therefore the file doesn't exist during the init target, so I had to add the following line at the beginning of my build.xml:
<property name="manifest.available" value="true"/><!-- It will be available by the time we need it -->
Secondly, manifest.file still has to be set and I set it in project.properties (there's no UI for that setting yet and I wonder how it would behave in the presence of the variable in path)
manifest.file=${build.dir}/manifest.tmp
Next, I overwrite the manifest template in the -pre-jar target:
<tstamp>
<format property="current.time" pattern="HH:mm:ss" locale="de,DE"/>
</tstamp>
<target name="-pre-jar" >
<manifest file="${manifest.file}">
<attribute name="MyApp-built-time" value="${current.time}"/>
<attribute name="MyApp-built-by" value="${user.name}"/>
</manifest>
After that, the new problem became obvious: the timestamp was different for each invocation of
<ant antfile="mylib/build.xml" target="jar">
in the multi-module project and Ant had to repack the jar with the new timestamp in the manifest. I solved this by defining the timestamp property in every project's build.xml. Although the properties are not inherited due to inheritall="false", Netbeans allows for overcoming that:
<property name="transfer.current.time" value="${current.time}"/>
This mechanism is broken in Java EE projects, but the workaround is simple:
<presetdef name="ant">
<!-- workaround transfer.* not working in Java EE projects -->
<ant>
<propertyset>
<propertyref prefix="transfer."/>
<mapper from="transfer.*" to="*" type="glob"/>
</propertyset>
</ant>
</presetdef>

How Can I Call Another Ant Created Testing Suite From Any Target

<target name="Regression_Test" depends="displayEnvironment">
<property name="TestSuite" value="RegressioonTestSuite.xml"/>
<ant antfile="TestNG.xml" dir="${tools.loc}" target="runTestNG" inheritAll="true"/>
<!--<antcall target="buildAndTest" inheritAll="true"/>-->
</target>
In Above Code..
<property name="TestSuite" value="RegressioonTestSuite.xml"/>
Is Not Working, It does not pass to RegreessionTestSuite.xml
Created a file for regression testing Named as RegreessionTestSuite.xml
if i write
this code outside of target then it works but inside any target it won't
as per comments above, it would be helpful if you could post the error/exception you are getting.
In the meanwhile, a few tips to debug it could be:
call ant via the exec task (you can also do an echo of the command you are executing, to double check for typos etc.)
are the target you want to invoke in a different file? is this file 'imported' into your master build file?
Thank You for your support..
I am newer in Ant
Found Solution...Made a silly mistake
<ant antfile="TestNG.xml" dir="${tools.loc}" target="runTestNG" inheritAll="true">
<property name="TestSuite" value="RegressioonTestSuite.xml"/>
</ant>

copy a file in ant based on condition

I have to copy a file if a property is set in ant target, but I always get an error for this code:
<condition property="component.is.x">
<equals arg1="${COMPONENT_ID}" arg2="x" />
</condition>
<target name="copyschemaparamsfile" if="sql.file.present" >
<if>
<equals arg1="${component.is.x}" arg2="true" />
<then>
<copy file="${in.root}/schema/${COMPONENT_ID}-schema.sql"
tofile="${tmp.dir}/${COMPONENT_ID}/x/schema/schema.sql"
failonerror="false" />
</then>
<else>
<copy file="${inf.root}/schema/${COMPONENT_ID}-schema.sql"
tofile="${tmp.dir}/${COMPONENT_ID}/${COMPONENT_ID}/schema/schema.sql" failonerror="false" />
</else>
</if>
</target>
Error is:
Ant could not find the task or a class this task relies upon.
This is common and has a number of causes; the usual
solutions are to read the manual pages then download and
install needed JAR files, or fix the build file:
- You have misspelt 'if'.
Fix: check your spelling.
- The task needs an external JAR file to execute
and this is not found at the right place in the classpath.
Fix: check the documentation for dependencies.
Fix: declare the task.
- The task is an Ant optional task and the JAR file and/or libraries
implementing the functionality were not found at the time you
yourself built your installation of Ant from the Ant sources.
Fix: Look in the ANT_HOME/lib for the 'ant-' JAR corresponding to the
task and make sure it contains more than merely a META-INF/MANIFEST.MF.
If all it contains is the manifest, then rebuild Ant with the needed
libraries present in ${ant.home}/lib/optional/ , or alternatively,
download a pre-built release version from apache.org
- The build file was written for a later version of Ant
Fix: upgrade to at least the latest release version of Ant
- The task is not an Ant core or optional task
and needs to be declared using <taskdef>.
- You are attempting to use a task defined using
<presetdef> or <macrodef> but have spelt wrong or not
defined it at the point of use
Remember that for JAR files to be visible to Ant tasks implemented
in ANT_HOME/lib, the files must be in the same directory or on the
classpath
I am always getting above error when I execute. Can someone please suggest how to check for a parameter and copy from one directory to other within an ant target?
Ant <if/> is part of Ant-Contrib. To use, follow the directions on the Ant-Contrib Tasks installation page:
(1) Copy ant-contrib-0.3.jar to the lib directory of your Ant
installation. If you want to use one of the tasks in your own project,
add the lines
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
to your build file.
(2) Keep ant-contrib-0.3.jar in a separate location. You now have to
tell Ant explicitly where to find it (say in /usr/share/java/lib):
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="/usr/share/java/lib/ant-contrib-0.3.jar"/>
</classpath>
</taskdef>

"Ant all" not working for me

I have got involved in a project. This project uses ant which is not something I am comfortable with. I have checked out the source code and tried running ant on the most outer directory.
Running 'ant' in commando prompt takes 1 sec and I get a BUILD SUCCESFULL message. If I run 'ant all' I get a
BUILD FAILED. Java.io.IOExceptio: Cannot run program "ant": CreateProcess=2, the system cannot find the file specified and then a long stacktrace.
Most of the people on the project runs OS-X while I use Windows XP.
Any help or information is appreciated :)
EDIT:
<target name="-all-submodules">
<subantlight target="all">
<filelist refid="ordered_build_files"/>
</subantlight>
</target>
In another xml file
<macrodef name="subantlight">
<attribute name="target"/>
<element name="files" optional="no" implicit="true" description="Filessets/lists of build files"/>
<sequential>
<apply executable="ant" failonerror="true">
<arg value="-f"/>
<srcfile/>
<arg value="#{target}"/>
<files/>
</apply>
</sequential>
</macrodef>
This is what throws IOException when it hits the line with "apply executeable..".
UPDATED EDIT:
If i set the absolute path like this
<macrodef name="subantlight">
<attribute name="target"/>
<element name="files" optional="no" implicit="true" description="Filessets/lists of build files"/>
<sequential>
<apply executable="MyAbsolutePathHereToAnt.bat" failonerror="true">
<arg value="-f"/>
<srcfile/>
<arg value="#{target}"/>
<files/>
</apply>
</sequential>
</macrodef>
Everything works.
I have set ANT_HOME to my ant directory. I have set my JAVA_HOME to Java JDK directory. In my PATH I have set %ANT_HOME%\bin;%JAVA_HOME%\bin
Calling echo %ANT_HOME% produces the right path.
I can't see what I am during wrong here.
ant with no attributes calls the default target on the build.xml file on the curent path. 'ant all' will call the 'all' target on the same build file.
First - double check the default ant target - is it 'all' or something different? I guess, the default target is not 'all' in your case and the 'all' build includes a build target, that itself calls ant. And this causes the problem.
Hard to tell from here, but scan the build file for an <ant> task inside some <target>. The IO error smells a bit like a violation of user access rights or missing files near/within this <ant> task.
EDIT
the build.xml starts with something like
<project name="Name" default="compile" basedir="/src">
The 'default' attribute names the default target. If the attribute is missing, all top level targets are executed (since ant 1.6) which should include all in your case.
If it works 'for everyone else' then 'everyone else' might have a different environment. Have a look at the environment variable ant references in the script (like 'env.JAVA_HOME' and so on) and compare with the actual environment. Maybe you find a broken path.
Do you have the bin directory of your Ant installation in your PATH? If not, then add it.
It looks like the all target tries to execute Ant (recursively) but can't find it.

Categories

Resources