středa 9. května 2018

Kotlin - Append to StringBuilder using "+" "plus" instead of append() method

Kotlin - Override plus operator for StringBuilder

Kotlin - Append to StringBuilder using “+” “plus” instead of append() method

In Kotlin you can easily override operator. I will show simple example how can this help you to write more readable code.

At first we will override “plus” operator on for StringBuilder class

operator fun StringBuilder.plus(str: String): StringBuilder {
    append(str)
    return this
}

If you define this function as private. It will be accessible only in file, where it is defined. Or public and then it can be used anywhere in project.

And from this code:

sb.append("function ").append(generatedClassName).append("() {").append("\n")
functions.forEach { function: Function ->
    sb.append(indent).append("this.").append(function.name).append(" = undefined;").append("\n")
}
sb.append("}").append("\n")
sb.append("var ").append(name).append(" = new ").append(generatedClassName).append("();").append("\n")

we can remove all ‘append’ words and we get this code:
=>

sb + "function " + generatedClassName + "() {" + "\n"
functions.forEach { function: Function ->
    sb + indent + "this." + function.name + " = undefined;" + "\n"
}
sb + "}" + "\n"
sb + "var " + name + " = new " + generatedClassName + "();" + "\n"

Very simple, code is shorter and more readable.

úterý 1. května 2018

Gradle Kotlin DSL - Tipy

Gradle Kotlin DSL - Tipy

IntelliJ IDEA build by Gradle

build projektu u nás nedělá přímo IntelliJ IDEA ale deleguje to na Gradle

Idea Settings-> Gradle -> Runner -> Checkbox ‘Delegate IDE build/run actions to gradle’
enter image description here

Poznámky:
  • při buildu idea spouští tyhle tasky z gradlu :classes :testClasses

Jak Debugovat

  1. gradlew.bat <task> --no-daemon -Dorg.gradle.debug=true
  2. pak se připojit remote debuggerem na port 5005
    • Příklad nastavení v IntelliJ IDEA:
      Remote debugger - IntelliJ IDEA configuration example

ALE pozor! - build nejde spustit, pokud už předtím daemon běží

  • v tom potřeba je potřeba ho vypnout gradlew.bat --stop (to zastaví daemony)
Kam jde dát breakpoint:
  • jde do buildSrc
  • nešlo mi dát přímo do tasku v build.gradle.kts
  • nešlo mi dát do metody v build.gradle.kts (to v groovy gradlu fungovalo)

Parallelní build

používáme parallelní build java org.gradle.parallel=true
Proto pozor tasky, které na sobě nejsou závislé se spustí parallelně to znamená:
gradlew test uploadTestResults zde je nutné mít nastavenou závislost mezi tasky např:

tasks {
    val test by tasks.getting  
      
    val uploadTestResults by tasks.creating {  
        dependsOn(test)
        doLast {  
            ... process test resutls ...
        }  
    }
}

exclude task

V případě, vyjímečně chci spustit task bez jeho závislostí (např. když ho píšu) lze

gradlew uploadTestResults -x test - spustí ‘uploadTestResults’ bez tasku ‘test’

V konfiguraci tasku jde upravit jakékoliv parametry např:
gradle.startParameter.excludedTaskNames += "myTaskToExclude"

Logování v build scriptu

V build.gradle.kts je k dispzici variabla logger.
Gradle přidává novou logovací uroveň lifecycle a quiet
error, quiet, warning, lifecycle, info, debug
Výchozí úroven logovaní NENI info, ale LIFECYCLE !!!

gradlew <myTask> --info   - spusti příkaz s urovní info

Detaily zde: Gradle Logging

Jak získat logger uvnit buildSrc

val logger = Logging.getLogger(this.javaClass)
kde Logging je: import org.gradle.api.logging.Logging

Pak lze už normálně logovat:

logger.lifecycle("My log message")

Nastavení tasku

Vše patří do build.gradle.kts sekce tasks { ... }

Získání existujícího tasku:
val test by tasks.getting
Vytvoření nového tasku:
val jasmineResultsParser by tasks.creating {  
    doLast {  
      ... what task should do ...
    }  
}
validator, který zjistí zda je task “up to date”:
mytask {
    outputs.upToDateWhen { myFile.exists() }
}
Spouštět task jen v případě, že se spustí jiný task (=není excludovaný)
onlyIf{ 
   gradle.taskGraph.hasTask("mySubModule:test")
}

Další

Dependencies
  • gradlew :nuc-common:dependencies --configuration testCompile
    • vypíše strom závislostí pouze pro podprojekt ‘nuc-common’ a pouze pro konfiguraci ‘testCompile’
zjistit co se spusti

gradlew <myTask> --dry-run - nespustí příkazy, jen ukáže co by se spustilo

v IntelliJ IDEA nefunguje moc dobře navigace mezi fily

může pomoct plugin ‘Gradle/Maven Navigation’
enter image description here

Dependency management

Zavislosti se vyhodnocují jinak než v Mavenu. Gradle použije vždy nejnovější.

dependencyManagement {
    overriddenByDependencies(false)
}

úterý 3. října 2017

Jak naformátovat usb flash na FAT32

Potřeboval jsem kamarádovi naformátovat 64GB flasku na FAT32. Překvapilo mě, že to není zas tak jednodnoduché spousta způsobů nezabere. A protože se mi kamarád vždycky jednou za rok ozve a já už nevím jak jsem to dělal, tak si to tu napíšu:

Zabralo mi tohle:
Odsud http://www.ridgecrop.demon.co.uk/ stáhnout"FAT 32 Formatter".
Odkaz přímo na download: http://www.ridgecrop.demon.co.uk/download/fat32format.zip
a pak staci spustit:

fat32format.exe -c64 X:

X - je pismeno svazku
Příště to snad už najdu snáz.

středa 30. srpna 2017

Hg - Mercurial how to remove unwanted merge - using backout of backout

Issue
We have branches: "default" and "myB"
Into branch "default" was by mistake merged branch "myB".

Solution
One possible solution to solve this is:
Simple description: We will backout merge in "default" branch. Then we merge this backout into "myB" and then we will do "backout of this backout" in "myB" branch.

Exact way how to do it:

${merge} - revision with unwanted merge
${default_parent} - revision of parent changeset of ${merge} changeset in "default" branch
${myB_parent} - revision of parent changeset of ${merge} changeset in "myB" branch

commands:

hg update ${merge}
hg revert --all -r ${default_parent}
hg commit -m "backout of unwanted merge"   


hg update ${myB_parent}

hg merge tip
hg ci -m "merge backout into branch"

hg revert --all -r ${myB_parent}
hg commit -m "backout of backout"  



This is inspired by: https://www.mercurial-scm.org/pipermail/mercurial/2017-January/050172.html

sobota 25. února 2017

Maven - Jak spustit testy na všech modulech i když některé testy padají.

Maven buildi jednotlive moduly. A ihned jak v některém spadne test, tak build skončí, protože všechny ostatní moduly přeskočí.

  • mvn test
    - Build běží jen k modulu s prvním padajícím testem. A zbytek modulů přeskočí.
  • mvn test --fail-at-end
    - Pokud modul spadne, tak přeskočí jen moduly, které na tomto modulu zavísí.
  • mvn test --fail-never
    - Spouští všechny moduly - žádné nepřeskočí. I když některé popadají.

mvn test

Příklad:

[INFO] Reactor Summary:
[INFO] 
[INFO] smpl ................................................ SUCCESS [  1.166 s]
[INFO] smpl-test ........................................... SUCCESS [  4.077 s]
[INFO] smpl-common ......................................... SUCCESS [ 29.367 s]
[INFO] smpl-data ........................................... SUCCESS [  9.442 s]
[INFO] smpl-persistence .................................... SUCCESS [ 27.131 s]
[INFO] smpl-core ........................................... FAILURE [01:24 min]
[INFO] smpl-icm-persistence ................................ SKIPPED
[INFO] smpl-ips ............................................ SKIPPED
[INFO] smpl-sys-info ....................................... SKIPPED
[INFO] smpl-gmc-cloud ...................................... SKIPPED
[INFO] smpl-workflow ....................................... SKIPPED
[INFO] smpl-cluster ........................................ SKIPPED
[INFO] smpl-engine ......................................... SKIPPED
[INFO] smpl-definitions .................................... SKIPPED
[INFO] smpl-statistics-report-creator ...................... SKIPPED
[INFO] smpl-upgrade ........................................ SKIPPED
[INFO] smpl-monitoring ..................................... SKIPPED
[INFO] smpl-webserver ...................................... SKIPPED
[INFO] smpl-webserver-core ................................. SKIPPED
[INFO] smpl-node ........................................... SKIPPED
[INFO] smpl-icm-package-creator ............................ SKIPPED
[INFO] smpl-doc-exporter ................................... SKIPPED
[INFO] smpl-i18n-converter ................................. SKIPPED
[INFO] smpl-stopper ........................................ SKIPPED
[INFO] smpl-bobril ......................................... SKIPPED
[INFO] smpl-frontend ....................................... SKIPPED
[INFO] smpl-webserver-app .................................. SKIPPED
[INFO] smpl-sample-app ..................................... SKIPPED
[INFO] smpl-installer ...................................... SKIPPED
[INFO] smpl-incubator ...................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:35 min
[INFO] Finished at: 2017-01-19T09:38:42+01:00
[INFO] Final Memory: 54M/1044M
[INFO] ------------------------------------------------------------------------

mvn test --fail-at-end

Příklad:

[INFO] Reactor Summary:
[INFO] 
[INFO] smpl ................................................ SUCCESS [  1.443 s]
[INFO] smpl-test ........................................... SUCCESS [  5.906 s]
[INFO] smpl-common ......................................... SUCCESS [ 32.095 s]
[INFO] smpl-data ........................................... SUCCESS [ 13.179 s]
[INFO] smpl-persistence .................................... SUCCESS [ 28.213 s]
[INFO] smpl-core ........................................... FAILURE [01:27 min]
[INFO] smpl-icm-persistence ................................ SKIPPED
[INFO] smpl-ips ............................................ SKIPPED
[INFO] smpl-sys-info ....................................... SKIPPED
[INFO] smpl-gmc-cloud ...................................... SUCCESS [  1.289 s]
[INFO] smpl-workflow ....................................... SKIPPED
[INFO] smpl-cluster ........................................ SKIPPED
[INFO] smpl-engine ......................................... SKIPPED
[INFO] smpl-definitions .................................... SUCCESS [  0.693 s]
[INFO] smpl-statistics-report-creator ...................... SUCCESS [  1.108 s]
[INFO] smpl-upgrade ........................................ FAILURE [ 18.500 s]
[INFO] smpl-monitoring ..................................... SKIPPED
[INFO] smpl-webserver ...................................... SUCCESS [  0.630 s]
[INFO] smpl-webserver-core ................................. SKIPPED
[INFO] smpl-node ........................................... SKIPPED
[INFO] smpl-icm-package-creator ............................ SUCCESS [  1.222 s]
[INFO] smpl-doc-exporter ................................... SUCCESS [  1.074 s]
[INFO] smpl-i18n-converter ................................. SUCCESS [  1.342 s]
[INFO] smpl-stopper ........................................ SUCCESS [  0.833 s]
[INFO] smpl-bobril ......................................... SUCCESS [ 30.465 s]
[INFO] smpl-frontend ....................................... SUCCESS [01:42 min]
[INFO] smpl-webserver-app .................................. SKIPPED
[INFO] smpl-sample-app ..................................... SUCCESS [ 14.232 s]
[INFO] smpl-installer ...................................... SKIPPED
[INFO] smpl-incubator ...................................... SUCCESS [  0.620 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:42 min
[INFO] Finished at: 2017-01-19T09:27:37+01:00
[INFO] Final Memory: 65M/1149M

mvn test --fail-never

Příklad:

[INFO] Reactor Summary:
[INFO] 
[INFO] smpl ................................................ SUCCESS [  1.272 s]
[INFO] smpl-test ........................................... SUCCESS [  5.089 s]
[INFO] smpl-common ......................................... SUCCESS [ 32.105 s]
[INFO] smpl-data ........................................... SUCCESS [  9.521 s]
[INFO] smpl-persistence .................................... SUCCESS [ 27.626 s]
[INFO] smpl-core ........................................... FAILURE [01:25 min]
[INFO] smpl-icm-persistence ................................ FAILURE [ 10.546 s]
[INFO] smpl-ips ............................................ SUCCESS [ 13.612 s]
[INFO] smpl-sys-info ....................................... SUCCESS [  5.811 s]
[INFO] smpl-gmc-cloud ...................................... SUCCESS [  1.094 s]
[INFO] smpl-workflow ....................................... SUCCESS [ 31.439 s]
[INFO] smpl-cluster ........................................ SUCCESS [ 13.137 s]
[INFO] smpl-engine ......................................... FAILURE [01:31 min]
[INFO] smpl-definitions .................................... SUCCESS [  0.560 s]
[INFO] smpl-statistics-report-creator ...................... SUCCESS [  1.038 s]
[INFO] smpl-upgrade ........................................ FAILURE [ 16.010 s]
[INFO] smpl-monitoring ..................................... FAILURE [ 30.710 s]
[INFO] smpl-webserver ...................................... SUCCESS [  0.580 s]
[INFO] smpl-webserver-core ................................. SUCCESS [ 46.682 s]
[INFO] smpl-node ........................................... FAILURE [  6.602 s]
[INFO] smpl-icm-package-creator ............................ SUCCESS [  1.033 s]
[INFO] smpl-doc-exporter ................................... SUCCESS [  1.010 s]
[INFO] smpl-i18n-converter ................................. SUCCESS [  1.097 s]
[INFO] smpl-stopper ........................................ SUCCESS [  0.570 s]
[INFO] smpl-bobril ......................................... SUCCESS [ 14.878 s]
[INFO] smpl-frontend ....................................... SUCCESS [01:38 min]
[INFO] smpl-webserver-app .................................. SUCCESS [  2.341 s]
[INFO] smpl-sample-app ..................................... SUCCESS [ 13.023 s]
[INFO] smpl-installer ...................................... SUCCESS [ 17.248 s]
[INFO] smpl-incubator ...................................... SUCCESS [  0.557 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 09:41 min
[INFO] Finished at: 2017-01-19T09:50:06+01:00
[INFO] Final Memory: 77M/1483M
[INFO] ------------------------------------------------------------------------

pondělí 20. února 2017

Jak nadefinovat vlastní MBean ve Springu

Do svého aplikačního contextu přidat

    
    <context:mbean-export/>
(context je tento namespace xmlns:context="http://www.springframework.org/schema/context")

Pak již lze nadefinovat vlastní Mbean. (Příklad je v Kotlinu)

import org.springframework.jmx.export.annotation.ManagedOperation
import org.springframework.jmx.export.annotation.ManagedResource
import org.springframework.stereotype.Component

@Component
@ManagedResource(objectName = "MyApplicationName" + ":name=HelloMBeanWorld")
class HelloMBeanWorld {

    //Its component - you can simply autowire anything
    //@Autowired
    //private MyBean myBean

    @ManagedOperation(description = "My hello world MBean")
    fun getHelloMBeanWorld(): String {
        return "Hello MBean world!!!"
    }

}

A pak stačí spustit aplikaci a přistupovat ke své MBean třeba pomocí JConsole. Spring už pořeší, registrovaní a hlavně taky odregistrování bean a vše. Díky Springu!

Další možnosti jsou krásně popsané v dokumentaci springu: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jmx.html

pondělí 12. září 2016

Run Groovy Project Without Compilation Using Gradle

Run Groovy Project Without Compilation Using Gradle

To be honest it is automatically compiled by Gradle. All changes made into "buildSrc/src/main/groovy" are automatically compiled during startup.

Key feature of Gradle is how nicely it manages "buildSrc" directory.

Here is sample project https://bitbucket.org/bugs_/samples/src/default/RunGroovyProjectWithoutCompilationUsingGradle/