BuildGraalVMnativeimagesusingClojureDepsandCLItools.
Thisshouldbeusefulforcreatinglightweight,nativeCLIexecutablesusingClojureanddeps.edn.Seeclj.native-cliforastarterprojecttemplate.
Thisprojectdependsontools.deps.alphaandshouldbeconsideredalphaitself.
PrerequisitesClojureCLItools
GraalVM
NOTE:AsofGraalVM19.0.0,native-imageisnolongerincludedbydefault:
NativeImagewasextractedfromthebaseGraalVMdistribution.Currentlyitisavailableasanearlyadopterplugin.Toinstallit,run:guinstallnative-image.Afterthisadditionalstep,thenative-imageexecutablewillbeinthebindirectory,asforthepreviousreleases.
➜$GRAALVM_HOME/bin/guinstallnative-imageDownloading:Componentcatalogfromwww.graalvm.orgProcessingcomponentarchive:NativeImageDownloading:Componentnative-image:NativeImagefromgithub.comInstallingnewcomponent:NativeImagelicencefiles(org.graalvm.native-image,version19.0.0)UsageAssumingaprojectstructurelikethis:
.├──deps.edn└──src└──core.cljInyourdeps.ednspecifyanaliaswithadependencyonclj.native-image:
{:aliases{:native-image{:main-opts["-m""clj.native-image""core""--initialize-at-build-time";;optionalnativeimagenameoverride"-H:Name=core"]:jvm-opts["-Dclojure.compiler.direct-linking=true"]:extra-deps{clj.native-image/clj.native-image{:git/url"https://github.com/taylorwood/clj.native-image.git":sha"7708e7fd4572459c81f6a6b8e44c96f41cdd92d4"}}}}}Wherecore.cljisaclasswith-mainentrypoint,forexample:
(nscore(:gen-class))(defn-main[&args](println"Hello,World!"))Fromyourprojectdirectory,invokeclojurewiththenative-imagealias,specifyingthemainnamespace(coreinexampleabove):
➜clojure-A:native-imageLoadingcoreCompilingcoreBuildingnativeimage'core'withclasspath'classes:src:etc.'classlist:1,944.26ms8<----------------------[total]:38,970.37msNote:EitherGRAALVM_HOMEenvironmentvariablemustbeset,orGraalVM'snative-imagepathmustbepassedasanargument,andanyadditionalargumentswillbepassedtonative-imagee.g.:
➜clojure-A:native-image--verboseYoucannowexecutethenativeimage:
➜./coreHello,World!SeethisGistforanotherexample.
ExampleProjectsThereareexampledeps.ednprojectsinthelein-native-imagerepo:
jdnsmith-CLIJSON-to-EDNtransformerhttp-api-simpleHTTPAPIserverclojurl-cURL-liketoolusingclojure.spec,HTTPS,hiccupCaveatsThe--no-serverflagispassedtonative-imagebydefault,toavoidcreatingorphanedbuildservers.
Alsoseecaveatssectionoflein-native-image.
ReferencesGraalVMNativeImageAOTCompilation
Thisprojectwasinspiredbydepstar.
ContributingYou'llneedClojureCLItoolingandGraalVMinstalledtotestlocally.Justchangethesourceoftheclj.native-imagedependencytoa:local/rootinsteadof:git/url.
Issues,PRs,andsuggestionsarewelcome!
LicenseCopyright©2018TaylorWood.
DistributedundertheMITLicense.
评论