ChatGPT解决这个技术问题 Extra ChatGPT

Maven: How to include jars, which are not available in reps into a J2EE project?

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.

I really think you should review your decision of creating a Maven repository. It's not hard at all and 100% worth it.
The value of creating a Maven repository is dependent on the circumstances.

R
Ric Jafe

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. :)


Cool. Is there a way to specify a URL instead of a local machine path?
Thank you for a complete and working example!!! I hope you catch a gold fish and it grants you three wishes. :)
This should be the accepted answer as it is self-contained(in pom.xml)
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.
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...
P
Paul Verest

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.


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: pastebin.ca/1504318
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.
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"
m
mostar

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>

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!)
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.
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.
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.
G
Gregory Mostizky

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.


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.
A
Apurva Singh

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.


d
dfa

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


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.
i
ipingu

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)


C
Community

@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

Above solution returns 'dependencies.dependency.systemPath' for xx.jar should not point at files within the project directory. See stackoverflow.com/questions/10935135/…
C
Corehpf

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


J
Jonathan Hendler

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