Evaluating Sonarqube (Version 5.4), we want to migrate our current workflow using another Audit tool, which works like that :
The current version that runs in production is our reference version.
A new development version is checked out from GIT, a diff process calculates the new and modified files vs.
the reference version and starts the audit for these files.
There's also a slightly different handling of legacy code (components that already existed in 2012) and
new components (after 2012).
The build breaks if :
Blocker issues in changed files (those files already existing in 2012) from legacy components
Blocker or critical issues in new files (files created after 2012) from legacy and new components
How to implement that in Sonarqube ?
Tried two things already :
1.) Set property sonar.timemachine.period1 to the production/reference version in Ant script before starting Sonar task => didn't work,
it's always 'since previous version'
2.) Define two different projects in Sonarqube, one for the production versions and one for the new dev versions.
Then programmatically use the feature known from Sonarqube Web UI More / Compare Projects and get the diff
for Blocker and Critcal issues.
Problem : f.e. i'll get no diff for Critical issues if i have fixed 200 Critical issues that already existed in my production
reference, but introduced 200 new issues in the development version.
The Compare Projects feature has no metric for new or old issues, it's just counting issues for the compared projects.
The sonar.timemachine.period1 property has to be set via REST call (documentation here), before calling the Sonar task - if defined with Ant property task, it isn't transferred to Sonarqube Server.
Works like that, created a macrodef for reuse :
<project xmlns:sonar="antlib:org.sonar.ant">
<!-- Import Groovy -->
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<!-- Import Sonar -->
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml"/>
<property name="sonar.language" value="java" />
<property name="sonar.host.url" value="http://localhost:9000" />
<property name="sonar.projectKey" value="com.whatever:foobar" />
<property name="sonar.projectName" value="foobar" />
<property name="sonar.projectVersion" value="v_1_2_3_xy" />
<property name="sonar.scm.provider" value="git" />
<property name="sonar.sources" value="src"/>
<property name="sonar.java.binaries" value="bin"/>
<property name="sonar.java.libraries" value=" ... " />
<macrodef name="sonarsetproperty">
<attribute name="host" default="${sonar.host.url}"/>
<attribute name="property" />
<attribute name="projectid" default="${sonar.projectKey}"/>
<attribute name="value"/>
<attribute name="usertoken" default="6e44ba2b9c0f47118d502fbf1d6d36fcfd5f7eb2"/>
<attribute name="verbose" default="false"/>
<sequential>
<groovy>
<![CDATA[
println """
================ Sonar SetProperty ================
SonarHost => #{host}
SonarProperty => #{property}
Value => #{value}
================ Sonar SetProperty ================
"""
s = '#{host}/api/properties?id=#{property}&value=#{value}&resource=#{projectid}'
raw = '#{usertoken}:'
bauth = 'Basic ' + javax.xml.bind.DatatypeConverter.printBase64Binary(raw.getBytes())
url = new URL(s)
HttpURLConnection conn = url.openConnection()
conn.setRequestMethod('POST')
conn.setRequestProperty("Authorization", bauth)
conn.connect()
if(conn.responseCode == 200 || conn.responseCode == 201) {
response = conn.content.text
if(#{verbose}) println '=== Response ===\n' + response + '\n=== Response ==='
} else {
ant.fail(message: "Error Connecting to ${url}, Errorcode ${conn.responseCode}")
}
]]>
</groovy>
</sequential>
</macrodef>
<!-- user needs to be admin -->
<sonarsetproperty property="sonar.timemachine.period1" value="v_1_0_0_xy"/>
<!-- Execute Sonar -->
<sonar:sonar />
</project>
Somehow i expected to see the sonar.timemachine.period1 in
Sonarqube Server Web UI / Administration /General Settings / Differential Views after the REST call but that's not the case.
Note => Instead of using username:password for BasicAuth, simply create a usertoken athttp://sonarhost/account/security and use usertoken: instead - means usertoken as userid with separator ':' and a blank password.
Related
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.
I am using Input tasks to collect specific property values and I want to concatenate those into one property value that references my properties file.
I can generate the format of the property but at runtime it is treated as a string and not a property reference.
Example properties file:
# build.properties
# Some Server Credentials
west.1.server = TaPwxOsa
west.2.server = DQmCIizF
east.1.server = ZCTgqq9A
Example build file:
<property file="build.properties"/>
<target name="login">
<input message="Enter Location:" addproperty="loc" />
<input message="Enter Sandbox:" addproperty="box" />
<property name="token" value="\$\{${loc}.${box}.server}" />
<echo message="${token}"/>
</target>
When I call login and provide "west" and "1" for the input values, echo will print ${west.1.server} but it will not retrieve the property value from the properties file.
If I hardcode the property value in the message:
<echo message="${west.1.server}"/>
then Ant will dutifully retrieve the string from the properties file.
How can I get Ant to accept the dynamically generated property value and treat it as a property to be retrieved from the properties file?
The props antlib provides support for this but as far as I know there's no binary release available yet so you have to build it from source.
An alternative approach would be to use a macrodef:
<macrodef name="setToken">
<attribute name="loc"/>
<attribute name="box"/>
<sequential>
<property name="token" value="${#{loc}.#{box}.server}" />
</sequential>
</macrodef>
<setToken loc="${loc}" box="${box}"/>
Additional example using the Props antlib.
Needs Ant >= 1.8.0 (works fine with latest Ant version 1.9.4)
and Props antlib binaries.
The current build.xml in official Props antlib GIT Repository (or here) doesn't work out of the box :
BUILD FAILED
Target "compile" does not exist in the project "props".
Get the sources of props antlib and unpack in filesystem.
Get the sources of antlibs-common and unpack contents to ../ant-antlibs-props-master/common
Run ant antlib for building the jar :
[jar] Building jar: c:\area51\ant-antlibs-props-master\build\lib\ant-props-1.0Alpha.jar
Otherwise get the binaries from MVNRepository or here
The examples in ../antunit are quite helpful.
For nested properties look in nested-test.xml
Put the ant-props.jar on ant classpath.
<project xmlns:props="antlib:org.apache.ant.props">
<!-- Activate Props antlib -->
<propertyhelper>
<props:nested/>
</propertyhelper>
<property file="build.properties"/>
<input message="Enter Location:" addproperty="loc" />
<input message="Enter Sandbox:" addproperty="box" />
<property name="token" value="${${loc}.${box}.server}"/>
<echo message="${token}"/>
</project>
output :
Buildfile: c:\area51\ant\tryme.xml
[input] Enter Location:
west
[input] Enter Sandbox:
1
[echo] TaPwxOsa
BUILD SUCCESSFUL
Total time: 4 seconds
Solution is :
Consider problem is this, where you want to achieve this :
<property name="prop" value="${${anotherprop}}"/> (double expanding the property)?
You can use javascript:
<script language="javascript">
propname = project.getProperty("anotherprop");
project.setNewProperty("prop", propname);
</script>
I gave it a try and this is working for me.
I am trting to build Blackberry project and generate the cod file.
My environment is Windows 7 and the apache-ant version is 1.8.2.
I have encountered two problems and they all lead to failure.
The command I used to build is "ant -Dbuild_env=prod"
Below is part of the code in the build.xml file.
<property file="build.properties" />
<property environment="SystemVariable"/>
<!--<property name="jde.home" value="${SystemVariable.JDE_HOME}"/> -->
<property name="jde.home" value="E:\eclipse-blackberry\plugins\net.rim.ejde.componentpack5.0.0_5.0.0.25\components"/>
<property name="bb_sign_pwd" value="${SystemVariable.BB_SIGN_PWD}"/>
<property name="jde.sigtooljar" value="${jde.home}/bin/SignatureTool.jar"/>
First problem:
If I use the code
<property name="jde.home" value="${SystemVariable.JDE_HOME}"/>
to run the building,
the error(error 1) is
"jde home must be a directory",
but if I use the code
<property name="jde.home" value="E:\eclipse-blackberry\plugins\net.rim.ejde.componentpack5.0.0_5.0.0.25\components"/>
it works but still build failed.
I think may be the ant cannot find the enviromental varialbe although I set the enviromental varialbe "JDE_HOME" in the Windows to "E:\eclipse-blackberry\plugins\net.rim.ejde.componentpack5.0.0_5.0.0.25\components".
Second problem:
Use the code
<property name="jde.home" value="E:\eclipse-blackberry\plugins\net.rim.ejde.componentpack5.0.0_5.0.0.25\components"/>
instead but another error(error 2) is
"XXX\XXX\XXX\build.xml:49:Java returned: -1"
Line 48 to 50 in the build.xml is like this
48:<target name="build" description="Builds Project" depends="init"/>
49:<rapc jdehome="${jde.home}" jdkhome="${SystemVariable.JAVA_HOME}" destdir="${output.dir}" output="${app.name}_${build_env}" quiet="false" generatesourcelist="true"/>
50:<jdp title="${app.title}" type="cldc" vendor="XXX Inc." version="${app.version}" description="XXX Application" icon="${icon.path}" focusicon="true" startuptier="7" ribbonposition="0"/>
Is there any one can help and tell me why there is error 1 and error 2?
Thanks.
I have an ant script to manage out build process. For WiX I need to produce a new guid when we produce a new version of the installer. Anyone have any idea how to do this in ANT? Any answer that uses built-in tasks would be preferable. But if I have to add another file, that's fine.
I'd use a scriptdef task to define simple javascript task that wraps the Java UUID class, something like this:
<scriptdef name="generateguid" language="javascript">
<attribute name="property" />
<![CDATA[
importClass( java.util.UUID );
project.setProperty( attributes.get( "property" ), UUID.randomUUID() );
]]>
</scriptdef>
<generateguid property="guid1" />
<echo message="${guid1}" />
Result:
[echo] 42dada5a-3c5d-4ace-9315-3df416b31084
If you have a reasonably up-to-date Ant install, this should work out of the box.
If you are using (or would like to use) groovy this will work nicely.
<project default="main" basedir=".">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"
classpath="lib/groovy-all-2.1.5.jar" />
<target name="main">
<groovy>
//generate uuid and place it in ants properties map
def myguid1 = UUID.randomUUID()
properties['guid1'] = myguid1
println "uuid " + properties['guid1']
</groovy>
<!--use the uuid from ant -->
<echo message="uuid ${guid1}" />
</target>
</project>
Output
Buildfile: C:\dev\anttest\build.xml
main:
[groovy] uuid d9b4a35e-4a75-454c-9f15-16b4b83bc6d0
[echo] uuid d9b4a35e-4a75-454c-9f15-16b4b83bc6d0
BUILD SUCCESSFUL
Using groovy 2.1.5 and ant 1.8
I have a number of file/executable locations that are likely to be different depending on which computer I am running them on, and I would like to abstract these out through ant properties somehow. What's the best way to do this? Is there a system-wide ant setup script that gets called? Or can I make such a script?
In addition to Vladimir's solution you might have a default properties file for each of the OS or other you might deploy your build system on. Use the ${os.name} (and other Java system properties) to set up a path. For example
<property file="build-${os.name}.properties">
These files can be maintained and checked in into your version control system as well.
I use the more or less standard build.properties and build-local.properties files.
The first contains default values, common to all environments, the second only the exceptions. The first one is checked into subversion while the other is not.
EDIT : copy/pasting Akr's excellent idea
In addition you might have a default properties file for each of the OS or other you might deploy your build system on. These files can be checked in into your version control system as well.
The Ant script would then include all the files as follow (remember: in Ant the first definition wins):
<property file="build-local.properties"/>
<property file="build.properties"/>
<property file="build-${os.name}.properties">
Setup an ant build file called properties.xml, in which you should define the properties that you want to customize.
Here is properties.xml boilerplate I am using for my projects ( I've adapted it from one of the books on Ant ):
<?xml version="1.0" encoding="UTF-8"?>
<project
name="workspace-properties"
>
<dirname
property="workspace-properties.basedir"
file="${ant.file.workspace-properties}"
/>
<!--
==========================================================
Load Environment Variables
==========================================================
-->
<!-- #Load environment variables -->
<property environment="env" />
<!-- this is here to deal with the fact that an IntelliJ IDEA build
has no ant home
-->
<property
name="ant.home"
value="${env.ANT_HOME}"
/>
<!-- get Unix hostname, and set to Windows comparable name -->
<!-- #Trick to get host name x-platform -->
<property
name="env.COMPUTERNAME"
value="${env.HOSTNAME}"
/>
<!--
==========================================================
Load property files
Note: the ordering is VERY important.
==========================================================
-->
<!-- #Allow even users property file to relocate -->
<property
name="user.properties.file"
location="${user.home}/.build.properties"
/>
<!-- Load the application specific settings -->
<!-- #Project specific props -->
<property file="build.properties" />
<!--
==========================================================
Define your custom properties here.
You can overwrite them through build.properties and
${user.home}/.build.properties
==========================================================
-->
<property name="myexec1" location="/usr/bin/myexec1"/>
<property name="myexec2" location="/usr/bin/myexec2"/>
</project>
Important thing here is to come up with as many useful default property values as possible, then you may even never come up with custom build.properties files.
Then you just <import> this file in your project's build.xml.
<project
name="my-project"
>
<!-- this is done, so you may import my-project somewhere else -->
<dirname
property="my-project.basedir"
file="${ant.file.my-project}"
/>
<import file="${my-project.basedir}/relative/path/to/properties.xml"/>
<target name="using.myexec1">
<echo message="myexec1=${myexec1}"/>
</target>
</project>
If you want a custom value for myexec1 in my-project, just drop a custom flat build.properties file in the same directory where build.xml is located.
The build.properties file may look like this:
myexec1=c:/custom/path/to/myexec1.exe