Creating Java CI with Maven
with GitHub Actions
In recent years, tools like Travis CI and CircleCI have set out to solve the problem behind the CI/CD building. Often confusing for devs who had to switch sites again and again, these tools created the need to be added to the repository page. Since 2019, GitHub Actions has support for CI/CD and now makes it easier to automate how we build, test, and deploy our projects. In this tutorial, we are going to build a CI/CD pipeline to deploy a Java application with TotalCross but you can use into your favorite framework/vanilla project. It's quite generic.
TotalCross is a cross-platform free open source GUI creator and will be very helpful for us in this tutorial. How about starting in just 8 minutes? See more about TotalCross, the fastest way to build GUI!
Let's get start
The next steps may be easier if you get TotalCross get started. After you create your Java with Maven project in any IDE of your choice and you upload the repository to GitHub, you only need to open the Actions dashboard:
Now let's set up our own workflow:
You can choose the name of your file. Let's call it ci.yml
:
Right below we can edit our pipeline descriptor file.
Your workflow file
I recommend copy and paste the following code:
name: Java CI with Maven
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Maven
run: mvn package
- name: Archive results
uses: actions/upload-artifact@v1
with:
name: artifacts
path: target/install
We need to talk about some lines to make it available to your customized options. Line-by-line:
Line | Word | Description |
---|---|---|
01 | name |
set the task title in the action pane |
03 | on |
and its subsequent lines define the triggers for starting the task, is required |
09 | jobs |
groups one or more tasks |
10 | build |
it's our only task, you could call it by some other name like apple-juice
|
12 | runs-on |
defines the host machine's operating system, with TotalCross you can generate applications for Windows, android, iOS and more |
14 | steps |
the next lines starting with - define items. For each item a uses orrun is required as "minimum requirement" |
Let's take a closer look at the steps
-
uses
a Marketplace action to checkout the project; -
uses
another community action to install the JDK. The version can be changed on line 20; - Runs the necessary command for Maven build. Switch to the command you already use on line 22, something like
mvn -B package --file pom.xml
; - The last step only exports the resulting artifacts. They can be used by other jobs. You can change the path to the artifacts on line 27.
We need to commit the changes:
Then we need to return to Actions:
Wait a few minutes until the work is finished and the artifacts are available:
Download the artifacts and test!
You might like
GitHub actions is a very powerful tool and is being improved very quickly, visit the documentation. Some interesting features are already available. For example you may have job related badges in the repository README.md:
You can also create jobs for Test and Publish your projects, your starting point can be run
commands (they behave like shell commands) or knowing some community actions like:
If you liked this article, please clap and share your comments below! Did you like the TotalCross project? Support us with a star!
Thanks a lot!
Top comments (1)
Ow, it's a very good tip! Before I created maven projects directly in the IDE (Eclipse) but now I'm using them directly on GitHub actions. I usually use it to create test projects for TotalCross, so they are simpler