Aled
Aled

Reputation: 157

How to distribute a binary dependency in maven?

I'm trying to convert a project from ant to maven.

The unit tests depend on a third party binary jar, which is not available in any public maven repositories.

How do I make maven handle this situation? I have found two solutions, neither of which are acceptable. First is to use a system dependency; this doesn't work because a) the dependency should only be for the tests, and b) the dependency is not found by eclipse after generating an eclipse project.

Second is to manually install the dependency in a local repository. This seems to be the recommended way. I don't want to do this because I want users to be able to build and test with a simple 'mvn test'. If users have to read a document and copy/paste some shell commands to be able to build and test, then something's wrong.

I suppose it would be OK if maven itself installed the dependency in the local repository as part of the build - is this possible, and if so, how?

Aled.

Upvotes: 2

Views: 1883

Answers (2)

Cemo
Cemo

Reputation: 5570

First of all my way would be using a repository manager such as nexus and installing this dependency to there.

However there is another solution. You can include this 3rd party jar to your project and with test plugin you can configure to include it in classpath such this:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.10</version>
    <configuration>
      <additionalClasspathElements>
        <additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>
        <additionalClasspathElement>path/to/additional/jar</additionalClasspathElement>
      </additionalClasspathElements>
    </configuration>
  </plugin>

By the way, I hope that you are aware of that maven is executing surefire plugin in order to run tests by default lifecycle.

Upvotes: 1

Alex Gitelman
Alex Gitelman

Reputation: 24732

You may want to look at install:install-file. You can make it execute in the early phase of your project (validate or initialize) via standard means.

On the second thought, if it fails because of missing dependency in the same project, there are couple more options. One is to call ant script via antrun plugin to install artifact.

Or create additional module not dependent on your artifact to be executed prior to main module and have that module install artifact as described earlier.

Upvotes: 2

Related Questions