Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve a maven dependency in a bitbucket pipeline that exists in another repo

I'm setting up a deployment pipeline for my companys mulesoft APIs using bitbucket-pipelines. This technology is attractive to us because of the built in integrations with Jira. The problem is that we are using a domain project. All of the other dependencies get downloaded from anypoint exchange, but the domain project cannot be hosted there, so I get this error:

[ERROR] Failed to execute goal on project sourceControlDemo: Could not resolve dependencies for project com.mycompany:sourceControlDemo:mule-application:1.0.0-SNAPSHOT: Could not find artifact com.mycompany:[mycompany]-domain:jar:mule-domain:1.0.0 in anypoint-exchange (https://maven.anypoint.mulesoft.com/api/v1/maven) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project sourceControlDemo: Could not resolve dependencies for project com.mycompany:sourceControlDemo:mule-application:1.0.0-SNAPSHOT: Could not find artifact com.mycompany:[mycompany]-domain:jar:mule-domain:1.0.0 in anypoint-exchange (https://maven.anypoint.mulesoft.com/api/v1/maven)

In our current process, which involves building the projects locally, the domain project is included in the workspace and this error does not occur.

It seems as though there are a few strategies here: * Create a custom docker image with the dependency included - This options seems overkill, and presents the biggest skill gap for me, as I have never used docker. * Host the domain project on a private maven repo to be referenced in the dependent project pom.xml - This seems like the "normal" way to do it. However, it again seems like overkill for one dependency. * Clone the domain project repo in the pipeline file, install the project in the local repository automatically - this is the option I'd really like to go with. I've managed to clone the repo and run mvn install in its root, but upon running mule deploy in the next step, it still cannot find the dependency. I'm not sure what the file structure really looks like in the pipeline, and can't figure out how to configure the pom to look in the right place.

My bitbucket-pipelines.yml file

image: maven:3.6.1-jdk-8

pipelines:
  branches:
    DEV:
        - step:
            name: install domain project
            trigger: automatic
            caches:
              - maven
            script:
              - apt-get update -y
              - apt-get install -y git
              - cd ..
              - git clone [email protected]:[mycompany]/[mycompany]-domain.git
              - cd [mycompany]-domain
              - mvn install
              - cd ../build
        - step:
            name: Build and Deploy
            trigger: automatic
            caches:
              - maven
            deployment: DEV
            script:
              - mvn deploy -e -DmuleDeploy

My pom.xml file for the non domain project

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>sourceControlDemo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>mule-application</packaging>

    <name>sourceControlDemo</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <app.runtime>4.1.5</app.runtime>
        <mule.maven.plugin.version>3.2.7</mule.maven.plugin.version>
    </properties>

    <build>
        <finalName>${artifactId}-${BITBUCKET_BUILD_NUMBER}</finalName>
        <plugins>
            <plugin>
                <groupId>org.mule.tools.maven</groupId>
                <artifactId>mule-maven-plugin</artifactId>
                <version>${mule.maven.plugin.version}</version>
                <extensions>true</extensions>
                <configuration>
                  <armDeployment>
                      <uri>https://anypoint.mulesoft.com</uri>
                      <target>${target}</target>
                      <targetType>${target_type}</targetType>
                      <username>${username}</username>
                      <password>${password}</password>
                      <environment>${environment}</environment>
                      <muleVersion>4.1.5</muleVersion>
                  </armDeployment>
                </configuration>
                <executions>
                  <execution>
                    <id>default-deploy</id>
                    <phase>deploy</phase>
                    <goals>
                      <goal>deploy</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>[mycompany]-domain</artifactId>
            <version>1.0.0</version>
            <classifier>mule-domain</classifier>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <repositories>
          <repository>
            <id>anypoint-exchange</id>
            <name>Anypoint Exchange</name>
            <url>https://maven.anypoint.mulesoft.com/api/v1/maven</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>mulesoft-releases</id>
            <name>MuleSoft Releases Repository</name>
            <url>https://repository.mulesoft.org/releases/</url>
            <layout>default</layout>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>mulesoft-releases</id>
            <name>mulesoft release repository</name>
            <layout>default</layout>
            <url>https://repository.mulesoft.org/releases/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

I've also tried this in the pom:

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>[mycompany]-domain</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <type>jar</type>
    <classifier>mule-domain</classifier>
    <systemPath>${basepath}/.m2/repository</systemPath>
</dependency>

and got this error

[ERROR] Failed to execute goal on project sourceControlDemo: Could not resolve dependencies for project com.mycompany:sourceControlDemo:mule-application:1.0.0-SNAPSHOT: Could not find artifact com.mycompany:[mycompany]-domain:jar:mule-domain:1.0.0 at specified path /root/.m2/repository -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project sourceControlDemo: Could not resolve dependencies for project com.mycompany:sourceControlDemo:mule-application:1.0.0-SNAPSHOT: Could not find artifact com.mycompany:[mycompany]-domain:jar:mule-domain:1.0.0 at specified path /root/.m2/repository

In case it's not clear, I've censored my company's name.

The expected result is for the project to successfully build and deploy.

like image 446
Shane Chambry Avatar asked Oct 31 '19 20:10

Shane Chambry


People also ask

What does Mvn dependency resolve do?

Description: Goal that resolves the project dependencies from the repository. When using this goal while running on Java 9 the module names will be visible as well.

How do I use Maven with Bitbucket pipeline?

When you run the Maven release plugin (ie. mvn release:prepare release:perform), maven will create git commits and attempt to push them back to your source code repository. A Bitbucket Pipeline does not have the permissions to push to a repository so we have to enable this by adding an SSH key associated with a Pipeline.

How does Bitbucket Pipelines work with dependencies?

Bitbucket Pipelines runs your builds in a Docker container that provides the build environment. There are a few ways you can specify any dependencies that your build needs in that container – we recommend running dependencies as services in their own containers.

Is bitbucket a Maven repository?

Bitbucket is not a maven repository. You should use such repositories as Nexus, Artifactory and so on. You must be a registered user to add a comment.

How do I push a Bitbucket pipeline to a git repository?

A Bitbucket Pipeline does not have the permissions to push to a repository so we have to enable this by adding an SSH key associated with a Pipeline. We need a key pair to allow Pipelines to git push back to this repo when we are releasing an artifact via a mvn release:perform command.


2 Answers

You can follow this article, to achieve it. cloud-repo-and-bitbucket-example

Here is one way to do it. Works for me:

  • Add the settings.xml file and change your cloud repo user and password for bitbucket secure variables.
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
      https://maven.apache.org/xsd/settings-1.0.0.xsd"

    <!-- Complete Documentation for the settings.xml file can be found at https://maven.apache.org/settings.html -->
    <activeProfiles>
        <activeProfile>example</activeProfile>
    </activeProfiles>

   <profiles>
        <profile>
            <id>example</id>
            <repositories>
                <repository>
                    <id>${private_repo_id}</id>
                    <name>Example Java Repository</name>
                    <url>${private_repo_url}</url>
                </repository>
            </repositories>
        </profile>
    </profiles>
    <servers>
        <server>
            <id>io.cloudrepo</id>
            <username>${prived_user_repo}</username>
            <password>${prived_password_repo}</password>
        </server>
    </servers>
</settings>
  • Add those variables on your bitbucket pipeline

bitbucket-pipeline-security-variables

  • Add to your bitbucket-pipelines.yml the maven step including the setting.xml
image: maven:3.6.1

pipelines:
  default:
    - step:
        caches:
          - maven
        script:
          - mvn -s settings.xml -B verify
like image 154
datruq Avatar answered Oct 17 '22 05:10

datruq


This following works when an external repository is not the preferred approach.

  1. Create a key pair in the repository which should consume the other dependency (the main project): https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/

  2. Add the public key as access key to the projects which is the dependency (the dependency): https://confluence.atlassian.com/bitbucket/use-access-keys-294486051.html

  3. Configure the build pipeline based on the maven Docker image (adjust to the latest version provided by Bit Bucket). Pay attention to the first step, which is not executed in parallel building the necessary dependency before the main project is built.

image: maven:3.6.3

pipelines:
  default:
    - step:
        name: Build Local Dependencies
        caches:
          - maven
        script:
          - git clone [email protected]:<user>/<project>.git
          - cd <project>
          - mvn clean install
          - cd ..
    - parallel:
      - step:
          name: Build and Test
          caches:
            - maven
          script:
            - mvn -B verify --file pom.xml
          after-script:
              # Collect checkstyle results, if any, and convert to Bitbucket Code Insights.
            - pipe: atlassian/checkstyle-report:0.3.0
      - step:
          name: Security Scan
          script:
            # Run a security scan for sensitive data.
            # See more security tools at https://bitbucket.org/product/features/pipelines/integrations?&category=security
            - pipe: atlassian/git-secrets-scan:0.5.1
like image 39
k_o_ Avatar answered Oct 17 '22 03:10

k_o_