Maven POM file for installing multiple 3rd party commercial libraries

37,288

Solution 1

Recently discovered a new solution to this. Basically you can create a local repository within the project which can be checked in with the rest of the source code. Blogged about it here: http://www.geekality.net/?p=2376.

The gist is to deploy dependencies to a folder in your project.

mvn deploy:deploy-file
    -Durl=file:///dev/project/repo/
    -Dfile=somelib-1.0.jar
    -DgroupId=com.example
    -DartifactId=somelib
    -Dpackaging=jar
    -Dversion=1.0

And then simply let Maven know about it and use dependency declarations as normal through your pom.xml.

<repositories>
    <repository>
        <id>project.local</id>
        <name>project</name>
        <url>file:${project.basedir}/repo</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.example</groupId>
    <artifactId>somelib</artifactId>
    <version>1.0</version>
</dependency>

Not extremely Maven'y, but it works and moving the dependencies to a company repository later should be quite simple.

Solution 2

You can just create pom.xml with multiple executions of install-file goal of Maven install plugin. Assuming those files are already available locally somewhere (or you can download them using Wagon plugin).

  <project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.somegroup</groupId>
    <artifactId>my-project</artifactId>
    <version>1.0</version>

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.4</version/>
          <executions>
            <execution>
              <id>install1</id>
              <phase>package</phase>
              <goals>
                <goal>install-file</goal>
              </goals>
              <configuration>
                <file>lib/your-artifact-1.0.jar</file>
                <groupId>org.some.group</groupId>
                <artifactId>your-artifact</artifactId>
                <version>1.0</version>
                ... other properties
              </configuration>
            </execution>
            <execution>
              <id>install2</id>
              <phase>package</phase>
              <goals>
                <goal>install-file</goal>
              </goals>
              ... etc

            </execution>
            ... other executions
          </executions>
        </plugin>
      </plugins>
    </build>
  </project>

So, with above pom fragment mvn package should do the trick.

There are good Maven POM tutorial and POM reference.

Solution 3

Just to add to the correct example provided by @eugene-kuleshov:

  1. Once you configure the maven-install-plugin with the goal install-file in your pom.xml file with multiple executions, one execution per external jar, you have to use these jars in your pom.xml as usual:

    <dependency>
        <groupId>org.some.group</groupId>
        <artifactId>your-artifact</artifactId>
        <version>1.0</version>
    </dependency>
    

    The maven-install-plugin only copies your external jars to your local .m2 maven repository. That's it. It doesn't automatically include these jars as maven dependencies to your project.

    It's a minor point, but sometimes easy to miss.

  2. You do not need to include any <repositories> in your pom as long as you are installing the external jars to the .m2 repository (which is the default)

Share:
37,288

Related videos on Youtube

Lemon
Author by

Lemon

Software Developer, Geek, HSP, SDA, ..., open, honest, careful, perfectionist, ... Currently into indoor rowing and rock climbing, just to mention something non-computer-related... Not the best at bragging about myself... so... not sure what more to write... 🤔

Updated on July 24, 2022

Comments

  • Lemon
    Lemon almost 2 years

    I have a bunch of projects which are dependant on a set of commercial 3rd party libraries. We currently don't have a company repository so I have to install the libraries in my own local repo.

    Running mvn install:installFile -Dpackaging=jar -Dfile=<file> -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> for each file is rather tedious. Could create a bat file, but is there a way to do this using maven?

    I'm thinking a project for all the jars and a single pom file with all the group ids, artifact ids, versions and filenames and then the possibility of just running mvn install in that project, or something along those lines.

    Is anything like this possible?


    Note: I'm using Maven 3, but a Maven 2 compatible solution would be nice too.

  • Lemon
    Lemon over 11 years
    Say you have the pom.xml and a folder called lib where all the jar files are. Could you create an example?
  • Lemon
    Lemon over 11 years
    Sorry, I'm a complete newb in this area. What would the rest of the file look like? With the pom.xml being as minimum as possible.
  • Phillip Scott Givens
    Phillip Scott Givens about 11 years
    This pom.xml file only installs one artifact. The question calls for multiple artifacts.
  • Eugene Kuleshov
    Eugene Kuleshov about 11 years
    Obviously, example shows an incomplete pom. I've copied corresponding sections to make it more obvious... BTW, someone, please accept the answer.
  • Aliaksandr Arashkevich
    Aliaksandr Arashkevich almost 7 years
    So we do this, but now Maven is looking into a different repository than the local one. How do we force it to look in the local repo?
  • jesjimher
    jesjimher about 6 years
    In my case deploy-file had some problems with corporate proxy, but mvn install:install-file with -DlocalRepositoryPath=/dev/project/repo achieved the same result