Gradle version in Jenkins build

Out of the box Jenkins can extract the metadata from Maven build for use as environment variables in the build steps, but it doesn’t have the same support for Gradle. This can be achieved using the EnvInject plugin, and a custom task in your build.gradle file. The first step is to setup a task in build.gradle that will print out the project name and version in a format that can be used directly in a properties file. ...

August 31, 2017 · Nigel Sim

The strange beast of PyTZ and datetime

Update A good explaintation of what is happening is provided here. In this day and age dateutil.tz is the better solution to timezones, as it works as expected with datetime. TL;DR The crux of this post is, make sure to never use datetime.replace(tzinfo=...) when working with PyTZ, use tz.localize(...) instead, otherwise you’ll end up with some very strange times. The PyTZ docs do mention this helper method as a way to fix incorrect conversion across timezones, but out of the box PyTZ timezones seem odd. Consider this simple code that takes both the native datetime.replace approach, and the localize approach: ...

November 11, 2014 · Nigel Sim

Null and JPA2 Native Query in MSSQL

This post involves a slightly edge case scenario that I encountered a couple of months ago, so hopefully I get all the details right the first time. Essentially, I had a JPA2 project using Hibernate 3.6.10 as the ORM. This project had a requirement of some native SQL being used for dynamic table creation, so to achieve this I would call Query q = em.createNativeQuery(sql); and the proceed to call q.setParameter(...). This worked fine for both setting columns to a value, and setting them to null, at least on H2 and MySQL. However, if you tried to set to null when using SQLServer you’d get the following: ...

October 3, 2014 · Nigel Sim

Saltstack: Passing objects to templates

Quick one. When you pass a variable like this to a template through the context/default parameter it is iterpreted as a literal string: server_xml: file: - managed - name: /opt/tomcat/conf/server.xml - template: jinja - source: salt://tomcat/files/server.xml.tmpl - context: deploy_conf: deploy_conf Which means that you end up with errors like this: Unable to manage file: Jinja variable 'unicode object' has no attribute 'control_port To pass the object itself in you need to put it in the braces: ...

May 12, 2014 · Nigel Sim

Using MapMessage with ActiveMQ with a Python Stomp.py consumer

Out of the box a STOMP consumer on an ActiveMQ broker will be able to receive TextMessages, but MapMessages will arrive without content. This is because we need to specify a converter to ActiveMQ(?) which we can do in the subscription setup, in a similar way that this thread discusses sending MapMessages from the Python end. Unfortunately there is a little bit of manual handling, because the best the client library is do is deliver a string, so you’ll have to handle the deserialisation. ...

December 11, 2013 · Nigel Sim

Simple type checking using property()

Python usually relies of duck typing for type safety, but from time to time it can be handy to enforce some type checking, particularly when new users are going to be using your objects. The following are three utility methods for applying type checking to class properties, using the new style object property() method. def deleter(attr): """Deleter closure, used to remove the inner variable""" def deleter_real(self): return delattr(self, attr) return deleter_real def getter(attr): """Getter closure, used to simply return the inner variable""" def getter_real(self): return getattr(self, attr) return getter_real def setter(attr, valid_types): """Setter closure, used to do type checking before storing var""" def setter_real(self, var): if not isinstance(var, valid_types): raise TypeError("Not of required type: "+str(valid_types)) setattr(self,attr,var) return setter_real def typed(attr, valid_types, docs=""): """Wrapper around property() so that we can easily apply type checking to properties""" return property(getter(attr), setter(attr, valid_types), deleter(attr), docs) # Example class class A(object): a = typed("_a", int) # Testing output a1 = A() a1.a = 1 print "Got stored value = " + str(a1.a) a1.a = "1" The results are: ...

January 8, 2013 · Nigel Sim

Active Directory on EC2/VPC - Using Elastic IP in DNS

The basic use case is this: we want an Active Directory server running in an AWS VPC that can serve machine within the VPC, and in other locations. The AD DC has an Elastic IP to allow external entities to access it, specifically the DNS. However, due to the way Elastic IPs work the Windows network stack sees its IP as being in the 10.0.0.0/16 range of the VPC, and so, the dynamic updating of the DC’s DNS entries results in all the address pointers being to this private IP. ...

December 18, 2012 · Nigel Sim

Reloading Tiles2 Config in Spring 3.x

When you are using Tiles for layout composition with Spring you configure it as a view resolver by adding something like this to the applicationContext.xml <!-- Configure the Tiles templates --> <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> <property name="definitions"> <list> <value>/WEB-INF/tiles.xml</value> </list> </property> <property name="preparerFactoryClass" value="org.springframework.web.servlet.view.tiles2.SpringBeanPreparerFactory" /> </bean> <!-- Resolve views using Tiles --> <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" /> </bean> This will read the tiles.xml on startup. If you want it to refresh when the file changes you either need to add the Tiles filter, or more elegantly just add this context-param to the web.xml ...

September 21, 2011 · Nigel Sim

Spring MVC Validation BindingResult

A quick note about using the BindingResult to detect and report errors in a form. One gotcha that got me was the need to set a name on the @ModelAttribute in order to properly relate the form:form commandName and the validation object. Essentially, if you don’t set a name then @ModelAttribute will get the command name from the name of the argument, and BindingResult will get the command name from the type of the argument, meaning that when you go to use form:errors nothing will be displayed. ...

September 7, 2011 · Nigel Sim

Programmatically getting the Maven version of your project

It is often handy to be able to extract the Maven version of your project at run time, either for displaying in an about box, or in debugging information. One option is to read /META-INF/maven/${groupId}/${artifactId}/pom.properties. However, this file is only available in the packaged version of your project, so during development the method will fail. The approach I’ve taken to fulfil this requirement is to create a text (properties) file in the project resources, and have Maven process this. This allows Maven to inject the version number during compile. The following snippets need to be configured. ...

August 31, 2011 · Nigel Sim