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.
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 **.
After a lot of trial and error in various ways, I think the following method is the simplest and easiest to do.
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>
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"
#...#
#...#
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)
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.
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".
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
--quite
or -q
to wget, the token may be output to the console, so be sure to add it [^ silent-fetch].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
# ... #
Let's welcome the long-awaited screen!
Recommended Posts