This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
Question: How do I add a local JAR file to a Maven project?
How do I add native JAR files (not yet part of the Maven repository) directly to my project’s library source?
Answer:
Install the JAR into your local Maven repository, as shown below: mvn install:install-file \ -Dfile=<path-to-file> \ -DgroupId=<group-id> \ -DartifactId=<artifact-id> \ -Dversion=<version> \ -Dpackaging=<packaging> \ -DgeneratePom=trueCopy the code
Each of these refers to:
<path-to-file> : path to the file to be loaded, e.g., → c:\kaptcha-2.3.jar <group-id> : group to which the file should be registered, e.g., → com.google.code <artifact-id> : Example: kaptcha < Version > : indicates the version of a file, for example, 2.3 < Packaging > : indicates the packaging of a file, for example, JARCopy the code
Answer:
First, I’d like to credit this answer to an anonymous Stack Overflow user — I’m pretty sure I’ve seen similar answers here before — but can’t find them now.
The best option for using local JAR files as dependencies is to create a local Maven repository. Such a repository is nothing more than a proper directory structure with POM files.
Take my example: My main project is located on masterProjectLocation, subproject1 is located on {master_project}location, subproject1 is located on masterProjectLocation, Subproject1 on {master_project}/${subproject1}.
I then create a Maven repository in ${master_project}/local-maven-repo.
In the POM file masterproject/{master_project}/masterproject/{subproject1}/pom.xml located in subproject1, you need to specify the repository that takes the file path as the URL parameter:
<repositories>
<repository>
<id>local-maven-repo</id>
<url>file:///${project.parent.basedir}/local-maven-repo</url>
</repository>
</repositories>
Copy the code
You can specify dependencies just like any other repository. This makes your POM repository independent. For example, once the required JAR is available in Maven Central, you just need to remove it from the local repository and it will be extracted from the default repository.
<dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.servicebinder</artifactId> < version > 0.9.0 - the SNAPSHOT < / version > < / dependency >Copy the code
Last but not least, use the -dlocalRepositorypath switch to add the JAR file to the local repository, as shown below:
MVN org, apache maven. Plugins: maven - install - plugin: 2.5.2: install - file \ - Dfile = / some/path/on/my/local/filesystem/felix/servicebinder/target/org.. Apache felix. Servicebinder - 0.9.0 - the SNAPSHOT. Jar \ - DgroupId = org, apache felix - DartifactId = org.. Apache felix. Servicebinder \ - Dversion = 0.9.0 - the SNAPSHOT - Dpackaging = jar \ -DlocalRepositoryPath=${master_project}/local-maven-repoCopy the code
After installing the JAR files, you can commit your Maven repository to the code repository, and the entire setup is system independent. (Working example at GitHub).
I agree that it’s not a good habit to have a JAR dedicated to a source code repository, but in real life, a quick and dirty solution is sometimes better than a full-blown Nexus repository hosting a JAR that you can’t distribute.
The article translated from yl2gl72eozkinivz3vc6swkesy – ac4c6men2g7xr2a – translate. Translate. Goog/questions / 4…
The authors suggest that sometimes you do need to add native JAR packages to a Maven project, but it is more efficient to add them to your company’s private repository
Thank you for reading this, if this article is well written and if you feel there is something to it
Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!
If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️