Inicio
Explorar
Enviar búsqueda
Cargar
Iniciar sesión
Registrarse
Publicidad
Groovy on the shell
Denunciar
sascha_klein
Seguir
2 de Jun de 2017
•
0 recomendaciones
1 recomendaciones
×
Sé el primero en que te guste
ver más
•
776 vistas
vistas
×
Total de vistas
0
En Slideshare
0
De embebidos
0
Número de embebidos
0
Check these out next
Django & Buildout (en)
zerok
Kettunen, miaubiz fuzzing at scale and in style
DefconRussia
淺談 Groovy 與 AWS 雲端應用開發整合
Kyle Lin
The Ring programming language version 1.10 book - Part 23 of 212
Mahmoud Samir Fayed
Introduction to zc.buildout
Ricardo Newbery
Cara instal
عبد المنان الشافعي
Hls за час
volegg
DevOps for Opensource Geospatial Applications
tlpinney
1
de
45
Top clipped slide
Groovy on the shell
2 de Jun de 2017
•
0 recomendaciones
1 recomendaciones
×
Sé el primero en que te guste
ver más
•
776 vistas
vistas
×
Total de vistas
0
En Slideshare
0
De embebidos
0
Número de embebidos
0
Descargar ahora
Descargar para leer sin conexión
Denunciar
Software
Slides from my talk Groovy on the shell at Gr8conf EU 2017
sascha_klein
Seguir
Publicidad
Publicidad
Publicidad
Recomendados
GroovyFX - groove JavaFX Gr8Conf EU 2017
sascha_klein
610 vistas
•
52 diapositivas
Shestakov Illia "The Sandbox Theory"
LogeekNightUkraine
95 vistas
•
21 diapositivas
Grooscript in Action SpringOne2gx 2015
Jorge Franco Leza
2.3K vistas
•
49 diapositivas
Capistrano与jenkins(hudson)在java web项目中的实践
crazycode t
1.1K vistas
•
80 diapositivas
Pentesting an unfriendly environment: bypassing (un)common defences and mate ...
Sandro Zaccarini
308 vistas
•
61 diapositivas
Игры с огнём: знакомимся с BDD и Cucumber фреймворком BDDfire
SQALab
3.2K vistas
•
63 diapositivas
Más contenido relacionado
Presentaciones para ti
(20)
Django & Buildout (en)
zerok
•
11.5K vistas
Kettunen, miaubiz fuzzing at scale and in style
DefconRussia
•
2.2K vistas
淺談 Groovy 與 AWS 雲端應用開發整合
Kyle Lin
•
12.6K vistas
The Ring programming language version 1.10 book - Part 23 of 212
Mahmoud Samir Fayed
•
15 vistas
Introduction to zc.buildout
Ricardo Newbery
•
958 vistas
Cara instal
عبد المنان الشافعي
•
99 vistas
Hls за час
volegg
•
1.2K vistas
DevOps for Opensource Geospatial Applications
tlpinney
•
540 vistas
Windows Azure loves OSS
Kazumi Hirose
•
9.5K vistas
Stupid Buildout Tricks
Ricardo Newbery
•
3.2K vistas
Security of Go Modules and Vulnerability Scanning in GoCenter and VS Code
Deep Datta
•
155 vistas
2012 12-08-travis-ci-could-do
Kenichi Murahashi
•
2.1K vistas
Security of Go Modules and Vulnerability Scanning in VSCode
Deep Datta
•
111 vistas
Embedding Groovy in a Java Application
Paolo Predonzani
•
11.2K vistas
Security of go modules and vulnerability scanning in GoCenter
Deep Datta
•
306 vistas
New Security of Go modules and vulnerability scanning in GoCenter
Deep Datta
•
157 vistas
Security of Go Modules and Vulnerability Scanning in GoCenter and VSCode
Deep Datta
•
224 vistas
Security of go modules and vulnerability scanning in go center (1)
Deep Datta
•
176 vistas
Vulpes tribes backend
Jiří Soušek
•
74 vistas
More developers on DevOps with Docker orchestration
Giulio De Donato
•
672 vistas
Similar a Groovy on the shell
(20)
Universal groovy
Shin-Jan Wu
•
311 vistas
Untangling fall2017 week2_try2
Derek Jacoby
•
308 vistas
Untangling fall2017 week2
Derek Jacoby
•
167 vistas
Goの標準的な開発の流れ
Ryuji Iwata
•
446 vistas
Cape Cod Web Technology Meetup - 3
Asher Martin
•
467 vistas
Drone CI
Thomas Boerger
•
630 vistas
Docker on Windows
Carl Su
•
2.4K vistas
Lets make better scripts
Michael Boelen
•
122 vistas
Vagrant - Version control your dev environment
bocribbz
•
3.7K vistas
Life of a Chromium Developer
mpaproductions
•
1.8K vistas
Boxen: How to Manage an Army of Laptops
Puppet
•
19.1K vistas
Biicode OpenExpoDay
fcofdezc
•
624 vistas
Using the Groovy Ecosystem for Rapid JVM Development
Schalk Cronjé
•
894 vistas
Scaling Development Environments with Docker
Docker, Inc.
•
8.8K vistas
Docker perl build
Workhorse Computing
•
3.3K vistas
GroovyFX - Groove JavaFX
sascha_klein
•
709 vistas
WebSocket on client & server using websocket-sharp & ASP.NET Core
Chen Yu Pao
•
3K vistas
Sonatype DevSecOps Leadership forum 2020
Daniel Garcia (a.k.a cr0hn)
•
167 vistas
Zenoss: Buildout
Jeffrey Clark
•
774 vistas
Security of Go Modules and Vulnerability Scanning in GoCenter and VSCode
Deep Datta
•
159 vistas
Publicidad
Más de sascha_klein
(8)
DSL101
sascha_klein
•
674 vistas
Bob the Builder - Gr8Conf EU 2017
sascha_klein
•
252 vistas
Using Groovy with Jenkins
sascha_klein
•
2.8K vistas
Introduction to Graphics- and UI-Design
sascha_klein
•
898 vistas
Android on Groovy
sascha_klein
•
2.9K vistas
Groovy on the shell
sascha_klein
•
1.9K vistas
Groovy on the Shell
sascha_klein
•
5.8K vistas
Vert.x using Groovy - Simplifying non-blocking code
sascha_klein
•
3.4K vistas
Último
(20)
意大利罗马二大毕业证文凭成绩单制作指南
nahej99297
•
0 vistas
法国亚眠大学毕业证文凭成绩单制作指南
nahej99297
•
0 vistas
Enterprises-Have-Replaced-12000-ADCs-See-Why.pptx
Avi Networks
•
2 vistas
Machine_Learning_Engineering_with_Python.pdf
HikmetMARASLI
•
0 vistas
python ppt.pptx
ssuserd10678
•
2 vistas
在哪里可以办荷兰大学文凭《荷兰南方高等专业大学毕业证成绩单仿制》
nukotk
•
2 vistas
Everything you should know about Adams.pdf
Qandle
•
0 vistas
High performance PHP8 at Scale - PhpersSummit 2023
Max Małecki
•
12 vistas
Boost Your Business with ChatGPT-3 Integrated CRM - Slideshare.docx
Yoroflow
•
2 vistas
Software testing.pdf
SwagatGogoi3
•
5 vistas
Memory Management with Page Folios
Adrian Huang
•
2 vistas
Math Behind Software
Bartłomiej Żyliński
•
4 vistas
在哪里可以办英国大学文凭《罗伯特戈登大学毕业证成绩单仿制》
nukotk
•
3 vistas
App modernization.pdf
SahilMalik864906
•
0 vistas
JPrime_JITServer.pptx
Grace Jansen
•
0 vistas
Patches_Presentation.pptx
ssuser46d193
•
0 vistas
DREAM 5 benefits of CRM Software for pest management.pptx
Dream Service Software
•
0 vistas
AI: Your Personal Intern
Joe Brinkman
•
0 vistas
ch11lect1.ppt
NupurMishra34
•
2 vistas
Why Choose Laravel Framework for your next project ?
Softlabs Group
•
2 vistas
Publicidad
Groovy on the shell
Groovy on the shell_ Alexander (Sascha) Klein <alexander.klein@codecentric.de> 1
Why ? 2
Groovy is cool 3
Because I can 4
To do complex stuff 5
Testing your scripts 6
If you know Groovy better than Bash 7
About me Alexander Klein Branchmanager codecentric AG Curiestr. 2 70563 Stuttgart, Germany +49 (0) 172 529 40 20 alexander.klein@codecentric.de www.codecentric.de blog.codecentric.de @saschaklein 8
Executing Groovy scripts $ groovy Test.groovy 9
She-Bang Comment (#!) #!/usr/local/bin/groovy 10
Using the default environment Defining the classpath Using system variables Defining system variables #!/usr/bin/env groovy #!/usr/bin/env groovy -cp myjar.jar #!/usr/bin/env groovy -cp ${HOME}/bin #!/usr/bin/env VAR1=value1 VAR2=value2 groovy 11
But Does not work on Linux Alternative script for /usr/bin/env Without defining system variables #!/bin/bash ARGS=( $1 ) # separate $1 into multiple space-delimited arguments. shift # consume $1 PROG=`which ${ARGS[0]}` # find path for executable unset ARGS[0] # discard executable name ARGS+=( "$@" ) # remainder of arguments preserved "as-is". # iterate array and expand variables declare -a PARAMS for i in "${ARGS[@]}" do PARAMS=("${PARAMS[@]}" "`eval "echo $i"`") done exec $PROG "${PARAMS[@]}" # execute the command 12
'Hacks' for the rescue #!/bin/bash //usr/bin/env /path/to/groovy "$0" $@; exit $? println "My groovy script" #!/bin/bash //usr/bin/true && export CLASSPATH=.... //usr/bin/true && export OPTS="-Dmy.opt1=foo -Dmy.opt2=bar" //usr/bin/true && export OPTS="$OPTS -Dmy.opt3=foobar" //path/to/groovy $OPTS "$0" $0; exit $? println System.env[my.opt1] 13
'Hacks' for the rescue #!/bin/bash REM=''' ' # bash stuff here export export CLASSPATH=.... export OPTS="-Dmy.opt1=foo -Dmy.opt2=bar" export OPTS="$OPTS -Dmy.opt3=foobar" /path/to/groovy $OPTS "$0" $@ exit $? ' ''' // groovy stuff here println "My groovy script" 14
A solution for Windows @ECHO OFF REM = / REM dummy groovy statement in first line with an REM assignment to a dummy var called REM SET _JAVA_OPTIONS= SET CLASSPATH= C:pathtogroovy "%~dp0%~nx0" %* GOTO :EOF / // dummy groovy annotation to make groovy compiler // to ignore the first line @interface ECHO {} // groovy stuff here println "My groovy script" 15
GroovyServ Faster Startup JVM process running in the background Scriptstart ~10-20 times faster 16
Installation Windows GroovyServ is part of the Groovy Windows-Installer Linux Mac OS X Binary package curl -s "https://get.sdkman.io" | bash sdk install groovyserv brew install groovyserv $ cd /tmp $ unzip groovyserv-1.1.0-bin.zip $ groovyserv-1.1.0-bin/bin/setup.sh export PATH=/tmp/groovyserv-1.1.0/bin:$PATH 17
Usage $ groovyclient MyScript.groovy #!/usr/bin/env groovyclient 18
Writing Scripts 19
Restrictions Classname == Filename to be found from other script 20
Executing shell commands "mkdir foo".execute() ["mkdir", "my directory"].execute() 21
Workingdir & Environment "ls".execute([], new File("/tmp")) "env".execute(["VAR1=Test", "VAR2=Something"], new File(".")) "env".execute([*:System.env, VAR1: 'Test'] .collect{ k,v -> "$k=$v" }, new File(".")) 22
Result access println "ls".execute().text println "cmd /c dir".execute().text "ls".execute().inputStream.eachLine { println it } "ls".execute().in.eachLine { println it } "myCommand".execute().err.eachLine { println it } def proc = new ProcessBuilder("myCommand") .redirectErrorStream(true).start() proc.in.eachLine { println it } 23
Process control Process process = "mkdir foo".execute() process.waitFor() int exitValue = process.exitValue() if(!exitValue) { //do your error-handling } if(!"mkdir foo".execute().waitFor()) { //do your error-handling } "grep pattern".execute().waitForOrKill(1000) def process = "myLongRunningCommand".execute() ... process.destroy() 24
Process output Process process = "myCommand".execute() def out = new StringBuffer() def err = new StringBuffer() process.waitForProcessOutput( out, err ) if( out.size() > 0 ) println out if( err.size() > 0 ) println err def p = "rm -f foo.tmp".execute([], tmpDir) p.consumeProcessOutput() // Prevent blocking by small buffer p.waitFor() 25
Piping "less temp.sh".execute().pipeTo("grep Detected".execute()).text def proc1 = "less temp.sh".execute() def proc2 = "grep Detected".execute() proc1 | proc2 println proc2.text 26
Wildcards "ls *.java".execute() // won't work "sh -c ls *.java".execute() // Shell resolves the wildcard 27
Shell Helper I class Shell { final Map env = System.env File dir = new File('.') boolean redirectErrorStream = false long timeout = 5000 Shell env(Map env) { this.env.putAll(env); return this } Shell dir(File dir) { this.dir = dir; return this } Shell dir(String dir) { this.dir = new File(dir); return this } Shell redirectErrorStream(boolean redirectErrorStream = true) { this.redirectErrorStream = redirectErrorStream; return this } Shell timeout(int timeout = 5000) { this.timeout = timeout; return this } 28
Shell Helper II Process execute(String command) { new ProcessBuilder(['sh', '-c', command]) .directory(dir) .redirectErrorStream(redirectErrorStream) .environment(env.collect{k,v -> "$k=$v"} as String[]) .start() } int call(String command, boolean consumeOutput = true) { Process proc = execute(command) if(consumeOutput) proc.consumeProcessOutput() if(timeout) proc.waitForOrKill(timeout) else proc.waitFor() return proc.exitValue() } def eachLine(String command, Closure action) { execute(command).in.eachLine(action) } } 29
Shell Helper III def shell = new Shell() shell.call("mkdir /tmp/groovy") shell("echo 123 >> /tmp/groovy/test") shell.dir("/tmp/groovy").call("echo $HOME >> test2") shell.eachLine("ls ."){ println "- $it -" } shell.eachLine("cat test2"){ println "- $it -" } 30
Helpfull stuff Accessing System-Variables Accessing System-Properties Get your PID println System.env.PWD println System.properties.'user.dir' import java.lang.management.* println ManagementFactory.runtimeMXBean.name.split('@').first() 31
CliBuilder 32
Parsing Commandline arguments DSL ontop of Apache Commons CLI !#/usr/bin/env groovy def cli = new CliBuilder(usage: 'MyScript') cli.with { v(longOpt: 'version', 'show version information') } def opt = cli.parse(args) if (!opt) System.exit(2) if (opt.v) { println "Version: 1.0.0" } 33
A Sample usage: MyScript [options] [args] -?,--help usage information --config <arg> A script for tweaking the configuration -D <property=value> use value for given property -s,--source <path> Aliases for '-source' -source <path> Specify where to find the files -v,--version version information 34
The code I #!/usr/bin/env groovy def cli = new CliBuilder(usage: 'MyScript [options] [args]') cli.with { source(args:1, argName: 'path', optionalArg: false, 'Specify where to find the files') _ (longOpt: 'config', args:1, argName: 'arg', optionalArg: false, 'A script for tweaking the configuration') s (longOpt: 'source', args:1, argName:'path', optionalArg: false, "Aliases for '-source'") '?' (longOpt: 'help', 'usage information') v (longOpt: 'version', 'version information') D (args: 2, valueSeparator: '=', argName: 'property=value', 'use value for given property') } 35
The code II def opt = cli.parse(args) if (!opt) // usage already displayed by cli.parse() System.exit(2) if(opt.'?') cli.usage() else if (opt.v) println "Version: 1.0.0" else { if (opt.config) println "Configuration: $opt.config" if(opt.D) { println "Custom properties:" println opt.Ds.collate(2).collect{ it.join('=') }.join('n') } def home = System.properties.'user.dir' if (opt.s || opt.source) home = opt.s ?: opt.source println "Working on files:" opt.arguments().each println "$home/$it" } 36
Questions? Alexander Klein Branchmanager codecentric AG Curiestr. 2 70563 Stuttgart, Germany +49 (0) 172 529 40 20 alexander.klein@codecentric.de www.codecentric.de blog.codecentric.de @saschaklein 37
Dependency Management 38
Grape Groovy Adaptable Packaging Engine Cachedirectory ~/.groovy/grape @Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE') import org.springframework.jdbc.core.JdbcTemplate @Grab('org.springframework:spring-orm:3.2.5.RELEASE') import org.springframework.jdbc.core.JdbcTemplate 39
@Grab Annotation I Annotatable everywhere often seen at import statements group (String) Maven groupId or Ivy organization module (String) Maven artifactId or Ivy artifact version (String) literal '1.1-RC3' Ivy range '[1.0, 2,0]' → 1.0 or 2.0 '[2.1,)' → 2.1 or greater classifier (String) 'jdk15' optional 40
Multiple Dependencies @Grapes([ @Grab(group='commons-primitives', module='commons-primitives', version='1.0'), @Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='0.9.7') ]) class Example { ... } 41
Repositories Configuration in ~/.groovy/grapeConfig.xml Grape cache → ~/.groovy/grapes Maven Local → ~/.m2/repository JCenter (includes Maven Central) IBiblio Java.net http://jcenter.bintray.com http://www.ibiblio.org http://download.java.net/maven/2 @GrabResolver(name='restlet', root='http://maven.restlet.org/') @Grab(group='org.restlet', module='org.restlet', version='1.1.6') 42
Exclude transitive dependencies @Grab('net.sourceforge.htmlunit:htmlunit:2.8') @GrabExclude('xml-apis:xml-apis') 43
GrabConfig GrabConfig systemClassLoader (boolean) using the System-ClassLoader initContextClassLoader (boolean) ContextClassLoader = CL of the current thread autoDownload (boolean) automatic downloading disableChecksums (boolean) checking checksums @GrabConfig(systemClassLoader=true) @Grab(group='mysql', module='mysql-connector-java', version='5.1.6') 44
Proxy-Configuration $ groovy -Dhttp.proxyHost=<host> -Dhttp.proxyPort=<port> -Dhttp.proxyUser=<user> -Dhttp.proxyPassword=<user> yourscript.groovy JAVA_OPTS = -Dhttp.proxyHost=<host> -Dhttp.proxyPort=<port> -Dhttp.proxyUser=<user> -Dhttp.proxyPassword=<user> 45
Publicidad