8

JBang Cheatsheet (2024) for Java Developers

 8 months ago
source link: https://www.mastertheboss.com/java/jbang-cheatsheet-2021/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JBang Cheatsheet (2024) for Java Developers

6 January 20246 January 2024 by F.Marchioni

Welcome to the JBang Cheatsheet, your comprehensive guide to mastering this powerful tool for rapid Java development. JBang eliminates the need for complex setup and repetitive configuration, allowing you to focus on writing code and exploring new Java projects. Whether you’re a seasoned Java developer or just starting out, this cheatsheet will equip you with the knowledge and skills to leverage JBang‘s capabilities and accelerate your Java development journey.

Note: if you are new to JBang we recommend checking this article: JBang: Create Java scripts like a pro

JBang CheatSheet for Java Developers

Install JBang on Linux

curl -Ls https://sh.jbang.dev | bash -s - app setup
curl -Ls https://sh.jbang.dev | bash -s - app setup

Upgrade JBang

jbang version --update
jbang version --update

Init a JBang Script

// --template=hello
jbang init hello.java
// --template=hello
jbang init hello.java

Execute a JBang Script

jbang example.java
jbang example.java

Debug a script

jbang --debug helloworld.java
jbang --debug helloworld.java

Edit a JBang script

jbang edit -b foo.java
jbang edit -b foo.java

Run a Java class in a JAR file

jbang file.jar --main=com.App
jbang file.jar --main=com.App

Run a Class using Maven GAV

jbang group:artifact:version --main=com.App
jbang group:artifact:version --main=com.App

Run a Java Snippet

echo "hello world" | jbang -c 'lines().forEach(s->println(s.substring(6)))`
echo "hello world" | jbang -c 'lines().forEach(s->println(s.substring(6)))`

Create an Executable JAR

jbang export portable MyApp.java
jbang export portable MyApp.java

Generate the Maven POM

jbang export mavenrepo --force -O target --group=com.sample -Dartifact=example -Dversion=1.0 MyApp.java
jbang export mavenrepo --force -O target --group=com.sample -Dartifact=example -Dversion=1.0 MyApp.java

Add Dependency

//DEPS log4j:log4j:1.2.17
//DEPS log4j:log4j:1.2.17

Add a GitHub Dependency

//DEPS https://github.com/DiUS/java-faker
//DEPS https://github.com/DiUS/java-faker

Add BOM Dependency

//DEPS io.quarkus:quarkus-bom:1.11.0.Final@pom
//DEPS io.quarkus:quarkus-resteasy
//DEPS io.quarkus:quarkus-bom:1.11.0.Final@pom
//DEPS io.quarkus:quarkus-resteasy

Add JAR/Source Dependency

//DEPS myapp.jar
//DEPS myclass.java
//DEPS myapp.jar 
//DEPS myclass.java

Use System Properties in Comments

//DEPS group:artifact${version}
//DEPS group:artifact${version}

Use Env Vars in Comments

//DEPS group:artifact${env.version}
//DEPS group:artifact${env.version}

Pass Dependencies to jbang

jbang --deps g1:a1:v1,g2:a2:v2 test.java
jbang --deps g1:a1:v1,g2:a2:v2 test.java

Add File Resources

//FILES resource.properties
//FILES META-INF/resources/index.html=index.html
//FILES resource.properties
//FILES META-INF/resources/index.html=index.html

Add Manifest

//MANIFEST version=${version:unknown}
//MANIFEST version=${version:unknown}

Set Quarkus Properties

//Q:CONFIG quarkus.datasource.db-kind=postgresql
//Q:CONFIG quarkus.datasource.db-kind=postgresql

Remote file expansion [1]

jbang CountWords.java %https://github.com/dwyl/english-words/raw/master/words.txt
jbang CountWords.java %https://github.com/dwyl/english-words/raw/master/words.txt

Add Repository

//REPOS mavencentral,acme=http://maven.acme.repo
//REPOS mavencentral,acme=http://maven.acme.repo

Add Java Sources

//SOURCES App.java
//SOURCES othernested/*.java
//SOURCES App.java
//SOURCES othernested/*.java

JDK Configuration

//JAVA 11 forces use of Java 11.
//JAVA 13+ Java 13 or higher will be used.
//JAVA 11  forces use of Java 11.
//JAVA 13+ Java 13 or higher will be used.

Install a JDK

jbang jdk install 17
jbang jdk install 17

Install JDK from your Machine

jbang jdk install 17 `sdk home java 17.0.4.1-tem`
jbang jdk install 17 `sdk home java 17.0.4.1-tem`

Uninstall JDK

jbang jdk uninstall 14
jbang jdk uninstall 14

List JDKs

jbang jdk list
jbang jdk list

Set default JDK

jbang jdk default 12
jbang jdk default 12

java / javac Options

//JAVAC_OPTIONS -source 11
//JAVA_OPTIONS -Xms128m -Xmx512m
//JAVAC_OPTIONS -source 11
//JAVA_OPTIONS -Xms128m -Xmx512m

Install a JBang App

jbang app install CamelJBang@apache/camel
jbang app install CamelJBang@apache/camel

List Available Apps

jbang app list
jbang app list

[1] JBang will download the file – tagged with % – and pass it as argument to the Script

JBang CheatSheet

Conclusion

With this JBang Cheatsheet, you now have access to a concise and well-structured guide that covers the essential commands, syntax, and functionalities of jbang. Whether you are a developer, a student, or simply curious about this powerful Java-based scripting tool, this JBang Cheatsheet will empower you to streamline your scripting workflow, automate tasks, and boost productivity.

Found the article helpful? if so please follow us on Socials

Categories Java Post navigation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK