Missing ant property while configuring worklight database via ant task - ant

I am integrating worklight 6.1 official deployment ant task into gradle build script.
We will utilize below script to run flexible continuous integration process.
def antTaskLibClasspath = "ant-task-lib/worklight-ant-deployer.jar"
def verifyDatabaseArguments(String database){
switch(project.ext[database + 'Type']){
case 'derby':
def dbNameProp = database + 'Name'
def dbDirPathProp = database + 'Dir'
def dbProperties = ['databaseName':project.ext[dbNameProp], 'databaseDir':project.ext[dbDirPathProp] ]
.each { key, value ->
if(!value?.trim()){
def errMsg = "Missing mandatory parameter : ${key}"
println errMsg
throw new StopExecutionException(errMsg)
}
}
project.ext[dbDirPathProp] = Eval.me(""" "${project.ext[dbDirPathProp]}" """).replace("\\", "/")
def dbFile = file(project.ext[dbDirPathProp] + "/${project.ext[dbNameProp]}")
if(!dbFile.exists()){
if(dbFile.mkdirs()){
println "Create folders ${project.ext[dbDirPathProp]} for ${database} derby database"
}else {
def errMsg = "Can not create folders for ${database} derby database"
println errMsg
throw new StopExecutionException(errMsg)
}
}
break
case 'oracle':
break
default:
errMsg = "Unsupported database type"
println errMsg
throw new StopExecutionException(errMsg)
break
}
}
verifyDatabaseArguments("worklightDatabase")
verifyDatabaseArguments("worklightReportsDatabase")
task configure {
description "Configure database for worklight server"
doLast {
ant.typedef(name:'configureDatabase',
classname:'com.ibm.worklight.config.ant.database.ConfigureDatabaseTask',
classpath:antTaskLibClasspath
)
ant.configureDatabase(kind: "Worklight"){
switch(worklightDatabaseType){
case 'derby':
derby(database:worklightDatabaseName, datadir:worklightDatabaseDir)
break
}
}
ant.configureDatabase(kind: "WorklightReports"){
switch(worklightReportsDatabaseType){
case 'derby':
derby(database:worklightReportsDatabaseName, datadir:worklightReportsDatabaseDir)
break
}
}
}
}
And these are properties which were referenced in the script overhead. I've set them in gradle.properties where the build.gradle is located.
worklightDatabaseType=derby
# derby database name
worklightDatabaseName=WRKLGHT
# derby database file directory
worklightDatabaseDir=${System.properties['user.home']}/.derby/ibm
worklightReportsDatabaseType=derby
# derby database name
worklightReportsDatabaseName=WLREPORT
# derby database file directory
worklightReportsDatabaseDir=${System.properties['user.home']}/.derby/ibm
The command line shows ant error message which I can't figure out what's going wrong after I try to configure database with command $ > gradle configure.
Line 75 is the place where I start the first invocation of ant.configureDatabase( ...
Did I miss any mandatory configuration which may not documented in the worklight 6.1?
My jdk version is jdk7_60, gradle version 2.4 without wrapper.
Any suggestion/comment is appreciated, thank you~
By the way, if any consultant from IBM mobilefirst team is watching,
please consider to ask your product team to rename ant task which has dash symbol in it's name.
It seems that Groovy AntBuilder can not load ant element name includes dash symbol,
so we have to unzip worklight ant task jar archives and search for the task class we need in defaults.properties,
then redefine ant task using class name manually in gradle script. That's not convenient.
The build progress of worklight project is complex. We don't like to use ant to setup build automation.
Appended after Peter post his suggestion:
Thank you, Peter.
I have step over the ant library issue after adopting your way.
But it stucks again, and I couldn't interpret the error message.
At first I assume this issue is because worklight 6.1 ant deploy task may not be compatible with gradle,
so I wrote ant build script below to test if my assumption is correct,
which implements the same build logic and variable as previous gradle script does.
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="install">
<loadproperties srcfile="build.properties" />
<taskdef resource="com/worklight/ant/defaults.properties">
<classpath>
<pathelement location="worklight-ant-task-libs/worklight-ant-deployer.jar"/>
</classpath>
</taskdef>
<target name="configure">
<configuredatabase kind="Worklight">
<derby database="${worklightDatabaseName}" datadir="${worklightDatabaseDir}"/>
</configuredatabase>
<configuredatabase kind="WorklightReports">
<derby database="${worklightReportsDatabaseName}" datadir="${worklightReportsDatabaseDir}"/>
</configuredatabase>
</target>
</project>
build.properties
# derby database name
worklightDatabaseName=WRKLGHT
# derby database file directory
worklightDatabaseDir=${user.home}/.derby/ibm
# derby database name
worklightReportsDatabaseName=WLREPORT
# derby database file directory
worklightReportsDatabaseDir=${user.home}/.derby/ibm
And my build progress fails again.
I have upload the ant error message log file to google drive.
Can anyone give me some suggestion to help me figure it out?
Thank you~

I'm going to use a lot of guesswork here, but according to this section of the Ant Manual, ant.library.dir is only set by Ant's Launcher class, which Gradle's Ant integration probably doesn't use.
I suspect that the Worklight Ant task you're using assumes that property is set and throws an exception. You can check this by running
gradle --stacktrace configure
assuming that configure is the task you want to run. This will show you where the exception is being thrown from (and any causes too).
You might be best off adding an unpacked Ant distribution to your source tree (or perhaps just its lib directory) and adding the following entry to a gradle.properties file in the root of your project:
systemProp.ant.library.dir=<path to Ant>/lib
Of course you should replace <path to Ant> with the actual path to an Ant installation.

Related

Gradle doFirst on ant target during execution

I'am trying to override a pre-built age old ant script. For some reasons, I cannot edit the ant file directly.
-----------Psuedo of the gradle tasks--------
ant.importBuild antFileName
defaultTasks = ['greet']
task takeCare << {
println('Switch off security cam')
}
-----------Psuedo of the ant targets---------
<target name="greet">
<echo>Be nice, say hello!</echo>
<antcall target="tackle"/>
</target>
<target name="tackle">
<echo>Brought him down!</echo>
</target>
What I want to achieve is to do a pre-check on gradle before calling ant target tackle. So my output looks like -
Be nice, say hello!
Switch off security cam.
Brought him down!
How do I achieve this?
Did you try this?
tackle.dependsOn takeCare
UPDATE
If you use dependsOn and run
gradle tackle
you will get
:takeCare
Switch off security cam
:tackle
[ant:echo] Brought him down!
But if you use run greet, takeCare will not be executed, because you make an ant call directlly. So greet runs ant target and not gradle task.
You can try to change the tackle target in gradle.
ant.project.getTargets().find{it.value.name == "tackle"}.value
See java docs: Target

Building a Jar of a Frege project using Gradle

I would like to:
use the Frege programming language to write a simple "Hello World" piece of code,
then using the Frege compiler generating the equivalent Java source code,
then building an executable Jar file to run from the command line,
all the previous steps should be "controlled" by Gradle.
I am able to generate the source code (items 1. and 2. from the previous list), but I am not able to specify a "package" structure of the Java source code in output, i.e. I can not see the package Java statement as the first line of code in the generate Java source code. I can specify to the Frege compiler where to put the generated code though (via the -d argument).
I think this is the reason why when building an executable Jar, then launching it, I am seeing similar errors (according to different attempts on Gradle tasks) e.g.: no main manifest attribute.
The Frege source code is saved in a file named HelloFrege.fr, the generated Java source code is in a file named HelloFrege.java (I verified the file contains the expected main method).
Here there's a version of the Gradle "Jar task":
//create a single Jar with all dependencies
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Hello Frege Jar Example',
'Implementation-Version': version,
'Main-Class': 'HelloFrege'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
Here there's another version of the Gradle "Jar" task:
jar {
manifest {
attributes 'Main-Class': 'HelloFrege'
}
}
How can I solve this problem? I would like to avoid to manually add the package reference to the automatically generated Java source code file.
If your module name in Frege is unqualified such as HelloWorld, you will not see the package statement generated in Java. The module name will become the class name and the package will be empty or default package.
If your module name is qualified such as foo.bar.HelloWorld, then foo.bar will be the package name and HelloWorld will be the class name in the generated Java source.
The rule is that the last part of the module name becomes the class name and the qualifiers form the package name in the generated Java source.
I am not sure what gradle can do for you in this regard, but without gradle, the following should at least be possible:
... build your jar, as before ...
jar -uvfe project-all.jar HelloFrege
java -jar project-all.jar # run it
This, of course, is just another way to create a manifest. If this works, then it would be time to investigate why gradle refuses to do it.
Postscriptum: After thinking another minute about what the problem might be, it occurs to me that you may think that the source file name/path has anything to do with the java package name. This is not so in Frege, though it is good practice to have the file path match the package name, and the file base name match the class name (just like in Java). In addition, to remove some confusion, use the module keyword in frege. As explained by Marimuthu, the Java package and class name is derived from the frege module name.
Example:
$ cat Foo.fr
module my.org.Baz where
...
$ java -jar fregec.jar -d bin Foo.fr
This generates the Baz class in package my.org, and creates the class file in bin/my/org/Baz.class
I am posting here my findings so far. The combination of Gradle commands that works for me is the following one (calling it from the command line typing gradle clean generateJavaSrcFromFregeSrc fatJar):
task generateJavaSrcFromFregeSrc {
ant.java(jar:"lib/frege3.21.586-g026e8d7.jar",fork:true) {
arg(value: "-j") // do not run the java compiler
arg(value: "-d")
arg(value: "src/main/java") // the place where to put the generated source code (paired to the -d argument)
arg(value: "src/main/frege/HelloFrege.fr")
}
}
jar {
manifest {
attributes 'Main-Class': 'org.wathever.HelloFrege'
}
}
task fatJar(type: Jar) {
from files(sourceSets.main.output.classesDir)
from files(sourceSets.main.output.resourcesDir)
//from {configurations.compile.collect {zipTree(it)}} // this does not include the autogenerated source code
baseName = project.name + '-fatJar'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
The manifest details need to be specified in the jar block of code, if I specify them in the task fatJar then when running the jar I get no main manifest attribute, in [...].
If I use just the block of code jar with the property from("$projectDir") { include 'lib/**'} to include the Frege jar, then I get errors like java.lang.ClassNotFoundException (I think because the Jar is included as it is and not as a set of .class files).
The folder src/main/java/org/wathever needs to be there before running Gradle (additional info: the Maven convention prefix src/main/java with as a suffix the "Java package" as specified inside the HelloFrege.fr source code: module org.wathever.HelloFrege where)
Some useful details I found:
How to build a fat Jar
Another how to build a fat Jar
An "Hello Frege" example without the Gradle management
The Gradle documentation on how to use the Jar task

How to determine Jenkins build directory from Ant?

I am trying to migrate an Ant script I wrote to build and deploy projects from within the Jenkins framework (instead of triggered from an SVN post-commit hook, which was the expedient way we initially approached things). Everything is great, except I need to stage files for the deploy step and I want to stuff them into the 'build' directory Jenkins creates for the job (and since my build.xml lives in a non-project-specific location, ${basedir} and ${user.dir} do not point to the desired location).
within the Jenkins configuration, I've setup the following:
[Jenkins]
Build Record Root Directory: E:/builds/${ITEM_FULLNAME}
[Job-Specific]
Build File: C:\vc-tools\shadow\build.xml
when running a build, the script is appropriately launched and a job-specific build directory is created, e.g.
E:\builds\Test\2012-08-07_12-51-21
I want to get at this directory from within the build script, but cannot figure out how. some of the things I've tried:
[echo] ${basedir}: C:\vc-tools\shadow
[echo] ${user.dir}: C:\vc-tools
[echo] ${env.workspace}: C:\Program Files (x86)\Jenkins\workspace\Test
[echo] ${env.build_id}: 2012-08-07_12-51-21
[echo] ${jenkins_home}: C:\Program Files (x86)\Jenkins
[echo] ${BuildDir}: E:/builds/${ITEM_FULLNAME}
note: for that last one, I tried passing in:
BuildDir=E:/builds/${ITEM_FULLNAME}
as a property configured from the job within Jenkins (clearly ${} expansion doesn't take place in this context).
according to the documentation, there are no specific environment variables that are set to the full build directory path -- I can fudge it by hardcoding the E:\builds root and tacking on ${env.build_id}, but was hoping there would be an easier way to access the complete path from something Jenkins exposes (either an Ant property and an environment variable) in order to make the script more flexible.
I am using Jenkins version 1.476.
thanks
It's always a good idea for your project to have a copy of it's build logic included alongside the source code. It makes your build more portable across machines.
Having said that it's also quite common to setup build files containing common shared build logic. ANT defines the following tasks to support such activity:
include
import
So a possible solution is to store a simple build.xml file, in the root of your project directory:
<project name="my project" default="build">
<include file="C:\vc-tools\shadow\common-build-1.0.xml" as="common"/>
<target name="build" depends="common.build"/>
</project>
Notes:
It's a good idea to use a revision number in the common build file name. This assists in preserving backward compatibility with other builds using the older logic.
Update
When Jenkins runs a job is sets a number of environment variables.
The following ANT logic will print the location of the Jenkins workspace directory:
<property environment="env"/>
<target name="run">
<echo message="Jenkins workspace: ${env.WORKSPACE}"/>
<echo message="Job directory: ${env.WORKSPACE}../../jobs/${env.JOB_NAME}"/>
<echo message="Build data: ${env.WORKSPACE}../../jobs/${env.JOB_NAME}/build/${env.BUILD_ID}"/>
</target>
These days (Jenkins v. 1.484) 'run' target from answer above should look like this:
<target name="run">
<echo message="Jenkins workspace: ${env.WORKSPACE}"/>
<echo message="Job directory: ${env.WORKSPACE}/../../${env.JOB_NAME}"/>
<echo message="Build data: ${env.WORKSPACE}/../../${env.JOB_NAME}/builds/${env.BUILD_ID}"/>
</target>

Could not create task or type: getProjectData from Ant

I am trying to run an Ant task from within IBM RSA IDE using Ant build ...
I get the following error message:
BUILD FAILED
build.xml:21: Could
not create task or type of type: getProjectData.
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 'getProjectData'.
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
Please neither file bug reports on this problem, nor email the
Ant mailing lists, until all of these causes have been explored,
as this is not an Ant bug.
Here's the Ant buildfile:
<!-- Get property locationName. -->
<target name="config">
<echo message="${ear.project.name}" />
<getProjectData projectName="${ear.project.name}" />
</target>
I am not quite sure what the problem is here because the error message seems not helpful. Any suggestions?
I believe getProjectData is an IBM extension to ant. Like you, I had a similar error, but I was able to get it working after ensuring the Run in the same JRE as the workspace option was enabled (which you can find by right-clicking the build file, run-as, Ant Build..., and selecting the option on the JRE tab).
I discovered the solution on the IBM info center:
The Run in the same JRE as the workspace option enables the classpath
of the workbench to access the additional Ant tasks that perform
operations that are specific to the workbench, such as projectImport,
projectBuild, workspaceBuild, ejbDeploy, or earExport. If your Ant
build script uses any Ant tasks that perform workbench operations,
verify that you selected the Run in the same JRE as the workspace
option; otherwise you might get the following error message in the
Console view:
Problem: failed to create task or type <Ant task> Cause:
The name is undefined.
The build file I used looked like this:
<?xml version="1.0"?>
<project name="Test" default="config" basedir=".">
<target name="config">
<getProjectData Basedir="${basedir}" />
<echo message="getProjectData: projectName=${projectName}
nature=${natureName}
workspace=${workspaceName}
basedir=${basedir}" />
</target>
</project>
And output:
Buildfile: C:\DATA\java\workspace\test-java\build.xml
config:
[getProjectData] Setting projectName=test-java
[getProjectData] Retrieved following Project Data :
[getProjectData] workspaceName=C:\DATA\java\workspace
[getProjectData] natureName=Java
[echo] getProjectData: projectName=test-java
nature=Java
workspace=C:\DATA\java\workspace
basedir=C:\DATA\java\workspace\test-java
BUILD SUCCESSFUL
Total time: 78 milliseconds

Error using Ant to create EJB after upgrading WAS from 6.0 to 7.0

Ant task breaks at the <wsejbdeploy> tag. The exception message is :
[wsejbdeploy] Error executing deployment: java.lang.ClassNotFoundException.
Error is com.ibm.etools.ejbdeploy.batch.plugin.BatchExtension.
[wsejbdeploy] java.lang.ClassNotFoundException:
com.ibm.etools.ejbdeploy.batch.plugin.BatchExtension
[wsejbdeploy] at java.lang.Class.forName(Class.java:136)
In addition to that, ivy.xml reports problems, but when I inspect it, the messages have no sense (screenshot). I suspect the problems could be:
a) Additional files that I have for the build which conflict with the upgraded Ant (in RSA 7.0 i've had Ant 1.6.5 and RSA 8.0 comes with Ant 1.7.1). Additional files are:
required for ant tasks execution
ant-contrib/ant-contrib.jar
antelope-tasks/AntelopeTasks_3.2.10.jar
antform/antform.jar
antform/defaultStyle.txt
antlr/antlr.jar
checkstyle/checkstyle-4.2.jar
checkstyle/checkstyle-optional-4.2.jar
checkstyle/checkstyle-frames-errors.xsl
clover/clover.jar
clover/cenquatasks.jar
clover/clover.license
doccheck/doccheck-modified.jar
ivy/ivy-20060723172807.jar
jakarta-commons/commons-beanutils.jar
resources/checkstyle/checks-source.xml
resources/checkstyle/checksTransformation.xsl
b) Changes in the Ant regarding EJB build which require some settings to be reconfigured.
c) Something else :)
Except the Error, everything else is a stab in the dark so feel free to ask for any additional info.
The problem was in the additional file needed for wsejbdeploy. In the 6.0 version, you copy wsanttasks.jar and wsprofile.jar from the websphere 6.0 installation folder and then reference it like this
<taskdef name="wsejbdeploy" classname="com.ibm.websphere.ant.tasks.WsEjbDeploy"
classpathref="all-libs"/>
where "all-libs" is a reference to the folder where you copied the jars. In websphere 7.0 there is no wsanttasks.jar and you must reference the runtime file, eg
<taskdef name="wsejbdeploy" classname="com.ibm.websphere.ant.tasks.WsEjbDeploy">
<classpath>
<pathelement path="C:/IBM/SDP80/runtimes/base_v7/plugins/com.ibm.ws.runtime.jar" />
</classpath>

Resources