DEV Community

Allan C. M. Lira
Allan C. M. Lira

Posted on

Creating Java CI with Maven

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.

GitHub Actions CI/CD banner

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!

TotalCross code sample GIF

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:

Imgur

Now let's set up our own workflow:

Imgur

You can choose the name of your file. Let's call it ci.yml:

Imgur

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
Enter fullscreen mode Exit fullscreen mode

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

  1. uses a Marketplace action to checkout the project;
  2. uses another community action to install the JDK. The version can be changed on line 20;
  3. 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;
  4. 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:

Imgur

Then we need to return to Actions:

Imgur

Wait a few minutes until the work is finished and the artifacts are available:

Imgur

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:

Imgur

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)

Collapse
 
vaneskasousa profile image
Vaneska Sousa

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