Automate Java (Maven) project build with CircleCI + Orbs

Introduction

I couldn't find a sample that explained the automation of development / build with Java (Maven) with CircleCI, so I tried it.

Premise

I will not explain about having a GitHub or CircleCI account, installing and setting VS Code or git, etc. in this article. Regarding GitHub account settings and CircleCI account settings, there is an article I wrote earlier, so I will list it here.

-Sign up on GitHub --New user registration -Sign up to CircleCI-New user registration

Java (Maven) project to be automated

I wrote

-[Create a Java (Maven) project with VS Code and develop it on a Docker container] (https://qiita.com/mfunaki/items/dc09764cb08bb787c261)

Let's use CircleCI to automate the build of the project mentioned in.

Part 1: Quickly automate for the time being

Using a web browser, click Go to App at the top right of CircleCI's Home Page (https://circleci.com/ja/) or access https://app.circleci.com/ directly. On the screen called Project, the Set Up Project button is displayed in the repository that is not registered as a CircleCI project even if it exists on GitHub, so the project being created this time (CCI-SMP-Java-Maven) Click the Set Up Project button next to it. Project Then, the recommended configuration file (config.yml) template will be displayed. This time, instead of selecting ** Maven (Java) ** and clicking the ** Commit and Run ** button, ** ↓ Click the button (Download) **. config.yml If your web browser asks where to save config.yml, create a .circleci directory in your local CCI-SMP-Java-Maven repository and store config.yml in it. I will. If you don't hear it, it's probably saved automatically in your download folder, so create a .circleci directory in your local CCI-SMP-Java-Maven repository and put it in your download folder. Make sure to copy or move the config.yml file in. When the change notification (U) of the config.yml file is displayed on Visual Studio Code, add, commit and push it. config.yml After adding, committing and pushing, go back to your web browser and click the ** Use Existing Config ** button. You will be asked Have you added a config.yml file ?, but you can click the ** Start Building ** button. Have you added a config.yml file? Then, the CI/CD process starts, but the specified user setting file does not exist: /home/circleci/project/pom.xml in Install Dependencies, so the pom.xml file used by Maven cannot be found. You can see that he is angry. Install Dependencies Looking at the Visual Studio Code Explorer, the pom.xml file exists, but not in the top-level directory, but in the my-app directory. Therefore, it seems good to specify this location. config.yml If you look at the 4th line of the config.yml file you added earlier, you can see that you are using maven Orb, so search for maven Orb from the list of CircleCI Orbs here (https://circleci.com/developer/ja/orbs). .. CircleCI Developer Clicking circleci/[email protected] (version number may vary with new version release) will bring up the maven Orb manual, so the parameters in the maven/test job * Specifying * app_src_directory ** seems to work. app_src_directory At the same time, set the version of circleci/maven on the 4th line to 1.0.3.

config.yml


version: 2.1

orbs:
  maven: circleci/[email protected]

workflows:
  maven_test:
    jobs:
      - maven/test: # checkout, build, test, and upload test results
          app_src_directory: my-app

I updated it, committed and pushed it and it worked! From the CCI-SMP-Java-Maven project, select the maven_test workflow and then the maven/test job and you should see something like this. maven/test At first glance, everything seems to work on the green, but there are (at least) two things that don't seem to work.

  1. There is no way to access the generated /home/circleci/project/my-app/target/my-app-1.0-SNAPSHOT.jar file.
  2. I can't see the test results. Looking inside ** Uploading test results **, I get an error because I can't save the test results. Uploading test results

Part 2 isn't just about going through the build (in green), it's getting the generated * .jar file so you can see the test results report.

Part 2: Receive test results and build results (* .jar)

test results

First of all, the test result, but it seems to work if you can specify the storage destination of the test result correctly. Maven Orb Manual In the middle of ** Usage Examples **, ** custom_test_results_path ** is If your tests results are not in the default (target/surefire-reports) directory then you could add Since it says a custom directory., I will add the description of test_results_path in addition to app_src_directory to the parameter of maven/test in config.yml.

config.yml


version: 2.1

orbs:
  maven: circleci/[email protected]

workflows:
  maven_test:
    jobs:
      - maven/test: # checkout, build, test, and upload test results
          app_src_directory: my-app
          test_results_path: my-app/target/surefire-reports

When I update, commit, and push this config.yml to see the screen on the CircleCI side, ** 1 ** in white is added to the TETS tab in green, and in ** Uploading test results ** The execution result is also output as Archiving the following test results. Uploading test results Click the TESTS tab and the test results will be displayed like this. Good Job

Build result

Using Maven Orb makes it easier to store test results (test_results_path), but it cannot be used in combination with saving artifacts (store_artifacts), so config.yml is stored without Maven Orb. Describe. That said, the Maven Orb source code is published here here, so it's very helpful to be able to refer to it.

config.yml


version: 2
jobs:
  build:
    docker:
      - image: cimg/openjdk:15.0.1
    steps:
      - checkout
      - run:
          command: find . -name 'pom.xml' | sort | xargs cat > /tmp/maven_cache_seed
          working_directory: ~/project/my-app
      - restore_cache:
          key: circleci-java-maven-{{ checksum "/tmp/maven_cache_seed" }}
      - run: 
          command: mvn package
          working_directory: ~/project/my-app          
      - save_cache:
          paths:
            - ~/.m2/repository
          key: circleci-java-maven-{{ checksum "/tmp/maven_cache_seed" }}
      - store_test_results:
          path: ~/project/my-app/target/surefire-reports
      - store_artifacts:
          path: ~/project/my-app/target/my-app-1.0-SNAPSHOT.jar

Now, I will explain it in several parts.

Specifying a Docker container image

In the previous article, I used a Microsoft-made image (mcr.microsoft.com/vscode/devcontainers/java:0-15), but here I used a CircleCI-made convenience image optimized for use with CircleCI (CircleCI-made convenience image). I am using cimg/openjdk: 15.0.1). A list of convenience images can be found at here.

config.yml


  build:
    docker:
      - image: cimg/openjdk:15.0.1

Generating seed values ​​for cache

I saw a lot of libraries downloaded during the build process of the above thing. The library to be downloaded is specified in pom.xml along with the library name and version, and is saved in the .m2/repository directory. In other words, if the contents of the pom.xml file have not been changed, using the contents of .m2/repository will reduce the download burden. Here, the contents of the pom.xml file (output by cat) are acquired in / tmp/maven_cache_seed.

config.yml


      - run:
          command: find . -name 'pom.xml' | sort | xargs cat > /tmp/maven_cache_seed
          working_directory: ~/project/my-app
      - restore_cache:
          key: circleci-java-maven-{{ checksum "/tmp/maven_cache_seed" }}
(Omission)
      - save_cache:
          paths:
            - ~/.m2/repository
          key: circleci-java-maven-{{ checksum "/tmp/maven_cache_seed" }}

Even if you don't do this roundabout way, you can put circleci-java-maven-{{checksum" ~/project/my-app/pom.xml "}} in the key of ** restore_cache ** or ** save_cache **. It seems like you can pass it, but the method implemented here will result in an error in this ** restore_cache ** step if pom.xml does not exist (following steps). Is not executed), so I think you are doing it this way (the source of the method is the Maven Orb source code).

Run mvn package

This is simple. As a reminder, you can't use the $ CIRCLE_WORKING_DIRECTORY environment variable in working_directory (more precisely in config.yml). For more information, see the description of working_directory in Documentation (I'm addicted to this, but when I saw that literally" $ CIRCLE_WORKING_DIRECTORY "was created at the SSH login destination, I was able to notice it right away!).

config.yml


      - run: 
          command: mvn package
          working_directory: ~/project/my-app          

Save test results

This is also simple.

config.yml


      - store_test_results:
          path: ~/project/my-app/target/surefire-reports

Save the generated JAR file to an artifact

This is also simple.

config.yml


      - store_artifacts:
          path: ~/project/my-app/target/my-app-1.0-SNAPSHOT.jar

The JAR file saved in the artifact in this way can be obtained from the ARTIFACTS tab on the CircleCI job screen. ARTIFACTS

in conclusion

I think that there are some points that were difficult to understand due to redundant or insufficient explanation, but if you go through it, if it is a Java (Maven) project, it may be possible to automate it by putting it on CircleCI, whether it is new or existing. Isn't it? I think you got the feeling of skin.

I would like to introduce other languages ​​(and build tools) like this. See you again!

Recommended Posts

Automate Java (Maven) project build with CircleCI + Orbs
Build a Java project with Gradle
CICS-Run Java applications-(2) Build management with Maven
[CircleCI 2.0] [Java] [Maven] [JUnit] Aggregate JUnit test results with CircleCI 2.0
Build Java with Wercker
Try gRPC with Java, Maven
How to add another project as Maven library with CircleCI and use it for build
CI for Maven project at CircleCI
Java build with mac vs code
Create a Maven project with a command
[Gradle] Build a Java project with a configuration different from the convention
CICS-Run Java applications-(3) Build management with Gradle
Quick build maven project using maven docker container
Build OpenCV with Java Wrapper on Ubuntu 18.04
Reduce Java / Maven build time with Nexus 3 in OpenShift (okd 3.11) DevOps environment
Automate integration testing with Maven Failsafe plugin
Build Apache and Tomcat environment with Docker. By the way, Maven & Java cooperation
Java automated test implementation with JUnit 5 + Apache Maven
Deploy Java web app to Azure with maven
Build and test Java + Gradle applications with Wercker
Build an E2E test environment with Selenium (Java)
Build Spring Boot project by environment with Gradle
Java Repository of Eclipse with Maven: Missing artifact ~
Build a Java development environment with VS Code
vagrant java build
java, maven memo
Create a Java (Maven) project with VS Code and develop it on a Docker container
Build Java development environment with VS Code on Mac
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
[Environment construction] Build a Java development environment with VS Code!
Build Java program development environment with Visual Studio Code
Until you build a project described in scala with Maven and execute it with the scala command.