How to add another project as Maven library with CircleCI and use it for build

background

Under one parent Maven project, there are two child Maven projects, Project A and Project B. Project A and Project B have been added to CircleCI individually through GitHub, so they are considered irrelevant projects (ie separate repositories). However, since project A uses the functions of project B, there is a dependency on project B.

Task

In the local environment, the sibling relationship between project A and B can be confirmed via the parent Maven project, so you can build by simply adding project B as dependency to project A's pom.xml. However, it is considered an irrelevant project on CircleCI, so Could not resolve dependencies for project can occur. ** I want to eliminate the dependency dependency without polluting the pom.xml of Project A **.

Solution

After a lot of trial and error in various ways, I think the following method is the simplest and easiest to do.

  1. Output the build artifact of project B as artifact
  2. On the project A side, download the package B through the CircleCI API.
  3. Install B as an external library through Maven installation before CircleCI dependencies

Detailed procedure

B. Introduced Maven Assembly Plugin in Project B

Put the following code in project B's pom.xml. If you don't use this, there is a possibility that the library used by project B will not be included in the jar [^ fat-jar].

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <!-- ... -->
  <build>
    <plugins>
      <!-- ... -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.5</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>assemble-all</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <!-- ... -->
    </plugins>
  </build>
  <!-- ... -->
</project>

B. Designate the deliverable as artifact in Project B

You need to specify the file you want to output to CircleCI in artifact. The way is to add the following code to circle.yml.

#...#
general:
    #...#
    artifacts:
        #This file name is maven-assembly-Seems to be the default for plugins
        - "target/<file name>-<version>-jar-with-dependencies.jar"
    #...#
#...#

C. Commit project B and confirm successful build

Unless something strange happens, you should succeed. By the way, if you specify the artifact as artifact, you can also download it from the CircleCI build page. (In the artifacts item)

D. Issue an access token

If it's a private project, you'll need an access token to download the artifact. Access tokens can be issued by selecting "ACCOUNT SETTINGS"-> "Personal API Tokens"-> "Create New Token" on CircleCI.

circleci-access-token.PNG

E Add an access token to the environment variable of Project A

If you can issue a token, put it in the environment variable of project A because you will use the API in the build of project A and download it. The location is "PROJECT SETTINGS"-> "Environment Variables"-> "Add Variable".

circleci-add-environment-variables.PNG

Create a script for adding dependency

Create a file called ʻadd_dependencies.sh` and put the following contents (the project name is project_b, please replace the part surrounded by <> with an appropriate value). The library called jq added in the first line is used to decipher the Json response returned from CircleCI [^ jq-lib].

#!/bin/sh
sudo apt-get install jq
currentDirectory=$(pwd)
echo $currentDirectory
downloadUrl=$(curl -sS "https://circleci.com/api/v1.1/project/github/<User / organization name>/project_b/latest/artifacts?circle-token=$MY_ACCESS_TOKEN&branch=<Branch name>" | jq '.[] | .url' | grep project_b.*with-dependencies\.jar | sed -e "s/\"//g")
echo $downloadUrl
sudo wget $downloadUrl?circle-token=$MY_ACCESS_TOKEN -O project_b.jar --quiet
mvn install:install-file -Dfile=$currentDirectory/project_b.jar -DgroupId=<Group ID> -DartifactId=project_b -Dversion=<version> -Dpackaging=jar -DgeneratePom=true

Added a script to the CircleCI execution procedure

Once the script is complete, add a command to run it in project A's circle.yml. The timing of execution is before CircleCI resolves the dependency.

# ... #
dependencies:
    pre:
        - sudo chmod a+x add_dependencies.sh
        - ./add_dependencies.sh
# ... #

Commit project A and confirm successful build

Let's welcome the long-awaited screen! circleci-success.PNG

Reference

Recommended Posts

How to add another project as Maven library with CircleCI and use it for build
Automate Java (Maven) project build with CircleCI + Orbs
How to build API with GraphQL and Rails
How to use nginx-ingress-controller with Docker for Mac
How to build docker environment with Gradle for intelliJ
Use Maven to add your favorite Java library to your environment.
How to use Truth (assertion library for Java / Android)
How to use JDD library in Scala with Eclipse
How to use RealSense with ubuntu 20.04 and ROS Noetic
How to create a header or footer once and use it on another page
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
How to use a foreign key with FactoryBot ~ Another solution
How to read a library from a JAR file with VS Code << How to not use Maven / Gradle >>
How to use Font Awesome icon for ul and li
Until you build a project described in scala with Maven and execute it with the scala command.
Procedures for passing RealmObject to Fragment and how to use Parceler
How to use \ t Escape sequences different for mac and Windows-java
[Tips] How to solve problems with XCode and Swift for beginners
How to read a file and treat it as standard input
Steps to install Maven on Mac and use it in Eclipse
How to use MinIO with the same function as S3 Use docker-compose
[For those who create portfolios] How to use binding.pry with Docker
How to use StringBurrer and Arrays.toString.
CI for Maven project at CircleCI
How to use EventBus3 and ThreadMode
How to use equality and equality (how to use equals)
How to use mssql-tools with alpine
How to make a Maven project
The story of making it possible to build a project that was built by Maven with Ant
Add the pre-built jar library to Android and call it in the framework
[Swift] How to connect TabBar with Storyboard Reference and also use NavigationController
How to use args :, environment :, env_file: and .env files with docker-compose command
(For beginners) [Rails] Time saving tech! How to install and use slim
Automatically assign date and build number for the day to Maven version
Is it possible to put the library (aar) in the Android library (aar) and use it?
Use MyBatis to get Map with key as identifier and value as Entity
How to add local jar to maven pom.xml
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to create a Maven repository for 2020
How to set up and use kapt
How to build SquashTM and how to support Japanese
[Ruby] How to use slice for beginners
How to set JAVA_HOME with Maven appassembler-maven-plugin
[Java] [Maven3] Summary of how to use Maven3
How to use substring and substr methods
How to use @Builder and @NoArgsConstructor together
How to build Rails 6 environment with Docker
How to use ToolBar with super margin Part1 Set characters and change colors
Thinking about how to use Ionic-Native, Hybrid and HTML5 for mobile app production
How to convert a value of a different type and assign it to another variable
I'm making an Android app and I'm stuck with errors and how to solve it
How to set up computer vision for tracking images and videos with TrackingJs
How to use Eclipse on my PC with 32bit and 2GB of memory
[Java] How to test for null with JUnit
[Java] How to use FileReader class and BufferedReader class
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
[Ruby] How to use gsub method and sub method
How to use an array for HashMap keys
How to use Java framework with AWS Lambda! ??