Maven and dependencies NOT in repository - maven

We have a dependency third-party library that is available online in jar form, but it is not in Maven Repository, or known to be in any other repository.
How can we use pom.xml to auto-retrieve this dependency, based on a URL?
We don't want to store it in our Git repo, because that's A Bad Thing.
The idea here is that when people check out the project, they can use their IDE Maven integration (or just mvn command line tools) to download all the dependencies. So we would want to be able to also download this other third party dependency just like all the ones in Maven repo.
I have not been able to come up with an answer to this based on searches -- all solutions seem to be "download it first and create a local repo." Obviously Maven can download from the Internet, since that's how it connects to Maven Central and other repos. So I don't see why it cannot download arbitrary URLs that present packages in recognizable formats.

Long term, the best solution is to use your own artifact repository like Nexus, Artifactory or Archiva.
All of these have a manual upload function that you can use to set the groupId, artifactId and version, so you can then refer to the artifact as usual.
If you want to go really low tech, I think you can just put some machine's local repository behind an Apache, provided you grant read/write access.
Then you need to add your new repository in the Maven settings.xml file, as described here.
Maven uses the coordinates to navigate the repository (which has a specific layout) and verify artifact checksums for corruption/tampering using metadata files in specific locations of the repo.
AFAIK this is similar to other package management systems like APT and RubyGems that use repo manifests and don't allow arbitrary URL downloads.
Skipping the repository manager
If you really don't want or can't use a repository manager, you can always download the artifact and manually install it using the Maven Install Plugin:
mvn install:install-file -Dfile=your-artifact-1.0.jar -DgroupId=org.some.group -DartifactId=your-artifact -Dversion=1.0
However, you'll have to do this on every machine that runs the build, every time that artifact needs to change.

Related

How to convert a Gradle project's dependencies into a local maven repository?

I am building a Java SDK that can be used to work on my app.
When I run ./gradlew :my-sdk-library:dependencies I get my transitive tree of deps.
All my customers refuse to access libraries on the internet. And they do not have a local maven proxy either, so I need to supply my sdk jars and the other open source jars too.
So I would like to convert that into a local maven repository so that I can send it to those who cannot access our maven repository that is hosted on Artifactory, nor Maven central.
The naive approach is the make a shaded (shadow) jar containing all the libraries then import it as a implementation file(path-to-shaded.jar). But that is not good because IDEs do not like huge 200MB shaded jar files. And you lose all the dependency management provided by the GAV values.
So I want to produce a local maven repository I can send along with the SDK.
So if this were Maven I would go to a fresh VM, run mvn install, then just copy the ~/.m2/repository folder and there you go.
I did find a project https://github.com/sinsongdev/gradle-cash-to-maven-repo which might work to create a local maven repo using the gradle cache, but it is not widely used. I'll give it a try.
Basically I want exactly what https://github.com/johnrengelman/shadow does but instead of producing an uber jar, to create a local maven repo.
Is there some option like that in Gradle to create an offline copy of the repo or cache so that developers who are behind strict firewalls can use your SDK?

tell maven to get JAR dependency from given URL

Unfortunately, my project has an external dependency that was never published to any Maven repository. The only way I can get it is by direct download from github (they pushed the binary to github).
One (bad) way is to download the jar manually and commit/push it to my code repository (git). It wouldn't help me to manually deploy this artifact in my local binary repository because I share this project with external contributors that cannot access my private binary repo.
I wonder if maven has a better way to handle this? (Given that I can't upload the artifact to my repo or public repo).
I know that npm allows getting some dependencies from URL. Does maven support it as well?
AFAIK there is no nice way to handle this. You could
Write a script that downloads the jar and installs it in your local Maven repository. This script could be shared through your code repository.
Include downloading and installing the artifact into the Maven build process (by writing a Maven plugin or using the antrun plugin)
Set up a nexus in the cloud that everyone in your team can access.

Maven repository toolkit - Synchronise, verify, delete, verbose info about artifact (local repository), check pgp, uninstall

I've tried and failed to find an easy way to control my local repository and to work with remote repositories.
I don't think, I need whole repository management tool (such as Nexus or Artifactory) to do simple tasks, such as:
List locally installed artifacts
Show informations about artifact from repository (both local and remote)
Verify PGP signatures for artifact (locally after fetching the artifact, or remotely if possible)
Download artifacts by selector into local repository (can be mvn dependency:get)
Download artifacts javadoc/source into local repository
Verify availability of either sources or javadocs
Remove locally installed artifact by selector (either shorthanded gradle or longer mvn cli type)
Every single clue I've found was linked to specific build-script/system (ivy,grape,pom,gradle, ..), and I don't think I should have to create dummy project or directly touch files in repository manually.
If you come up with suggestions, I use unix-based platform and primarily CLI, but suggestions are welcomed for all platforms and UI variants.

How to update maven repository manually from the maven build?

We do not have our own repository at the moment. So, when we build with maven it creates .m2 repository in the home directory of the current user.
Now there are two third party jars which are not found in the Maven Central. Suppose one of them is hasp-srm-api.jar. Today the process is this:
a. The pom.xml of the project depending on hasp-srm-api.jar contain these lines:
<dependency>
<groupId>com.safenet</groupId>
<artifactId>hasp</artifactId>
<version>1</version>
</dependency>
b. Before doing the first build we execute the following command:
mvn install:install-file -Dfile=hasp-srm-api.jar -DgroupId=com.safenet -DartifactId=hasp -Dversion=1 -Dpackaging=jar
My question is this - is it possible to automate this step? I would like to be able to tell maven to check whether the hasp artifact exists and if not - install it manually using the aforementioned command line. How can I do it?
NO. It is not possible to have maven automatically deploy an artifact into a repository in the fashion you suggest. This goes for both local and remote repositories. If the artifact exists in a some repository somewhere, you can add that repository to your build's list of known remote repos, but other than that you have to add it yourself.
You can add it to your local .m2 repository, but that will then only be good for that individual environment. Other dev's will have to repeat the process. This is one of the main attractions of running your own repository server( like Nexus ); you can add the artifact to that repository and then everyone in your organization can use it forever. There is still no way to automate the deployment of the artifact, but it's easy to do and is permanent.
Note, setting up a repository manager is very easy to do. It's highly recommended. It makes the whole Maven thing make a whole lot more sense.
The best solution for such problems is using a repository manager which results in installing such kind of dependencies only once into the repository manager and the whole company can use it a usual dependency. That's it.
Other option you have is to write your own maven plugin. May be below link will be right place for you start
MOJO FAQ

Maven without Internet connection

I'm new to maven project.
I'm changing an ant project to maven project.
To install the 3rd party jar's in maven local repository, I used install command.
Its trying to download the resource jar.pom.
I don't have download access in my organization so the build failed for installtion.
After request i got the resouce jar and clean jar in my desktop(also i can get other necessary jar).
How to make maven to use these jar for the process and how to install the jar in local repository without internet acess.
I downloaded the jar and placed in local repository but it couldn't point the path and use those jars.
please let me know what steps i have follow to run maven install and other commands to build the project without internet access.
where should i placed the jar which i have downloaded by external way.
Please guide me for building and deploying the project.
Thanks in advance.
http://maven.40175.n5.nabble.com/Maven-installation-and-using-in-project-without-Internet-conncetion-tp4564443p4564443.html.
http://www.coderanch.com/t/544641/Jobs-Offered/careers/Maven-installation-project-without-Internet#2471141
I've posted same question in these link
You need an internet connection. Maven isn't initially self-sufficient. It needs to download a bunch of plugins along with their dependencies and the dependencies of your own project. And this really depends on what sort of settings you have for your projects. One set up will require one set of dependencies, another - a whole different one. You can't download artifacts from the Maven Central manually and then install them locally one by one. Simply put, that sounds stupid.
I understand that you're coming from the Ant world where Ant has everything it needs on the local file system. However, Maven relies on the fact that it will have a central repository (either Maven Central, or your own repository - Nexus, Artifactory, etc.) from which to download the plugins and dependencies it needs. There is no point in you migrating to Maven, unless you'll be allowed access to the Central Maven Repository.
Yes, indeed, you can run Maven offline and you can have Maven produce a local repository for you to use when you are in offline mode. However, what you're trying to do is against Maven's principles.
If your company won't allow access to Maven Central, just stick to Ant. Your effort will be a waste of your company's and, ultimately, your own time.
In fact the maven strenght is mainly in the internet accessible repositories and automatic dependency management. But it's possible to use this tool to build your project if you have all dependencies required for your project in your local repository. Then you may use -o option for offline mode and maven will not try to download updated artefact versions.
To get the artifacts into you local repository you have several options:
1) connect to the internet once and mvn build the project (this will download all required dependencies)
2) install dependencies as jar to the local repository manualy (using appropriate mvn command)
I think the questioner is looking for -o or --offline option for mvn. This is a command line option and can be provided while executing.
I think you can setup your repo correctly and execute the mvn goals once when you are connected to internet and use the -o option for later executions .
Hope this helps.
~Abhay
You can configure maven to run in offline mode. Add this entry to your settings.xml
<offline>true</offline>
See here for further information:
http://maven.apache.org/settings.html
Before you can use offline mode, you have to install all necessary third party jars to your local maven repository.
mvn install:install-file
-Dfile=filename.jar
-DgroupId=com.stackoverflow
-DartifactId=artifact
-Dversion=1.0.0
-Dpackaging=jar
-DcreateChecksum=true
-DgeneratePom=true
It's much easier to get those jars in your local repository using an internet connection and online mode.
It's possible to install these resource jars in your local maven repo using install-file. This will make the available to the build. You'll have to do this for each individually, but once that's done you won't have to do anything special.
To be clear, maven puts everything in your local repository, both the jar you're building with this project and the various library jars. Because your system cannot be connected to the internet to maven can populate the local repo with your libraries, you'll have to use this manual approach.
Edit: You should be able to run install-file anywhere. When you do, you'll need to provide the groupId, artifactId, version, and packaging using the command line options. If you already have a POM file for the library, you can provide that instead via -DpomFile=your-pom.xml.
This question has some useful info: How to manually install an artifact in Maven 2?

Resources