Can't run jenkins job jenkins using ant - jenkins

I have some job in jenkins and i need start it using ant
ant run
Build.xml
<project name="jenkins-facade" default="run" basedir=".">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<description>
simple example build file
</description>
<property name="post.json.encoded" value=""/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<property name="post.json" value='{"parameter": [{"name":"foo_param", "value":"123"}]}'/>
<urlencode name="post.json.encoded" value="${post.json}" />
</target>
<target name="run" depends="init"
description="run jenkins job">
<post to = "http://jankhost:8080/job/Test/buildWithParameters/"
verbose="true" failonerror="true">
<prop name="token" value="1234567"/>
<prop name="json" value="${post.json.encoded}"/>
</post>
</target>
</project>
But when i run it i got 403 error.
I need to run a task on your behalf, and not by anonymous. How can i do this? How to pass through the ant username and token?
scr

I'm surprised/horrified that there's an Ant task to do HTTP POST requests, but looking at its documentation, it appears that there's no way to set HTTP request headers, which you would need for a Jenkins API request like this.
So as an alternative, you could try setting the username and API token in the URL — I don't know whether the Ant task supports this, but you could try http://<user>:<api_token>#jankhost:8080/job/Test/buildWithParameters/
Another possibility, according to any https://<jenkins>/job/<name>/api page may be to use the secret token mechanism:
Another alternative (but deprecated) is to configure the 'Trigger builds remotely' section in the job configuration. Then building or polling can be triggered by including a parameter called token in the request.
You can then include this secret token in the build URL itself. However, as you have security enabled, this will not work out-of-the-box — you will need to install the Build Token Root plugin, and follow the instructions there.

Related

Generating HTML report of testcases results in SoapUI

I have an testsuite of API testing in SOAP UI.
I want an HTML report of testcases results. I am using basic SOAP UI version. Give me a solution apart from SOAP UI Pro.
Yes, it is possible to generate Junit Style HTML reports using SoapUI Opensource Edition as well.
All you need to do is the execution of tests has to be done
use Apache-Ant software, more details on installing and configuring here
write build script
Here is the sample build script(build.xml):
Note that modify the SOAPUI_HOME(or define environment variable), soapui project file path, results directory path according to your environment.
<project basedir="." default="testreport" name="ant script for testing soapui project">
<property environment="env"/>
<property name="soapui.project" value="/app/demo-soapui-project.xml"/>
<property name="results.dir" value="/tmp/results"/>
<property name="reports.dir" value="${results.dir}/Reports"/>
<property name="html.dir" value="${reports.dir}/html"/>
<target name="execute.project">
<exec dir="${env.SOAPUI_HOME}" executable="testrunner.sh">
<arg line="-raj -f ${results.dir} ${soapui.project}" />
</exec>
</target>
<target name="testreport" depends="execute.project">
<mkdir dir="${reports.dir}"/>
<junitreport todir="${reports.dir}">
<fileset dir="${results.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${html.dir}" />
</junitreport>
</target>
</project>
and execute following command (run soapui project and generate report):
ant
There is also simple way (i.e., every thing configured and readily available envrionment) if you are willing to use this docker image.
Short video also available there on how to.

error while building Nutch 1.10 [duplicate]

I have an issue where in I have defined dependancies in ivy.xml on our internal corporate svn. I am able to access this svn site without any proxy task in ant. While my dependencies resides on ibiblio, that’s something outside our corporate, and needs proxy inorder to download something. I am facing problem using ivy here.
I have following in build.xml
<target name="proxy">
<property name="proxy.host" value="xyz.proxy.net"/>
<property name="proxy.port" value="8443"/>
<setproxy proxyhost="${proxy.host}" proxyport="${proxy.port}"/>
</target>
<!-- resolve the dependencies of stratus -->
<target name="resolveTestDependency" depends="testResolve, proxy" description="retrieve test dependencies with ivy">
<ivy:settings file="stratus-ivysettings.xml" />
<ivy:retrieve conf="test" pattern="${jars}/[artifact]-[revision].[ext]"/><!--pattern here specifies where do you want to download lib to?-->
</target>
<target name=" testResolve ">
<ivy:settings file="stratus-ivysettings.xml" />
<ivy:resolve conf="test" file="stratus-ivy.xml"/>
</target>
Following is the excerpt from stratus-ivysettings.xml
<resolvers>
<!-- here you define your file in private machine not on the repo (e.g. jPricer.jar or edgApi.jar)-->
<!-- This we will use a url nd not local file system.. -->
<url name="privateFS">
<ivy pattern="http://xyz.svn.com/ivyRepository/ [organisation]/ivy/ivy.xml"/>
</url>
.
.
.
<url name="public" m2compatible="true">
<artifact pattern="http://www.ibiblio.org/maven2/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]"/>
</url>
.
.
.
So as can be seen here for getting ivy.xml, I don’t need any proxy as its within our own network which cant be accesses when I set proxy. But on the other hand I am using ibiblio as well which is external to our network and works only with proxy. So above build.xml wont work in that case. Can somebody help here.
I don’t need proxy while getting ivy.xml (as if I have proxy, ivy wont be able to find ivy file behind proxy from within the network), and I just need it when my resolver goes to public url.
When using setproxy, use the nonproxyhosts attribute to specify the hosts that the proxy should not be used for (pipe separated). e.g, modify the setproxy task in your example to
<setproxy proxyhost="${proxy.host}"
proxyport="${proxy.port}"
nonproxyhosts="xyz.svn.com"/>
For more details see http://ant.apache.org/manual/Tasks/setproxy.html

What is the preferred practice for enabling Checkstyle for Jenkins?

Hi guys : I noticed that there is an ant task for checkstyle
http://checkstyle.sourceforge.net/anttask.html
I want checkstyle to run in my Ant build, which is on jenkins.
Unfortunately, the instructions are somewhat cryptic - with references to enabling project dependencies , modules, and other ant-specific configurations. I have a massive build file and I'm not really a build engineer - so I want to keep it simple without adding too much bload to the script.
Jenkins has a nice little button which supports displaying the checkstyle results, however, jenkins requires that you run the checkstyle and configure it yourself when you run a build.
What is the simplest way to modify my build.xml and ivy.xml ( i assume i will need to add checkstyle to ivy to get the jar remotely) to enable a basic checkstyle analysis of all the code base when builds are run ?
The sample of how to do it with help of just Ant:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Build" default="build" basedir=".">
<property file="props.properties"/>
<taskdef resource="checkstyletask.properties" classpath="${checkstyle.jar.path}"/>
<target name="build" depends="checkstyle">
<echo>Starting build</echo>
<echo>Build finished</echo>
</target>
<target name="checkstyle">
<echo>Starting checkstyle</echo>
<checkstyle config="rules/sun_checks.xml" failOnViolation="false">
<fileset dir="src" includes="**/*.java"/>
<formatter type="plain"/>
<formatter type="xml" toFile="build/checkstyle_errors.xml"/>
</checkstyle>
<echo>Checkstyle finished</echo>
</target>
</project>
The quote from Checkstyle site:
failOnViolation - Specifies whether the build will continue even if
there are violations. Defaults to "true".
You can download checkstyle-5.4-bin.zip from here.
The distribution package contains sun_checks.xml - checkstyle configuration that checks the sun coding conventions and checkstyle-x.x-all.jar library with task engine.

Download jars from nexus using ant build tool as done automatically in Maven

I have a build.xml(ant based) which requires some jar from nexus to get copied in existing lib folder. i.e when it builds it should copy the jar from nexus with some version defined & then copy in lib & do compilation.
like happen in maven we define the artifact & its version . If changed will automatically download it from maven repo.
how can i do this in ant based builds?
experts pls advice.
I have taken the example listed in this thread one step further and created a macrodef to clean things up a bit for re-use. See below for downloading two artifacts from nexus (one snapshot, one release).
<project>
<target name="get-all">
<mkdir dir="lib" />
<nexus-get
groupId="foo.bar"
artifactId="some-artifact"
version="1.0.28"
repo="releases"
extension="jar"
dest="lib"
/>
<nexus-get
groupId="foo.bar"
artifactId="another-artifact"
version="1.0.0-SNAPSHOT"
repo="snapshots"
extension="jar"
dest="lib"
/>
</target>
<macrodef name="nexus-get">
<attribute name="groupId"/>
<attribute name="artifactId"/>
<attribute name="version"/>
<attribute name="repo"/>
<attribute name="extension"/>
<attribute name="dest"/>
<sequential>
<get src="http://my-nexus:9999/nexus/service/local/artifact/maven/redirect?r=#{repo}&g=#{groupId}&a=#{artifactId}&v=#{version}&e=#{extension}" dest="#{dest}/#{artifactId}.#{extension}" usetimestamp="true" />
</sequential>
</macrodef>
You would probably be interested in Ivy. It is a sub-project of Ant for dependency management. It is perfect for your situation because it can read Maven repositories and provides Ant tasks for downloading the published artifacts, constructing class paths from them, etc. It supports your use case of getting the most recent version of a dependency if you configure it to ask for the "latest.release" revision of the module.
Although there are surely specific ways to combine ant and maven the simplest thing (if you know the nexus URL and your artifact parameters to construct the download URL) would be just to use the ant Get task.
<project name="MyProject" default="resolveDependencies" basedir=".">
<target name="resolveDependencies">
<mkdir dir="lib" />
<get src="http://search.maven.org/remotecontent?filepath=log4j/log4j/1.2.9/log4j-1.2.9.jar" dest="lib/log4j-1.2.9.jar" usetimestamp="true" />
</target>
</project>
Perhaps use the Maven Ant Tasks.
As shown on http://maven.apache.org/ant-tasks/examples/dependencies.html
Can list dependencies in ant, and also do things like copy them
I think the section Using FileSets and the Version Mapper covers your need
You can use is filesetId, which will give you a fileset reference that can be used to copy files into a particular location. For example, to populate WEB-INF/lib with your dependencies you could use the following:
<artifact:dependencies filesetId="dependency.fileset" useScope="runtime">
<dependency groupId="junit" artifactId="junit" version="3.8.2" scope="test"/>
</artifact:dependencies>
<copy todir="${webapp.output}/WEB-INF/lib">
<fileset refid="dependency.fileset" />
<!-- This mapper strips off all leading directory information -->
<mapper type="flatten" />
</copy>

Deploy a app in weblogic only if it is not already present in it using ant wldeploy task

how can i deploy a app in weblogic only if it is not present in it using wldeploy ant task
when i run ant testapp it deploys fresh everytime over existing app as far as i could see in console messages (ie in sysout).
i call this ant target as dependency in some other target, and i want this to run only if app is not already present in weblogic server (to be more efficient)
<target name="testapp" depends="init-wls">
<wldeploy action="deploy" verbose="true" debug="true"
name="testapp" failonerror="false"
...
source="testapp.war"/>
</target>
an interesting question. I'm not sure if wldeploy can do what you want. One approach that might work would be to use the wlconfig ant task. You could use it to get the ApplicationRuntimeMBeans and then query their ApplicationName attributes (again, with wlconfig task) to see if the application is deployed. Not super straightforward but at least you would avoid the application redeployment.
This is just a quick idea off the top of my head so not sure if it is feasible in practice, sorry.... :)
--edit: tried it out, something like this should work, the assumption here is that if we can find the MBean then it is already deployed which should be a valid assumption since these beans live under AppDeployments:
<project name="test" default="deploy">
<property name="domainName" value="ejbTestDomain"/>
<property name="serverName" value="AdminServer"/>
<property name="appName" value="ejbWebEAR"/>
<target name="findApp">
<wlconfig url="t3://localhost:7001" username="weblogic" password="password_for_weblogic">
<query pattern="${domainName}:ServerRuntime=${serverName},Name=${appName},*,Type=ApplicationRuntime" property="app.is.deployed"/>
</wlconfig>
</target>
<target name="deploy" unless="app.is.deployed" depends="findApp">
<echo message="Deploying..."/>
<!-- deploy using wldeploy task -->
</target>
</project>

Resources