Project specific local repository in maven - java

Is there a maven equivalent of Node.js npm i which fetchs all the dependencies under node_modules directory, or Ruby bundle install --path <directory-path>?
I'm looking for a way to manage the dependencies written in pom.xml on the project's own responsibility. Without anything, all the artifacts are downloaded into ~/.m2/repository which is shared by all existing maven projects.
The version of maven is 3.1.1. Is there any good idea?

If you really like to change the repository for every project you can use the following:
mvn -Dmaven.repo.local=/WhatEverDirYouLike clean install
but it contradicts to the idea of the local repository.

Related

Showing jars in maven dependencies section without doing mvn install in cmd

I have added dependencies in pom.xml and immediately the corresponding jars started to show up in maven dependencies section of dynamic web project.
I just want to know that I have not done mvn install in cmd so how did they get saved in maven repository.
Another query I have, is that since jars are availble in maven dependencies folder of dynamic wep project, so my project runs successfully or not as depndencies are already satisfied without doing mvn install.
When you list a <dependency> in your project's POM, M2Eclipse (Eclipse's plugin in this case) will trigger Maven to resolve that declared dependency...meaning Maven will check your local repo first for that dependency, and if it's not found there it will pull it down from the next highest repo you have configured (possibly an agency-level repo, or Maven's default public repo on the web).
No mvn install is required, as the purpose of that would be to install your current project's packaged artifact into your local repo, rather than install any dependency.
Hope this helps to clarify why an install is not used to copy dependencies into your local repo.

Install Library and Its Dependencies From Maven Repository Into Internal Repository

Question
I have an internal maven repository in a shared folder say s:\mvnrepo – it's just a convention for a directory layout after all. Now I want to install a library com.example.lib:lib:1.0.0 and its dependencies from another external maven repository into s:\mvnrepo with the hypothetical command
mvn installlib com.example.lib:lib:1.0.0 -Dinto=file:///s:/mvnrepo
That would be a dream! Is such a thing possible?
(I don't have high hopes seeing as everything maven related is always so unapproachable and complicated...)
Supplemental Information
Now, I know there are maven repository management systems like Nexus but I really do think my use case should not require them. I also know about the deploy-file goal but it doesn't install transitive dependencies into the specified repository.
I also thought about simply creating a dummy project that has the specific library listed in its pom.xml. Then just execute mvn install -Dinto=file:///s:/mvnrepo. The problem is that the install goal apparently does not have an option to specify the repository directory (i.e. -Dinto is purely hypothetical).
I found out that it is possible to download a library and all its dependencies with Intellij. That's great but Intellij really only downloads the jars and does not create a maven compatible directory structure (i.e. there are no poms downloaded nor are the jars in a group subfolder etc.).
Background information
For work we want to have an internal maven repository such that it is possible to build an application on a freshly installed server that does not have an internet connection while still being able to specifiy dependencies in the gradle file as if we would use an external maven repository like jcenter (resp. bintray). So the gradle file we use would look something like this:
repositories {
// jcenter() // we don't want that
mavenUrl 'file:///s:/mvnrepo'
}
dependencies {
compile 'com.jakewharton.sdkmanager:gradle-plugin:0.12.0'
}
Now, I downloaded the sdkmanager plugin jar and its pom file from mvnrepository.com and used something like
mvn deploy:deploy-file -Dfile=sdkmanager.jar -Dpom=sdkmanager.pom -Durl=file:///s:/mvnrepo
All fine and dandy, the sdkmanager plugin is installed into s:\mvnrepo with the correct directory layout and gradle even picks it up! But gradle is not quite satisfied, you see. It demands the jarchivelib which is a dependency of com.jakewharton.sdkmanager:gradle-plugin:0.12.0 (see the page on mvnrepository.com where jarchivelib really is listed as a dependency of the sdkmanager plugin). Now, I could repeat the fun and download jarchivelib and its pom from mvnrepository.com, deploy it and so on. Thinking about it... that does not sound like fun at all! So now you see my concrete use case and maybe you could suggest an even better approach than what I seek for in my question.

Download a Maven artifact and install into a specific local repository (not .m2)

I have several local Maven repositories besides the one located in ~/.m2 directory and I want to simplify the process of installing new artifacts into them from Maven central.
So far I couldn't find a way to tell mvn dependency:get that dependency should be put into a specific local repository.
I did manage to find a way to install a given downloaded artifact using mvn install:install-file -DlocalRepositoryPath=, but I want to be able to get and put dependencies into a specific repository with as few manual steps as possible.
Before you ask why can't I just configure my project to use Maven central directly here is the answer: the project uses Gradle and I do not own its build script (i.e. I can't modify it). The project build script is written to work with several distinct repositories having the same base URI that I fortunately can change using build.properties file. So my idea is to have several local maven repositories in the same root directory and trick the build script to use them.
You can use the maven.repo.local property:
mvn dependency:get -Dmaven.repo.local=/path/to/localrepo

Quickest POM settings to turn an existing Eclipse web project in a Maven-managed project?

I'm converting an existing Eclipse-based web project to a Maven-managed one.
Since the project has lots of dependencies, many of which are custom (they're either internally made or they've been taken from sources that have no public repository), is there some 'magic' Maven POM setting that will let me load every jar from WebContent/WEB-INF/lib and make the project work as before right now, so that I can configure each dependency and do the necessary refactoring to turn it to a proper Maven project with a little more time and care?
I have already seen this question, but the project must continue to compile inside Eclipse, so - or at least I guess - it is not just a matter of using the Maven war plugin
What you want to do is called "installing" your non-mavenized JARs into your maven repository. This can be a local or remote repo that you host.
The command to install to your local repo is something like this: mvn install:install-file -Dfile=My-lib.jar -DgroupId=com.mycompany -DartifactId=My-lib -Dversion=1.2.3 -Dpackaging=jar
You'll want to review the various options for install to suit your project.
Once the non-mavenized dependencies are installed to your repo you can add them to your pom like any other maven dependency. They will be fetched from your local repo.
You will have to set up your own remote repo (like Artifactory) or install each plugin for every developer and CI server in your environment for others on your team to build the project. I strongly reccomend Artifactory, it makes it easy on your and your team to use maven and get dependencies.

Maven and m2e - build jar with dependencies information

We are using Maven and m2e tools for our development and today we encountered a problem.
One of our projects is small library that is required for other projects, so we packaged it into jar file and put in our private Maven repository.
For now, all of the jars that we put in this repository didn't have any external dependencies, but this library I mentioned uses some external jars.
Now, when I add information about this jar to other poms, this jar is downloaded from our private repository but Maven doesn't download dependencies needed by this jar.
I am wondering if I need to use some special target/add something to my pom.xml file that will inform Maven to include information about dependencies needed by this artifact.
EDIT:
Here is the workflow I perform when I upload jar to our private repository:
1.I generate jar file from Eclipse using m2e
2.mvn install:install-file -DgroupId=<your_group_name> -DartifactId=<your_artifact_name> -Dversion=<snapshot> -Dfile=<path_to_your_jar_file> -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true
3.I copy folder created in my local repository to remote repository
If your small library is a maven project as you state, there should be no reason to have eclipse build the jar and then use maven to install it and then manually copy to the remote repo. Instead you should use m2e to run the deploy goal:
mvn deploy
That will cause the jar to get built and then install it directly into your local maven repo then deploy it to the remote repo.
In eclipse this can be accomplished by right clicking your project, choosing Run As -> Maven Build... then in the run configuration window for Goals input type deploy then click Run. After this has been done once, you can just use Run As -> Maven Build to run the same config again.
I see you use -DgeneratePom=true during the installation of the jar file. What you need to do is create a pom.xml for your artifact. In the pom.xml, you can specify the dependencies that your jar file requires. When executing the install:install plugin goal, you use -DpomFile=pom.xml instead.
The best way to do this is to run mvn deploy
You have to setup the distribution repository to your private artifact manager (nexus or artifactory) in your settings.xml
see this for more details

Categories

Resources