How can I configure maven to use a repository based on the folder the project is in or in the dependency coordinates? - maven

I work for a company in which I have to use VPN to access its resources, including its artifact repository (artifactory). In order to access this repository, I configured a settings.xml in my /.m2 directory.
Nevertheless, I do not want to use this settings when I do my open source projects in my free time for multiple reasons (main one being not use the VPN).
My work-related projects and personal projects are in different directories.
I cannot add the repository manually in the pom files, because all other devs have it in their .m2 directory and do not care about using it always (and because there are a lot of projects).
Is is possible to:
use one settings.xml file per folder?
configure in the settings.xml file to only get given artifacts (based on the dependency coordinates, for instance starting with com.acme, for instance) from my company's repository?

No.
You can have different settings.xml. To choose a non-standard one, you need to specify it on the command line with -s other-settings.xml.
Alternatively, you can use different users, because you can have one standard settings.xml per user.

Related

Maven build using embedded JAR in project pom

I am working on a Maven project where a build is done through Jenkins, and 1 particular JAR has been removed from corporate repository recently.
So my build is failing as parent pom.xml is referring to a JAR not available in the repo.
But I have the old certified copy downloaded in my local repo, and I want to use the same JAR during build copying in the project folder and want to use the local copy of dependent JAR in pom.xml instead of downloading from corporate repository embedded in the project structure.
How can I do this?
Frankly, this does not sound like a good idea.
While it is possible to reference jars with the <systemPath> entry, it is generally considered bad practise.
By "corporate repository", do you mean a repository of your own company or of some other company? In the first case, you should request that the jar is put back in. In the second case, it would be better not to use the external corporate repository directly, but to set up your own Nexus/Artifactory through which you use (different) external repositories. This Nexus/Artifactory can then host additional artifacts like the one you need (you can e.g. upload them through the UI).

How to depend on a local library with maven

I have a project contains two sub projects:
A. a common library for external api
B. a program depends on above library
They are inside same directory. How I made B refer to A with maven?
Normally you will always share through a maven repository. That is mavens way to ensure a consistent and correct solution and a solution shareable by all developers.
You should search for a public maven repository with project A (e.g. http://search.maven.org or http://mvnrepository.com) and include in your pom
If it does not exist in public (is proprietary in someway or other), consider using an enterprise-wide maven repository such as nexus or artifactory to push to repositories.
Finally, some developers resort to either installing a mvn-local file if you are ever only going to work on an explicit workstation.
If you still prefer a filebased acces, it is possible to define a maven file repository and reference it in your pom. E.g. Heroku use this for bundling extra dependencies into their system.
Declare A as dependency in B's pom.xml. Make sure A has valid pom.xml and is deployed to your repository (local/nexus). We do that all the time. Take care to assign SNAPSHOT version if you always want latest to be pulled from repository.

Maven how to handle multiple projects and settings.xml in same PC

I want to have multiple (local) settings.xml files in my PC for my Maven projects, one per project. What is the best way of handling these settings.xml (hopefully somehow through version control)?
I know that each developer (and the CI server) will want to have some different variables in those settings.xml.
I am thinking something like one settings.xml in subversion per project, to provide a skeleton with some empty fields so that everyone can copy and paste the settings.xml file to their local directory where it gets picked up by Maven and also fill in the blank values that are unique per environment/user. Bear in mind, I am not referring to stuff that can be set via profiles, it's more like usernames and passwords and such.
This leaves us with the problem of multiple settings.xml files. IntelliJ as I see has Maven configuration per project so I shouldn't have any trouble setting separate locations for every project. As for the command line, do I really need to type in the path to my settings.xml file every time I run something (via the -s command)?

Multiple Maven modules with dependency on a JAR

In my multi-module Maven project, suppose I have two modules, car and horse. They both depend on a JAR file, transport.jar, a file not available in any online Maven repositories. As such, I need to find a way to make these modules depend on a file found somewhere in the project folder structure.
From what I understand, the default Maven solution would be to manually register the JAR file in the local repository. While this would work on a development machine, it breaks on the build server, which clears its local repository before each build.
I've been searching online on how to do this on and off for a while and found some helpful things, but nothing that completely works.
For instance, a common answer is to add a dependency to the file using <scope>system</scope>. However, not only do others claim that it's extremely bad practice to do so, it also doesn't work on the build server. (On a side note, I would also like to point out that using absolute paths to the JAR is also out of the question due to, again, it being built on several different machines.)
A more useful method I found was to define a local repository in the POM file, pointing towards the path file:${project.basedir}/lib. (Such as in this article) Unfortunately, if I place the JAR and repository definition in the car POM, I cannot successfully add a dependency to the JAR in horse. I've tried both with and without an additional reference to car in horse, as well as defining a second repository in horse, pointing to file:${project.basedir}/../car/lib. This problem would also remain if I tried to make a third module, transport-lib, specifically for wrapping the JAR dependency.
I could most likely add the JAR file to both modules and define two separate module-local repositories, but I really don't want to unless I have to due to the need to keep the two (often updated) JARs in sync etc.
So, my question is as follows: Can someone give me a confirmed-to-work method to have two modules depend on the same JAR file inside the project, given the parameters and restrictions mentioned?
Best solution is to use a repository manager like Archiva, Artifactory or Nexus and install that artifact into the repository manager. Afterwards you can use this artifact directly in your pom files without any issue.
Don't use the scope system, cause it will cause other problem after a release for other etc.

Maven: Is it possible to have more than one settings.xml files?

I have a multimodule project. All of the modules, but one are different. There are some things in the settings.xml file that I want to be different in the one module than from the rest.
Is it possible to have two settings.xml file and use them for different modules?
Think of the settings.xml as the configuration for your installation of maven. It determines the behavior of maven across it's use in your various projects.
This being said, if an individual project, i.e. a pom, requires something unique, it should be in that pom.
I think the thing to remember is that the project should be able to build on an individual dev's machine without any special intervention. In other words, the ideal case is that a given pom can successufl execute mvn install in a vanilla environment. So, don't put something in it that requires tweaking for a dev to get it to work. Also don't put anything in your settings.xml that enables a project to build, but then puts the burden on other devs to know what secrets are in your settings.xml.
You can set up different things in your individual pom files. What's in the pom files will override what's in settings. For instance, if your child poms sets up different repositories they'll be used over what is defined in settings.xml. Settings.xml is the default is nothing else applies. Depending on exactly what you want to do you might also take a look at the profiles feature.

Resources