Creating Java CI with Maven
Tags: java, ci, maven, github
Published: 2020-07-22 17:48:41

# 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](https://github.blog/2019-08-08-github-actions-now-supports-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](https://github.com/TotalCross/totalcross) but you can use into your favorite framework/vanilla project. It's quite generic.

![GitHub Actions CI/CD banner](https://github.blog/wp-content/uploads/2019/08/DL-V2-LinkedIn_FB.png)

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](https://totalcross.com/), the fastest way to build GUI! 

![TotalCross code sample GIF](https://totalcross.com/static/TotalCrossHelloWorld-1f0f67a1d4a91788980586e19a48a7d6.gif)

## Let's get start

The next steps may be easier if you get [TotalCross get started](https://learn.totalcross.com/documentation/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](https://i.imgur.com/99M30wg.jpg)

Now let's set up our own workflow:

![Imgur](https://i.imgur.com/bLWN0cb.jpg)

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

![Imgur](https://i.imgur.com/jroO3fI.jpg)

Right below we can edit our pipeline descriptor file.

## Your workflow file

I recommend copy and paste the following code:

```yaml
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` or` run` is required as "minimum requirement"

Let's take a closer look at the `steps`

 1. `uses` a [Marketplace](https://github.com/marketplace?type=actions) 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](https://i.imgur.com/X7lTCfe.jpg)

Then we need to return to *Actions*:

![Imgur](https://i.imgur.com/ECJvzZ7.jpg)

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

![Imgur](https://i.imgur.com/Og0c4Fn.jpg)

Download the artifacts and test! 

## You might like

GitHub actions is a very powerful tool and is being improved very quickly, visit the [documentation](https://docs.github.com/en/actions). Some interesting features are already available. For example you may have job related badges in the repository README.md:

![Imgur](https://i.imgur.com/vAtoxiY.jpg)

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:

- [AWS CloudFormation](https://github.com/marketplace/actions/aws-cloudformation-deploy-cloudformation-stack-action-for-github-actions)
- [Docker](https://github.com/marketplace/actions/build-and-push-docker-images)

If you liked this article, please clap and share your comments below! Did you like the TotalCross project? [Support us with a star](https://github.com/TotalCross/totalcross)!

Thanks a lot!