Maven downloading from central repository - java

I am on Netbeans and don't know Maven much. Whenever I import, open some Maven project, it starts donwloading something from some central repository, sometimes huge. It downloads things in .m2\repository.cache\m2e. I have limited bandwidth and don't want this. How to stop this?
I have set Options>Java>Maven>Dependency Download Strategy to never. Also tried mvn -o install and mvn -o for offline. Not solved.

The Maven way is to get you what the project says it needs, but you have not already downloaded to your local repository.
The huge file is the list of what is actually available in Maven Central, and for some reason unknown to me it is downloaded on a regular basis. If you do it once, it should be kept for future sessions.

Maven will download all the dependency only once to the local repository and not again and again.
Weather you have limited or unlimited bandwidth you have to download it to execute your project.

Maven has a very modular architecture. That means the the thing you get when you download the Maven distribution is in reality small core functionality.
The rest is downloaded from a Maven artifact repository, like Maven Central (which is the default repo).
Note that this applies not only for dependencies (the library your project uses), but also your plugins (i.e. the stuff that compiles, packages, and otherwise builds the projects). Hence the large number of downloads.
Like the other answers said, if you don't delete your local repository it should eventually contain all the artifacts (dependencies and plugins) you need without re-downloading. The only exception are SNAPHSOT dependencies which can get re-downloaded periodically, depending what's in your POM and settings.
Ultimately, you have two possibilities:
If you have access to a higher-bandwith connection somewhere, you can build the projects while using it, and your local repo will still store the needed artifacts.
If you have several computers/configurations behind a local network, you can set up a Maven repository manager, like Nexus or Artifactory, and use it as a local mirror. Note that those still need to download the artifacts at first as well.
But there isn't much else you can do. "Maven downloading the Internet" is, unfortunately in your case, by design.

Related

Loading maven dependencies from local Git server

We are using our git server for project management. How do I download maven project dependency from my git server?
For project checkout/pull and push we are using tortoise git
Clarification:
We are using another project as a submodule in our project. That project available on our local git server. So, when the new update is available I have to download and copy in m2 repository manual. Instead of a manual process, I want to download from my local git server.
Storing jar artifacts in git is a bad idea. Git is not meant for binary files. Use a maven repository server like Nexus or Artifactory instead.
EDIT: I admit that this answer lacks background and explanation. So I added a little.
Most sources I know do not recommend to put (large) binaries into git repositories because checking out the git repository means checking out all old versions of the binaries and that might be a lot of stuff.
There are specialised solutions (Maven repositories like Nexus/Artifactory) for the task at hand which can be directly used by Maven without giving URLs to separate artifacts (the URL of the repository suffices to find all artifacts in it).
AFAIK GitHub and GitLab offer services to provide Java artifacts as Maven repositories. So if you use on of these services, you probably have cheap other option.

How include 3rd party jars in maven project (compile, build, package) where adding local maven repo is not an option?

We have a git project that has some 3rd party jars which are not available in any Maven repo and are in a "lib" folder in the project. I need to include them for compiling, building and then package them into the WAR in WEB-INF/lib.
I cannot add them as a local maven repo from the command line because this projects needs to be buildable for anyone cloning the repo without requiring them to run extra commands (I have no way around this requirement).
I saw some people suggesting System scope but that then Maven won't package them into your WAR:
<dependency>
<groupId>com.roufid.tutorials</groupId>
<artifactId>example-app</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/yourJar.jar</systemPath>
</dependency>
How do I get these jars to be used for compiling/inspection, building and then packaged into the WAR?
You can use :
System scope but make the packaging yourself via assembly plugin
A maven repo along with your project (i.e. maven repo on-the-fly, basically same as local repo but without having a extra moving part to worry about because this repo follows your project).
For Maven repo on-the-fly option, you can do as described here (which is, take any already existing Maven repo, which already contains your needed jars, such as your local one, put it in your project, and then reference this repo from your project using relative paths).
I'll assume you've verified that whatever mechanism you might use to distribute these jars would be in compliance with the relevant licenses. If it would, then it seems there would be little reason for the jars' creators not to provide for official Maven distribution, so your best option might be to lobby for them to do that. But if not, and yet for some reason they'll allow for you distributing the jar (either through cloning of your repo, or via a separate Maven repo you maintain):
There are several ways. I give preference to approaches that don't put the jars in the git repo.
Publish a Maven Repo
So it's possible to host a public-facing repo and serve the artifacts that way. The pom can add your public-facing repo to the build, so that those who clone can build without having to run any special commands.
Running your own repo isn't terribly difficult. The OSS versions of Nexus or Artifactory jFrog would probably be perfectly capable.
But, if we're assuming the authors' refusal to publish their own jars via Maven means they don't want them distributed that way, then there's no reason to spend much time on the details of this option. So moving on...
Distribution in the Git Repo
I guess this is what you're doing, though again if Maven distribution violates the license I'd make sure you're splitting hairs the right way in thinking that this doesn't.
So the question would be how to get Maven to deal with the artifacts distributed in this way, and again there are some options.
Your objection to putting the jars in the local repo is that it would require extra commands of the user; but actually this could be automated in the "validate" phase of the build. Binding install:install-file to the validate phase should work.
Alternately, your objection to using system scope is that the file isn't copied into the final war. You might be able to use the dependency plugin to force the issue, but I'm not sure of that. What I am sure of is you could treat the directory containing the jars as a web resource with suitable configuration in the war plugin. (You'd want it to be treated as unfiltered and to map to the WEB-INF/lib folder.)
In any case, if you distribute jars (or other large binaries) in the git repo, I strongly recommend you look at git lfs. This will require one-time configuration by each of your users, but it will prevent your repo from gradually becoming bloated and unusable.
Use forward slash (/) to backslash () in the systemPath.
<dependency>
<groupId>com.roufid.tutorials</groupId>`enter code here`
<artifactId>example-app</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}\lib\yourJar.jar</systemPath>

Maven and dependency:sources goals: use alternate repository to download sources?

I am working with an enterprise Nexus repository, whose contents is rather static, meaning it is not a copy of central, but a snapshot at some time.
This Nexus repository does not contains sources classifier. Hence, when I launch one of those goals:
mvn dependency:sources
mvn eclipse:eclipse
I've got a lot, if not all, of dependencies without sources.
What I want to do is pretty simple: I want to tell dependency and eclipse plugins that they should use the central repository only when looking for sources.
If that possible natively?
Can I do that with Nexus and how ?
If you are not even the manager of this Nexus server you are out of luck. You can only work around it by using another Nexus server, maybe installed locally, that does access central as well as your corporate, locked-down instance.
However, most likely, you are not allowed to do that.. locking it down was the reason to have a static repo in the first place.
I would suggest to work with the Nexus administrator to add javadoc and sources to your static repo.
And btw. I have a little tool that can provision a repository called the Maven Repository Provisioner and it can include javadoc and sources. Check it out at https://github.com/simpligility/maven-repository-tools

Make my local .m2 folder as a repository

Is it possible to make the .m2 folder in my local machine a repository so that other team members can use my repository instead of checking anywhere else and get the dependencies from mine? I have seen http://www.sonatype.org/nexus/go but there, we have to upload all dependencies manually.
Can we make .m2 folder (in some machine) a maven repository so others can use it?
The important thing with you local repository is that it's stable. If you were to put it on a shared drive, everytime one of you team builds, the artifacts that the rest of the team see will change - development being what it is, this would be a nightmare as the bugs that are introduced while developing would immediately effect the whole team rather than being isolated to one person (the assumption here is that you have multiple artifacts and build with mvn install).
The correct way to do this is to setup a repo, that proxies the public Maven repos. Both Nexus and Artifactory are setup to do this out of the box and are very easy to install on either Windows or Unix. The proxy feature means that your repo only need contain the artifacts produced by your team and the repository will retrieve other artifacts from the public Maven repos as needed (often storing them for future use).

Switch to older version of Maven

I am trying to build a project using Maven but I don't know Maven.
Anyway I had a problem and I found in a link to use an older version of Maven. So I did, since the instructions for the project were about 2.0.9 anyway.
But now I am not sure how to proceed. I see under my user.home directory a .m2 folder with a repository directory.
Should I delete this or not?
The .m2 folder contains the downloaded artifacts and some other repository info, which do not depend on the used maven version. So you dont need to delete anything, just use the maven binary of your choice.
EDIT: the contents of the artifacts - .jars, .poms, .boms etc. are defined by the artifacts themselves which are the accesible through repositories/catalogues. The .pom of your project references the needed artifacts. The maven binary downloads the artifacts (and does many other things which are OT here) to your local cache - the .m2 directory. The point is, no matter what version of the maven binary you use, the artifacts remain the same.
You surely can delete your repository. As long as you have an internet connection maven will simply download the artifacts again. But there is nothing to be gained by the removal except a longer initial build time and some traffic.
Sometimes it does make sence to delete certain metadata from the repo, like the .lastUpdated files. It is only advisable if maven is unable to find a dependency which you know for sure is in your repository. You can then run find ./ -name "*.lastUpdated" -exec rm {} \;.

Categories

Resources