在这篇文章中,我将发布代码和步骤,以将Groovy脚本转换为二进制文件,以使其执行既不需要Groovy和Java都需要安装。另外,尽管很简单,但执行速度明显更好
特别是该应用程序将使用此平台的新API上传到LinkedIn(现在是文本,然后您会看到图像)。为此,您将要求author
和accessToken
能够发布
要求
完成所有必要的步骤
- sdkman, https://sdkman.io/install
安装后,我们将告诉您安装Java和Groovy的版本,以生成二进制文件:
sdk use groovy 4.0.11
sdk use java 22.3.r19-grl
配置
要生成一个刻板脚本的本地图像,有必要以静态模式进行编译(这仍然是Groovy的表现力,但是``)
compiler.config
withConfig(configuration) {
ast(groovy.transform.CompileStatic)
ast(groovy.transform.TypeChecked)
}
groovyscript
为了提高尊严的可读性,我将脚本分为2个.groovy文件,其中一个带有所有人的指向,以使请求http获取和发布,而另一个将是包含“逻辑业务的请求” “
httputil.groovy
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
class HttpUtil{
static Map postJson(Map map, String url, String token) {
println "-> ${JsonOutput.prettyPrint(JsonOutput.toJson(map).toString())}"
def request = HttpRequest.newBuilder(new URI(url))
.headers(
"Content-Type", "application/json",
"X-Restli-Protocol-Version", "2.0.0",
"LinkedIn-Version", "202301",
"Authorization", "Bearer $token"
)
.POST(HttpRequest.BodyPublishers.ofString(JsonOutput.toJson(map).toString()))
.build()
def response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofInputStream())
if( response.statusCode() > 299 ){
println response
return null
}
def result = response.body().text
println "<-${result}"
if( !result ){
return null
}
new JsonSlurper().parseText(result) as Map
}
static Map getJson(String url, String token){
def request = HttpRequest.newBuilder(new URI(url))
.headers(
"Content-Type", "application/json",
"X-Restli-Protocol-Version", "2.0.0",
"LinkedIn-Version", "202301",
"Authorization", "Bearer $token"
)
.GET()
.build()
def response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString())
def result = response.body()
println "<-${result}"
new JsonSlurper().parseText(result) as Map
}
}
虽然似乎有很多
postcontcontent.groovy
@GrabConfig(systemClassLoader = true)
@Grab('info.picocli:picocli-groovy:4.7.3')
@picocli.groovy.PicocliScript2
import groovy.transform.Field
import static picocli.CommandLine.*
@Option(names = ["-a", "--author"], description = "nickname", required=true)
@Field String author = ''
@Option(names = ["-k", "--token"], description = "token", required=true)
@Field String token = ''
@Option(names = ["-t", "--test"], description = "run as test")
@Field boolean test = false
@Parameters
@Field List<String> content = []
String postURL = "https://api.linkedin.com/rest/posts"
if( test ){
postURL="https://httpbin.org/delay/0"
}
Map post = [
"author" : "urn:li:person:${author}",
"commentary" : content.join(' '),
"visibility" : "PUBLIC",
"distribution" : [
"feedDistribution" : "MAIN_FEED",
"targetEntities" : [],
"thirdPartyDistributionChannels": []
],
"lifecycleState" : "PUBLISHED",
"isReshareDisabledByAuthor": false
]
HttpUtil.postJson(post, postURL, token)
此脚本使用picocli来促进命令。您只需要一个作者和一个令牌,我们还可以指出它是在测试模式(-t)中执行的,其中将使用HTTPBIN服务器作为后端进行测试
因此,这是LinkedIn期望发布帖子
的格式的“简单”帖子graalvm
将此脚本转换为二进制的阶段我将它们分为:
-
使用groovyc
从groovy到爪哇
-
在测试模式下使用Java执行它,以便代理生成graralvm的配置
-
执行本机图像以生成二进制
compile.sh
#!/bin/bash
set -e
# sdk use groovy 4.0.11
# sdk use java 22.3.r19-grl
echo compiling script
groovyc --configscript=compiler.groovy -d out PostContent.groovy
CP="$CP:./out"
CP="$CP:$HOME/.sdkman/candidates/groovy/4.0.11/lib/groovy-4.0.11.jar"
CP="$CP:$HOME/.sdkman/candidates/groovy/4.0.11/lib/groovy-json-4.0.11.jar"
CP="$CP:$HOME/.sdkman/candidates/groovy/4.0.11/lib/groovy-cli-picocli-4.0.11.jar"
CP="$CP:$HOME/.groovy/grapes/info.picocli/picocli/jars/picocli-4.7.3.jar"
CP="$CP:$HOME/.groovy/grapes/info.picocli/picocli-groovy/jars/picocli-groovy-4.7.3.jar"
echo generating graalvm configuration
java -Dgroovy.grape.enable=false -agentlib:native-image-agent=config-output-dir=conf/ \
-cp "$CP" \
PostContent -t -a test -k test test
echo building native image
native-image -Dgroovy.grape.enable=false \
--no-server \
--no-fallback \
--report-unsupported-elements-at-runtime \
--initialize-at-build-time \
--initialize-at-run-time=org.codehaus.groovy.control.XStreamUtils,groovy.grape.GrapeIvy \
-H:ConfigurationFileDirectories=out/conf/ \
--enable-url-protocols=http,https \
-cp "$CP" \
-H:ConfigurationFileDirectories=conf/ \
PostContent
如果在流程结束时一切顺利(小于1分钟),我们将拥有一个可以执行的kude4二进制文件
./postcontent -a NH123123 -k A_BEARER_TOKEN_YOU_CAN_USE_MY_PREVIOUS_POST Hi this is a post from groovy script
- 信息
-
要获取令牌,我指的是我告诉您如何生成它的另一篇文章,或者如果您喜欢使用该服务 我已经出版了。
结论
Groovy和Graralvm的新版本允许(我承认稍作努力)创建二进制控制台命令,该命令为Mã打开门,以便能够使用我喜欢的语言分发利润
正在待定未来能够附加图像,而新的API LinkedIn使它更加复杂。