I am writing a task with gant and I am getting struggled with the task xmlProperty. I have this example xml file:
<root>
<properties>
<foo>bar</foo>
</properties>
</root>
and when I do:
ant.xmlproperty(file:"myFile.xml")
println "${root.properties.foo}"
nothing is printed. Any idea what I am doing wrong?
xmlproperty loads the file in to Ant properties, not Groovy variables, so you need to access them via project.properties on the AntBuilder instance:
println ant.project.properties.'root.properties.foo'
Related
I created a Junit jenkins test case where a in-memory jenkins instance is launched (as we use #Rule jenkinsrule). The code of the test case is available here.
The test case will create a FreeStyleProject (= seed job) which will use as Groovy script DSL a maven.groovy file
But when the test case is executed, the following message is reported during the the job build execution. The message reports ghe consequence of the import/parsing of the mavenJob.groovy file as the job expects that a new job will be created.
Legacy code started this job. No cause information is available
Running as SYSTEM
Building in workspace /var/folders/t2/jwchtqkn5y76hrfrws7dqtqm0000gn/T/j h5344303144116520886/workspace/test0
Processing provided DSL script
ERROR: java.io.IOException: Unable to read /var/folders/t2/jwchtqkn5y76hrfrws7dqtqm0000gn/T/j h5344303144116520886/jobs/mvn-spring-boot-rest-http/config.xml
Finished: FAILURE
And of course no stack trace of the error is stdout or stderr.
How can I investigate the problem and fix it ?
Remark:
If I use the config.xml file and import it in a separate jenkins instance, the job succeeded
config.xml file generated, it looks good (vs same config.xml file created using the UI)
<?xml version='1.1' encoding='UTF-8'?>
<project>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<canRoam>false</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders>
<javaposse.jobdsl.plugin.ExecuteDslScripts>
<scriptText>mavenJob('mvn-spring-boot-rest-http') {
description 'A Maven Job compiling the project Spring Boot Rest HTTP Example'
parameters {
gitParameter {
name 'SELECTED_TAG'
description 'The Git tag to checkout'
type 'PT_TAG'
defaultValue '2.3.4-2'
branch ''
branchFilter 'origin/(.*)'
quickFilterEnabled false
selectedValue 'DEFAULT'
sortMode 'DESCENDING_SMART'
tagFilter '*'
useRepository '.*rest-http-example.git'
listSize '10'
}
}
scm {
git {
remote {
url 'https://github.com/snowdrop/rest-http-example.git'
// branch('$SELECTED_TAG')
branch('2.3.4-2')
}
}
}
rootPOM 'pom.xml'
goals 'clean install'
}</scriptText>
<usingScriptText>true</usingScriptText>
<sandbox>false</sandbox>
<ignoreExisting>false</ignoreExisting>
<ignoreMissingFiles>false</ignoreMissingFiles>
<failOnMissingPlugin>false</failOnMissingPlugin>
<failOnSeedCollision>false</failOnSeedCollision>
<unstableOnDeprecation>false</unstableOnDeprecation>
<removedJobAction>IGNORE</removedJobAction>
<removedViewAction>IGNORE</removedViewAction>
<removedConfigFilesAction>IGNORE</removedConfigFilesAction>
<lookupStrategy>JENKINS_ROOT</lookupStrategy>
</javaposse.jobdsl.plugin.ExecuteDslScripts>
</builders>
<publishers/>
<buildWrappers/>
</project>
Many thanks in advance for your help.
I created a thread discussion here too: https://groups.google.com/g/jenkinsci-users/c/mRSwARFapyA
Charles
The problem was related to many missing dependencies needed to run the test case.
I upgraded the build.gradle file and now that works.
https://github.com/ch007m/jenkins-job-dsl/blob/jenkins-2.271/build.gradle#L53-L72
BTW, the error message reported was not correlated at all to the root cause and How to fix the problem. that should be improved within the code ;-)
I've tried this gradle plugin https://github.com/schmutterer/gradle-openjpa but it complains that it cannot find certain libraries and doesn't support providedCompile which makes this unusable for me anyway.
I've also tried calling ANT tasks, my latest attempt below is throwing:
Caused by: C:\Work_Java\workspace\PaxHoldRelease\jpa_enhance.xml:5: taskdef class org.apache.openjpa.ant.PCEnhancerTask cannot be found
build.gralde
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'ear'
// Java compilier compliance level
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
mavenLocal()
mavenCentral()
}
ant.importBuild 'jpa_enhance.xml'
war.dependsOn enhance
dependencies {
// Ensure ear plugin gets war file
deploy files(war)
providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
compile 'javax.websocket:javax.websocket-api:1.1'
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.16'
compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.5.1'
compile 'org.glassfish:javax.json:1.0.4'
providedCompile 'org.apache.openjpa:openjpa:2.2.2'
providedCompile 'com.sybase:jconn3:6.05'
providedCompile files('libs/sqljdbc4-3.0.jar')
}
jpa_enhance.xml
This is the latest version in a long list of attempts and probably complete rubbish as I just ripped everything out in a fit of desperation :-(
<project>
<target name="enhance">
<taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask"/>
<!-- invoke enhancer on all .java files below the model directory -->
<openjpac>
</openjpac>
<echo message="Enhancing complete!"/>
</target>
</project>
Try this Andrew - I loosely based this gradle on the nice Enhancer script provided on S.O. by another member (for the DataNucleus enhancer).
Note that you will need to modify the entity-files (include/exclude) to point to your specific 'to be/to not be' enhanced Java source files. Further, this approach assumes that classpath derives from your parent build.gradle.
task openJPAEnhance {
description "Enhance JPA model classes using OpenJPA Enhancer"
dependsOn compileJava
doLast {
// define the entity classes
def entityFiles = fileTree(sourceSets.main.output.classesDir).matching {
include 'org/foo/mypkg/entity/*.class'
exclude 'org/foo/mypkg/entity/DoNotEnhance.class'
}
println "Enhancing with OpenJPA, the following files..."
entityFiles.getFiles().each {
println it
}
// define Ant task for Enhancer
ant.taskdef(
name : 'openjpac',
classpath : sourceSets.main.runtimeClasspath.asPath,
classname : 'org.apache.openjpa.ant.PCEnhancerTask'
)
// Run the OpenJPA Enhancer as an Ant task
// - see OpenJPA 'PCEnhancerTask' for supported arguments
// - this invocation of the enhancer adds support for a default-ctor
// - as well as ensuring JPA property use is valid.
ant.openjpac(
classpath: sourceSets.main.runtimeClasspath.asPath,
addDefaultConstructor: true,
enforcePropertyRestrictions: true) {
entityFiles.addToAntBuilder(ant, 'fileset', FileCollection.AntType.FileSet)
}
}
}
I hope this helps, and the individual who wrote that first gradle script did not mind that we re-purposed it (from DataNucleus) to OpenJPA.
I am using Ant as our setup script for our server, and we need to get the fully qualified name of our server. How can I get it using Ant, or is is possible in Ant?
The fully qualified hostname is like: xxx.company.com
<exec executable="hostname" outputproperty="computer.hostname"/>
will work on linux and windows, otherwise use the groovy solution from Marc O'Connor
Also nslookup will work on linux and windows, if you need the fullhostname you have to parse for the entry after Name: in nslookup ServerName output, use :
<groovy>
properties.'hostname' = "hostname".execute().text
//or
properties.'hostname' = InetAddress.getLocalHost().getHostName()
properties.'hostnamefull' = "nslookup ${"hostname".execute().text}".execute().text.find(/Name:\s(.+)/).split(/:\s+/)[1]
</groovy>
<echo>
$${hostname} => ${hostname}
$${hostnamefull} => ${hostnamefull}
</echo>
There is an Ant task called HostInfo that you can use to set properties containing the host name and domain of the current machine.
Alternatively, if you're running on Linux/Unix you could just call out to the hostname command:
<exec executable="hostname" outputproperty="myhostname">
<arg line="-f"/>
</exec>
The fully-qualified host name is then available in ${myhostname}.
EDIT: For a fully platform-independent solution, a custom task something like this (untested) should do the job:
public class GetHost extends Task
{
private String property;
public void setProperty(String property)
{
this.property = property;
}
#Override
public void execute() throws BuildException
{
if (property == null || property.length() == 0)
{
throw new BuildException("Property name must be specified.");
}
else
{
try
{
String hostName = InetAddress.getLocalHost().getHostName();
getProject().setProperty(property, hostName);
}
catch (IOException ex)
{
throw new BuildException(ex);
}
}
}
}
This can be used as follows:
<GetHost property="myhostname" />
Rehashing an answer I did for Maven :-)
Use an embedded groovy script to perform the Java hostname lookup:
<groovy>
properties["hostname"] = InetAddress.getLocalHost().getHostName()
</groovy>
Example
Project is self-documenting:
$ ant -p
Buildfile: /home/mark/tmp/build.xml
This is a demo project answering the followng stackoverflow question:
https://stackoverflow.com/questions/14653733
First install 3rd party dependencies
ant bootstrap
Then run the build
ant
Expect the following output
print-hostname:
[echo] Hostname: ?????
build.xml
<project name="demo" default="print-hostname">
<description>
This is a demo project answering the followng stackoverflow question:
https://stackoverflow.com/questions/14653733
First install 3rd party dependencies
ant bootstrap
Then run the build
ant
Expect the following output
print-hostname:
[echo] Hostname: ?????
</description>
<target name="bootstrap" description="Install 3rd party dependencies">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.0/groovy-all-2.1.0.jar"/>
</target>
<target name="print-hostname" description="Retrieve and print the hostname">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<groovy>
properties["hostname"] = InetAddress.getLocalHost().getHostName()
</groovy>
<echo message="Hostname: ${hostname}"/>
</target>
</project>
Another approach is to write a javascript scriptdef that sets this information in properties.
<scriptdef name="get-hostame" language="javascript">
<attribute name="prefix" /> <![CDATA[
importClass(java.net.InetAddress);
address = InetAddress.getLocalHost();
prefix = attributes.get("prefix");
project.setNewProperty(prefix + ".hostname", address.getHostName());
project.setNewProperty(prefix + ".fqdn", address.getCanonicalHostName());
project.setNewProperty(prefix + ".address", address.getHostAddress());
]]>
</scriptdef>
You can call it like this:
<get-hostame prefix="uwi.host" />
Here is the result:
[echoproperties] uwi.host.address=10.666.666.666
[echoproperties] uwi.host.fqdn=myhost.stackoverflow.com
[echoproperties] uwi.host.hostname=myhos
This is based on some advice I found here:
http://grokbase.com/t/ant/user/051sygfj9b/any-way-to-get-at-the-machine-name
use enviroment
<property name="hostname" value="${env.COMPUTERNAME}"/>
Use javascript which is built-in
<script language="javascript">//<![CDATA[
project.setProperty("hostname", Packages.java.net.InetAddress.getLocalHost().getHostName());
project.setProperty("hostname-full", Packages.java.net.InetAddress.getLocalHost().getCanonicalHostName());
//]]></script>
<echo message="hostname=${hostname}"/>
<echo message="hostname-full=${hostname-full}"/>
I have to integrate jbehave with jenkins. But I don't have idea how to do this. I saw that I have to create a task in Jenkins, but I don't know where I should wire jbehave with this task.
Can somebody help me?
Thanks,
Sarang
So I'm assuming you have JBehave integrated with Maven, correct?
The simple build environment can be set up as follows:
Go to Jenkins and add a new job of type "Build a maven2/3 project"
Configure your project to check out your from whatever source repository you use.
Configure the build phase of the project to run whatever Maven goal you need ("install" will probably work)
Hit save and you have a working project that will execute exactly as it would from a command line.
If you want to see the JBehave test output rendered nicely in Jenkins you should also follow these instructions to configure the Jenkins/XUnit plugin: http://jbehave.org/reference/stable/hudson-plugin.html
You will also need to make sure your project is configured to use the XML Output format in your StoryReporterBuilder to make use of the plugin (not mentioned in the instructions above).
You can visit the following for details:
http://jbehave.org/reference/stable/hudson-plugin.html
Per your comments, you want to specify the stories to run via Jenkins when using the Maven plugin. Here is one way:
Create a subclass of StoryFinder and set it as the storyFinderClass property in your Maven configuration. In the Jenkins commandline launcher, you can pass in stories as a -D system property which can then be read from your StoryFinder.
Commandline
mvn ... -Dcom.sarang.stories="foo.story,bar.story"
Maven
<plugin>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-maven-plugin</artifactId>
<version>[version]</version>
<executions>
<execution>
<id>run-stories-as-embeddables</id>
<phase>integration-test</phase>
<configuration>
...
<systemProperties>
<property>
<name>com.sarang.stories</name>
<value>${com.sarang.stories}</value>
</property>
</systemProperties>
<storyFinderClass>com.sarang.MyStoryFinder</storyFinderClass>
</configuration>
<goals>
<goal>run-stories-as-embeddables</goal>
...
</goals>
</execution>
</executions>
</plugin>
StoryFinder
package com.sarang;
import org.jbehave.core.io.StoryFinder;
import java.util.*;
public class MyStoryFinder extends StoryFinder {
#Override
protected List<String> scan(String basedir, List<String> includes,
List<String> excludes) {
//List<String> defaultStories = super.scan(basedir, includes, excludes);
String myStories = System.getProperty("com.sarang.stories");
return Arrays.asList(myStories.split(","));
}
}
I have a build.gradle file which loads PMD (downloading it from upstream Maven), and then loads an Ant build.xml file which requires PMD:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'pmd:pmd:4.2.5'
}
}
ant.importBuild 'shared-build.xml'
However, the Ant import fails:
taskdef class net.sourceforge.pmd.ant.PMDTask cannot be found
using the classloader AntClassLoader[]
at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:551)
[...]
at org.gradle.api.internal.project.DefaultAntBuilder.importBuild(DefaultAntBuilder.groovy:76)
How can Gradle's ant integration be instructed to make this available?
There's no straighforward way to do it, as Gradle does not offer any API support for this. So you need to hack it some way.
For example, you can do something like this, right before calling ant.importBuild
org.apache.tools.ant.Project.class.classLoader.addURL( file('libs/somelib.jar').toURI().toURL() )
Alternatively you can call the addURL() method with the paths you get through the Gradle's dependency resolution (again, this should be executed before the call to ant.importBuild).
configurations { someconf }
dependencies { someconf "org.eclipse.jdt:ecj:3.6.1" }
def antClassLoader = org.apache.tools.ant.Project.class.classLoader
configurations.someconf.each { File f ->
antClassLoader.addURL(f.toURI().toURL())
}
Of course, another solution would be to have the classpath defined inside your build.xml file so you won't have to do anything from Gradle.
See some input here http://gradle.1045684.n5.nabble.com/How-to-add-to-classpath-for-ant-importBuild-td3268631.html