Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download maven project dependencies to build offline later

Tags:

java

maven

I've used maven for a while in common way with settings out of the box, so I understand what it is but I'm newbie in maven settings.

I need to organize such workflow:

  1. Developer writes java code, use some dependencies from internet.
  2. Developer commits his work.
  3. TeamCity automatically can build his work. Without any manual work, and without internet.

I have idea how to do it:

  1. Developer uses maven. A "common" directory acts as repository for certain java projects.
  2. After the work is complete, the developer commits his project and common directory into svn.
  3. TeamCity updates project and common directory from svn and run "mvn package". Anything needs takes from common directory. Without worrying about internet connection and startup nexus, or other repo services.

My question is: How to use simple directory on filesystem as proxy repository for certain projects? Tell me please how to realize this idea or give me another idea to realize such workflow.

I can just commit local repository, but there are some limitations:

  1. Local repo zip artifacts. If I make even little changes to it - the whole cache file must be uploaded and downloaded to/from svn. It takes a long time.
  2. Local repo store artifacts for all projects. I want only certain projects to use this repo, because developers don't want to check changes and filter unused dependencies.

I test local directory to deploy projects, simple by writing "file://testRespoDir" in repo url, but I can't understand how to make this directory proxy all remote artefacts for project(project must not use local repo and use only common directory.

like image 569
cynepnaxa Avatar asked Dec 13 '13 05:12

cynepnaxa


People also ask

How do I download Maven dependencies to local?

You can use the Maven Dependency Plugin to download dependencies. Run mvn dependency:copy-dependencies , to download all your dependencies and save them in the target/dependency folder. You can change the target location by setting the property outputDirectory .

What is mvn dependency go-offline?

The go-offline goal of the Maven Dependency plugin downloads all the required dependencies and plugins for the project, based on the pom file. The –o option tells Maven to work offline and not check the Internet for anything.

Does Maven download dependencies of dependencies?

When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository. It helps to avoid references to dependencies stored on remote machine every time a project is build.

Does Maven automatically download compile dependencies?

Maven's local repository is a directory on the local machine that stores all the project artifacts. When we execute a Maven build, Maven automatically downloads all the dependency jars into the local repository. Usually, this directory is named . m2.


3 Answers

I found simple and perfect solution: POM include 2 repositories:

<repositories>
    <repository>
        <id>commonDep</id>
        <name>common dependency</name>
        <url>file://../common/repository</url>
    </repository>
    <!-- it used when collect dependencies before commit. If developer already download dependency, get it from local repo, save traffik and time -->
    <repository>
        <id>localPlugins</id>
        <name>local plugins</name>
        <url>file://${user.home}/.m2/repository</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>commonDep</id>
        <name>common dependency</name>
        <url>file://../common/repository</url>
    </pluginRepository>
    <!-- it used when collect dependencies before commit. If developer already download dependency, get it from local repo, save traffik and time -->       
    <pluginRepository>
        <id>localPlugins</id>
        <name>local plugins</name>
        <url>file://${user.home}/.m2/repository</url>
    </pluginRepository>
</pluginRepositories>

when developer opens project, he use local, common and central repositories in such order. It saves his traffic. When he finished work, he call script:

mvn dependency:go-offline -Dmaven.repo.local=../common/repository

all current dependencies of project copyed from his default local repository to common repository. Then developer commit common.

When we run build on teamcity, we checkout project and common and run it. There is no internet connection, but all dependencies exist in common repository.

like image 78
cynepnaxa Avatar answered Sep 29 '22 13:09

cynepnaxa


Yeah you can do this personally i wouldn't recommend it especially if you're using SNAPSHOT's however you should be able to.

So what you want to do is create a network drive (i dont know whether your on windows or linux but it dont matter).

Then mount that network drive on all systems which require it.

Then in maven config file specify the local maven repo location:

    <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
                      http://maven.apache.org/xsd/settings-1.0.0.xsd" >

       <localRepository>c:/mvn-repo/</localRepository>
        ...
   </settings>

Replace c:/mvn-repo/ with path to your the directory on the network drive you wish to use.

You can place this config in various places but i would suggest placing it in the root config file which lives @ ${MAVEN_HOME}/conf/settings.xml.

You will need to specify this on each computer which is using maven.

Then that should do it and all your maven run times will share the same local repo.

So how to get round different projects using different directories? Thats a tricky one you could use different directories on network drive and change the localRepository variable @ run time by specifying it as a runtime property.

mvn -Dmaven.repo.local=$HOME/.my/other/repository clean install

That way you would have it all parceled up nicely one network drive with a directory for each project then simply specify that variable @ run time to set which local repo to use.

like image 25
Tom Avatar answered Sep 29 '22 14:09

Tom


The flow you propose won't scale. I would rather set up a local corporate mirror of the central repository and have both developers and automation servers (teamcity etc.) use it. Trivial to set up, easy to maintain, easy to track dependencies on the third party software and put restrictions in place.

like image 34
bobah Avatar answered Sep 29 '22 12:09

bobah