103

in my J2EE project I've a couple of dependencies, which are not available in any Maven repository, because they're proprietary libraries. These libraries need to be available at runtime, so that have to be copied to target/.../WEB-INF/lib ...

Right now, I'm listing them as system dependency in my POM, but with this method the problem is, that aren't being copied to the target build during compilation. Also this method is not very elegant.

So which is the best way to integrate them in Maven?

Note: I don't want to create my own Maven repository.

samson
  • 1,623
  • 3
  • 13
  • 10
  • 7
    I _really_ think you should review your decision of creating a Maven repository. It's not hard at all and 100% worth it. – Robert Munteanu Jul 22 '09 at 20:47
  • 7
    The value of creating a Maven repository is dependent on the circumstances. – yincrash Mar 18 '12 at 06:23
  • 1
    possible duplicate of [Can I add jars to maven 2 build classpath without installing them?](http://stackoverflow.com/questions/364114/can-i-add-jars-to-maven-2-build-classpath-without-installing-them) – Jesse Glick Feb 06 '14 at 15:59

10 Answers10

199

For people wanting a quick solution to this problem:

<dependency>
  <groupId>LIB_NAME</groupId>
  <artifactId>LIB_NAME</artifactId>
  <version>1.0.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/WebContent/WEB-INF/lib/YOUR_LIB.jar</systemPath>
</dependency>

just give your library a unique groupID and artifact name and point to where it is in the file system. You are good to go.

Of course this is a dirty quick fix that will ONLY work on your machine and if you don't change the path to the libs. But some times, that's all you want, to run and do a few tests.

EDIT: just re-red the question and realised the user was already using my solution as a temporary fix. I'll leave my answer as a quick help for others that come to this question. If anyone disagrees with this please leave me a comment. :)

Ric Jafe
  • 2,321
  • 2
  • 18
  • 18
  • 4
    Cool. Is there a way to specify a URL instead of a local machine path? – phreakhead Jan 18 '13 at 01:26
  • 3
    Thank you for a complete and working example!!! I hope you catch a gold fish and it grants you three wishes. :) – johndodo Jun 04 '13 at 12:48
  • 14
    This should be the accepted answer as it is self-contained(in pom.xml) – Vikram Aug 07 '13 at 16:53
  • epic, this helped me do a Maven install for a project which requires openbeans (silly Android) – slinden77 Dec 25 '13 at 16:42
  • if the jar you are trying to import is not built using maven, then how will get its group id or artifact id? – Rajarshee Mitra Oct 04 '15 at 14:55
  • 3
    Hi Rajarshee, the group and artifact ids are completely arbitrary. The path to your jar in the file system is all that matters for this to work. Because in reality the jar is not a maven dependency you are only telling Maven to treat it like one. – Ric Jafe Oct 05 '15 at 17:13
  • I was looking for this! – yerlilbilgin Apr 23 '16 at 11:20
  • 1
    As of 2016 this does not seem to work. The build acknowledges the path but does not include the code in the shaded jar. Rats... – markthegrea Jul 01 '16 at 14:47
  • Let's say your jar file is the output from another project in your workspace that you're actively developing simultaneously. Its output would go to a target folder as a jar. You might want to copy that jar to a more appropriate folder for the POM to pick it up from as you have in this solution. Is there some way to do this during a maven build? – Mike Oct 01 '17 at 14:57
  • This still works if you change the variable from `${basedir}` to `${project.basedir}` and then provide the correct path to the DLL. Only problem I have now is getting Maven to include the jar in the `WEB-INF/lib` directory during build. – JRSofty Feb 13 '18 at 08:23
  • @JRSofty That's explicitly what `system` is supposed to do. [Define a custom repository](https://stackoverflow.com/a/11320920/1030702) instead (also, use `file:///` with 3 slashes). – Bob May 08 '18 at 07:26
56

As you've said you don't want to set up your own repository, perhaps this will help.

You can use the install-file goal of the maven-install-plugin to install a file to the local repository. If you create a script with a Maven invocation for each file and keep it alongside the jars, you (and anyone else with access) can easily install the jars (and associated pom files) to their local repository.

For example:

mvn install:install-file -Dfile=/usr/jars/foo.jar -DpomFile=/usr/jars/foo.pom
mvn install:install-file -Dfile=/usr/jars/bar.jar -DpomFile=/usr/jars/bar.pom

or just

mvn install:install-file -Dfile=ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0 -Dpackaging=jar

You can then reference the dependencies as normal in your project.

However your best bet is still to set up an internal remote repository and I'd recommend using Nexus myself. It can run on your development box if needed, and the overhead is minimal.

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
Rich Seller
  • 83,208
  • 23
  • 172
  • 177
  • 1
    Thanks this method is working good for me now. I extended the POM so that files are being automatically being installed to the local rep: http://pastebin.ca/1504318 – samson Jul 23 '09 at 13:12
  • Glad to help, but as others have said the best approach would be to use a Maven repository manager. This approach does not scale well. – Rich Seller Jul 23 '09 at 13:35
  • 4
    The command that worked for me: "mvn install:install-file -Dfile=cassandra-jdbc-1.1.1.jar -DgroupId=org.apache-extras.cassandra-jdbc -DartifactId=cassandra-jdbc -Dversion=1.1.1 -Dpackaging=jar" – Mazrick Jun 30 '12 at 03:14
27

Create a repository folder under your project. Let's take

${project.basedir}/src/main/resources/repo

Then, install your custom jar to this repo:

mvn install:install-file -Dfile=[FILE_PATH] \
-DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] \ 
-Dpackaging=jar -DlocalRepositoryPath=[REPO_DIR]

Lastly, add the following repo and dependency definitions to the projects pom.xml:

<repositories>
    <repository>
        <id>project-repo</id>
        <url>file://${project.basedir}/src/main/resources/repo</url>
    </repository>
</repositories>

<dependencies>    
    <dependency>
        <groupId>[GROUP]</groupId>
        <artifactId>[ARTIFACT]</artifactId>
        <version>[VERS]</version>
    </dependency>
</dependencies>
mostar
  • 4,723
  • 2
  • 28
  • 45
  • 1
    This worked very well for me. PS I would recommend a different directory for the local repo as putting it within "src" didn't seem right (it's not source!) – davidfrancis Feb 03 '17 at 17:25
  • 1
    It worked like a charm. I am migrating an old project from Ant to Maven and there are a few libs I can't find in web repos. – Paulo Pedroso Mar 16 '17 at 23:19
  • 1
    Caution: when the value for -DlocalRepositoryPath does not end in `/` (i.e., indicating a folder), then he repo is created with groupId. Ex: if group id is `foo.com` and localRepositoryPath is `repo`, then the repo folder becomes `repo.foo.com`. – Sheshadri Mantha Nov 13 '17 at 17:18
  • This procedure is as a project local to system repository management using maven build automation tool used primarily for Java projects and the file system. – Oleksii Kyslytsyn Jan 02 '20 at 14:19
8

You need to set up a local repository that will host such libraries. There are a number of projects that do exactly that. For example Artifactory.

Gregory Mostizky
  • 7,231
  • 1
  • 26
  • 29
  • 4
    A point on terminology, those are internal remote repositories, not local repositories. The local repository is the one on the local file system dependencies are downloaded to. – Rich Seller Jul 22 '09 at 11:11
3

None of the solutions work if you are using Jenkins build!! When pom is run inside Jenkins build server.. these solutions will fail, as Jenkins run pom will try to download these files from enterprise repository.

Copy jars under src/main/resources/lib (create lib folder). These will be part of your project and go all the way to deployment server. In deployment server, make sure your startup scripts contain src/main/resources/lib/* in classpath. Viola.

Apurva Singh
  • 4,534
  • 4
  • 33
  • 42
2

you can install them in a private, local repository (e.g. .m2/repository under your home directory): more details here

dfa
  • 114,442
  • 31
  • 189
  • 228
  • 7
    Yes I could do that, but then I'd have to tell everyone who wants to build the project that he has to put files X, Y in directory Z and that really complicates things. After all I want to use Maven to simplify the build process. – samson Jul 22 '09 at 09:40
2

If I am understanding well, if what you want to do is to export dependencies during the compilation phase so there will be no need to retrieve manually each needed libraries, you can use the mojo copy-dependencies.

Hope it can be useful in your case (examples)

ipingu
  • 307
  • 1
  • 5
  • 13
2

@Ric Jafe's solution is what worked for me.

This is exactly what I was looking for. A way to push it through for research test code. Nothing fancy. Yeah I know that that's what they all say :) The various maven plugin solutions seem to be overkill for my purposes. I have some jars that were given to me as 3rd party libs with a pom file. I want it to compile/run quickly. This solution which I trivially adapted to python worked wonders for me. Cut and pasted into my pom. Python/Perl code for this task is in this Q&A: Can I add jars to maven 2 build classpath without installing them?

def AddJars(jarList):
  s1 = ''
  for elem in jarList:
   s1+= """
     <dependency>
        <groupId>local.dummy</groupId>
        <artifactId>%s</artifactId>
        <version>0.0.1</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/manual_jars/%s</systemPath>
     </dependency>\n"""%(elem, elem)
  return s1
Community
  • 1
  • 1
Paul
  • 7,155
  • 8
  • 41
  • 40
  • Above solution returns 'dependencies.dependency.systemPath' for xx.jar should not point at files within the project directory. See http://stackoverflow.com/questions/10935135/maven-and-adding-jars-to-system-scope – sarah.ferguson Dec 09 '15 at 14:37
1

Continue to use them as a system dependency and copy them over to target/.../WEB-INF/lib ... using the Maven dependency plugin:

http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html

Corehpf
  • 560
  • 1
  • 6
  • 14
1

Install alone didn't work for me.

mvn deploy:deploy-file -Durl=file:///home/me/project/lib/ \
  -Dfile=target/jzmq-2.1.3-SNAPSHOT.jar -DgroupId=org.zeromq \  
  -DartifactId=zeromq -Dpackaging=jar -Dversion=2.1.3
Jonathan Hendler
  • 1,239
  • 1
  • 17
  • 23