pondělí 19. října 2015

Gradle - Get Hg Mercurial revision

Here is part of build.gradle file, which get mercurial revision and define task "revision", which print it to output.

It uses javahg to get revision number and that's why it needs Mercurial installation. We could use hg4j to remove this requirement. Anyway this script can be improved a lot, but for me it did it's work.

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.aragost.javahg:javahg:0.4'
}
}
task revision << {
println "Build revision: $scmRevision"
}
import com.aragost.javahg.Changeset
import com.aragost.javahg.Repository
import com.aragost.javahg.commands.ParentsCommand
String getHgRevision() {
def repo = Repository.open(projectDir)
def parentsCommand = new ParentsCommand(repo)
List<Changeset> changesets = parentsCommand.execute()
if (changesets == null || changesets.size() != 1) {
def message = "Exactly one was parent expected. " + changesets
throw new Exception(message)
}
return changesets[0].node
}
ext {
scmRevision = getHgRevision()
}
view raw build.gradle hosted with ❤ by GitHub

pátek 9. října 2015

Gradle - How to get Mercurial revision in gradle build file.

This part of gradle.build file add task "revision", which writes Mercurial changeset hash.

It can be improved, but ...

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.aragost.javahg:javahg:0.4'
}
}
task revision << {
println "Build revision: $scmRevision"
}
import com.aragost.javahg.Changeset
import com.aragost.javahg.Repository
import com.aragost.javahg.commands.ParentsCommand
String getHgRevision() {
def repo = Repository.open(projectDir)
def parentsCommand = new ParentsCommand(repo)
List<Changeset> changesets = parentsCommand.execute()
if (changesets == null || changesets.size() != 1) {
def message = "Exactly one was parent expected. " + changesets
throw new Exception(message)
}
return changesets[0].node
}
ext {
scmRevision = getHgRevision()
}
view raw build.gradle hosted with ❤ by GitHub

úterý 16. června 2015

Jak otestovat změnu času

Když je v testech potřeba posouvat čas. Bývá to poměrně pracné většinou člověk skončí s tím že píše nějaký "DateProvider", který se pak dá namockovat.

Ve spoustě případů se dá použít jednoduchá alternativa a využít Joda Time

Joda Time nabízí funkci DateTimeUtils.setCurrentMillisProvider(MillisProvider millisProvider) (a i další funkce setCurrentMillisFixed, setCurrentMillisOffset), kterými jde snadno říct, jak se má čas v testech chovat.
Po testu se musí čas zase vrátit do normálu!!! :) funkcí DateTimeUtils.setCurrentMillisSystem() V kódu se musí samozřejmě zjištovat čas přes Joda Time (např. new DateTime()).

A to je ve stručnosti vše.

úterý 12. května 2015

Maven Timeline Plugin - změření časů všech fází u konkrétního modulu.

Maven vypisuje časy buildu jednotlivých modulů. Jak ale zjistit proč konkrétní modul trvá tak dlouho...?

Narazil jsem na jednoduchý Maven Timeline Plugin, který vygeneruje jednoduché html s vizualizací časů.
Takto vypadá: http://blog.javabien.net/2014/04/22/maven-timeline-plugin/
A takto se používá: https://github.com/dgageot/maven-timeline