Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mounting Maven Repository to Docker

I am trying to build a Java application and make a package using docker. This builds needs a maven repository which I don't want to include in the image, since it's very large. I wanted to try using volumes and mount my local maven repository to the maven repository in the image. I used apt-get install -y maven in order to have maven available, but I can't find the directory .m2 in the image $HOME.

I used ls -la $HOME, ls -la and ls -la /root to find the maven home, but there is no .m2 directory there.

EDIT 1:

I have these lines in Dockerfile:

FROM ubuntu
MAINTAINER Zeinab Abbasimazar
# Install and configure required packages
RUN apt-get update; \
    apt-get install -y --no-install-recommends apt-utils; \
    apt-get install -y dialog; \
    apt-get install -y wget unzip curl maven; \
    mkdir $HOME/.m2/; \
    ls -la /usr/share/maven/conf/; \
    echo \
    "<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'> \
        <localRepository>/root/.m2/repository</localRepository> \
        <interactiveMode>true</interactiveMode> \
        <usePluginRegistry>false</usePluginRegistry> \
        <offline>false</offline> \
    </settings>" \
    > /usr/share/maven/conf/settings.xml
VOLUME ["/home/zeinab/.m2/", "/root/.m2/"]
# Build
RUN mvn  -X clean install -pl components -P profile

Which puts local repository configurations in image's maven configuration file, mounts my local maven repository to a directory in the image and finally performs the build. As I can see in the maven build log that it's using the local repository path I expected:

[DEBUG] Reading global settings from /usr/share/maven/conf/settings.xml
[DEBUG] Reading user settings from /root/.m2/settings.xml
[DEBUG] Using local repository at /root/.m2/repository

But still can't detect dependencies.

like image 910
Zeinab Abbasimazar Avatar asked Aug 13 '17 13:08

Zeinab Abbasimazar


People also ask

Is docker and Maven same?

Docker belongs to "Virtual Machine Platforms & Containers" category of the tech stack, while Apache Maven can be primarily classified under "Java Build Tools". Some of the features offered by Docker are: Integrated developer tools.


2 Answers

I finally found the solution for mounting my local maven repository in docker. I changed my solution; I am mounting it in the run phase instead of build phase. This is my Dockerfile:

FROM ubuntu
MAINTAINER Zeinab Abbasimazar
ADD gwr $HOME
RUN apt-get update; \
    apt-get install -y --no-install-recommends apt-utils; \
    apt-get install -y wget unzip curl maven git; \
    echo \
    "<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'> \
        <localRepository>/root/.m2/repository</localRepository> \
        <interactiveMode>true</interactiveMode> \
        <usePluginRegistry>false</usePluginRegistry> \
        <offline>false</offline> \
    </settings>" \
    > /usr/share/maven/conf/settings.xml; \
    mkdir /root/.m2/; \
    echo \
    "<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'> \
        <localRepository>/root/.m2/repository</localRepository> \
        <interactiveMode>true</interactiveMode> \
        <usePluginRegistry>false</usePluginRegistry> \
        <offline>false</offline> \
    </settings>" \
    > /root/.m2/settings.xml
WORKDIR .
CMD mvn  -X clean install -pl components -P profile

At first, I build the image using above Dockerfile:

sudo docker build -t imageName:imageTag .

Then, I run a container as below:

sudo docker run -d -v /home/zeinab/.m2/:/root/.m2/ --name containerName imageName:imageTag
like image 181
Zeinab Abbasimazar Avatar answered Nov 09 '22 23:11

Zeinab Abbasimazar


You don't find the ~/.m2 directory because it is created only when needed, i.e. when you store libraries in the local repository or when you add a config file.

You can create the ~/.m2 directory yourself and create your own settings.xml inside. There you can define the emplacement of the local repository:

<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">
  ...
  <localRepository>/path/to/local/repo/</localRepository>
  ...
</settings>

Read the documentation for more details.

like image 45
Ortomala Lokni Avatar answered Nov 10 '22 00:11

Ortomala Lokni