Sometimes you have a certain .jar file that you need as dependency, but the author of that .jar was too lazy to properly upload it to a public repository. That’s bad, but not a problem. There are two ways to solve this, but only one proper way.
The proper way: install the dependency
The proper way for you to use the dependency is to install it to your local repository. You can do so using the following command:
mvn install:install-file -Dfile=<filename> -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar # or as multiline command: mvn install:install-file \ -Dfile=<filename> \ -DgroupId=<groupId> \ -DartifactId=<artifactId> \ -Dversion=<version> \ -Dpackaging=jar
You can basically choose anything you like for <groupId>, <artifactId> and <version>, but of course it makes sense to use something appropiate. For example, if you downloaded a .jar from GitHub made by the user “someuser”, and the .jar is called “somejar-1.0.jar”, you could use this to install it:
mvn install:install-file -Dfile=somejar-1.0.jar -DgroupId=com.github.someuser -DartifactId=somejar -Dversion=1.0 -Dpackaging=jar # or as multiline command: mvn install:install-file \ -Dfile=somejar-1.0.jar \ -DgroupId=com.github.someuser \ -DartifactId=somejar \ -Dversion=1.0 \ -Dpackaging=jar
Now you can easily add it to your pom.xml:
<dependency> <groupId>com.github.someuser</groupId> <artifactId>somejar</artifactId> <version>1.0</version> </dependency>
The bad way: using system scope
If you are too lazy or you like to do bad stuff, you can also use the system scope. Just place the .jar somewhere in your filesystem and then specify the path to it using the system scope:
<dependency> <groupId>com.github.someuser</groupId> <artifactId>somejar</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>/path/to/somejar-1.0.jar</systemPath> </dependency>
Deploy a single file to a remote repository
You can also deploy single artifacts to remote repositories, just like this:
mvn deploy:deploy-file -Durl=<repositoryURL> -DrepositoryId=<repositoryId> -Dfile=<filename> -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar # or as multiline command: mvn deploy:deploy-file \ -Durl=<repositoryURL> \ -DrepositoryId=<repositoryId> \ -Dfile=<filename> \ -DgroupId=<groupId> \ -DartifactId=<artifactId> \ -Dversion=<version> \ -Dpackaging=jar
This requires that you setup your ~/.m2/settings.xml
to include the credentials to your repository, for example for regular username/password authentication:
<settings> <servers> <server> <id>repositoryId</id> <username>yourUsername</username> <password>yourPassword</password> </server> </servers> </settings>
Discord
Join my Discord Server for feedback or support. Just check out the channel #programming-help
🙂