What does it mean to say that a pom is installed in local repository? - maven

From the book Maven: The Complete Reference inside the section 3.6.1. Grouping Dependencies it says :
If you create this project in a directory named persistence-deps, all
you need to do is create this pom.xml and run mvn install. Since the
packaging type is pom, this POM is installed in your local repository.
You can now add this project as a dependency and all of its
dependencies will be added as transitive dependencies to your project.
When you declare a dependency on this persistence-deps project, don’t
forget to specify the dependency type as pom.
What does it mean to say that a pom is installed in local repository?

Every time Maven needs to find a a dependency, it first looks in the local repository - commonly located in an .m2 directory in your user home. If it can't find the dependency there, it downloads the dependency into your local repository and uses it from there.
When it says a pom is installed in the local repository, it means the POM file was copied into the correct place in the local repository. Other projects you build locally can then resolve that dependency and use it.

Related

Conditionally include maven dependency locally instead of nexus repository

I have a project A and B. Project B uses project A as a dependency. I am publishing project A to Nexus repository when changes are tested and good to go to Nexus. However I don't want to publish project A to nexus whenever I want to test changes locally.
Is there a way to use project A's changes locally into project B without having it to be read from repository. Some condition that will make maven read project A from local rather than Nexus.
I want to keep project A in common maven dependencies rather than making changes in Maven profiles.
Maybe I am missing some obvious point but I would assume that you can just mvn install project A with a new version and adjust the dependency of A in project B to use that new version. By installing project A it should be available to be used in project B locally because it will be installed into your local Maven repository.
E.g.
In project A's pom.xml
<project>
<name>A</name>
<version>1.1.1-new-version-for-testing</version>
</project>
In project B's pom.xml
<dependencies>
<dependency>
<artifactId>A</artifactId>
<version>1.1.1-new-version-for-testing</version>
</dependency>
</dependencies>
First things first, there is a local repository on your computer that contains all the dependencies (by default in ~/.m2 but you can change that)
You can think about it as a local cache of the dependencies required to work with your own project
When you change the project A you can install the "updated" version by running mvn install on project A.
After that command project B when tested will not try to contact maven repository and will get a version of project A from your local repository.
For SNAPSHOT dependencies, maven once in a day will try to get the later version from Nexus anyway, even if you have a copy in the local repository, because working with snapshots assumes that you're OK with getting daily changes.
But then you have the following choices:
Don't work with SNAPSHOT-s at all. This is something that you shouldn't do anyway in production (I mean, when you release project B, it should contain SNAPSHOT dependencies in its pom)
When you compile project B, assuming you have all the dependencies in the local repository, use mvn <whatever> -o. This -o option means that maven should be run in offline mode, that is it won't attempt to contact a remote repository altogether. (BTW, If you want to do the opposite, which is to forcefully download all new dependencies from Nexus, you can run mvn <whatever> -U

How to remove maven artifcat completely from SpringSource?

I have a local maven repository and installed a custom artifact. It works if i reference it in other projects. But now i want to use a server for a "own maven repository". If i delete the artifact from the local maven repository, i assumed that the project will not build when i do a maven clean and maven force update dependencies. The artifact cannot be found under .m2/ but Spring Source Tool Suite still can add the artifact to new Java Projects. Create New Java Project -> Edit Pom -> Maven Artifact is added, even if i deleted it from local repository .m2/ . How is this possible and how can i delete it completely, to be able to test if now all dependencies are updated from my server with the .m2/settings.xml configuration?
Your repository is just a directory/file structure. Go to your local repo, find the path (the group id is the path), and delete from the place where you start to see version numbers. When you rebuild, the artifact should be downloaded/replaced from your server/repo.

How to install a Maven/Gradle project along with all its dependencies to a local repository?

I have a Gradle project that depends on several open-source projects up on Maven Central. I'd like to install the project – along with all its direct and transitive dependencies – to my local maven repository, so that I could later zip it all up and put it on offline machines.
How do I do it with Gradle/Maven?
mvn dependency:get plugin will fetch the artifact with all dependencies to the local repository.
I had also developed a plugin to install remote artifacts to a local machine.
If you want to later ZIP up your project w/ dependencies and move them to a different machine, you could try Maven's appassembler plugin. It collects all dependencies and creates a launcher, all in the target folder, ready for deployment.
But note, this, by default, creates a flat directory structure with all dependencies, it doesn't preserve the Maven format. It also has the option to create a repository.

Unconventional dependency name in maven

I have a few dependencies like this. For example : jdic
In my pom.xml, I defined a dependency
<dependency>
<artifactId>jdic</artifactId>
<groupId>jdic</groupId>
<version>0.8.6</version>
</dependency>
And I have a remote repository (internal server): eg http://repo/thirdparty/
And jdic can be found in http://repo/thirdparty/jdic/jar/jdic.jar
As you notice, the naming is not conventional groupId:artifactId:version, instead its just jdic.jar
so when I run maven compile
mvn clean compile
maven tells me that it cannot resolve dependencies.
I'm aware that we can just download those jars to the local repository .m2/repository and run
mvn compile (ie without clean)
and it the jar will not be a problem. But is there any other way that I can make it retrieve from the remote despite its unconventional name and lack of metadata/pom info for those dependencies?
I already have a mirror to this internal repository that overrides the central
You could install the jdic in your internal/mirror repository with the version (jdic-0.8.6.jar).
If you are using a repository manager and you uploaded the jar to it, it would automagically create a pom for it as well as ensure the dependency was created with version, as per maven convention.
A remote Maven repository is NOT any web server putting files in any way you want. There are lots of convention to follow. One of them is the way to represent the version (which is one essential element of an artifact). With your "remote repository" it is clear that there is no way Maven can find the artifact.
Setup a real Maven remote repository to host such files. You can have a look in Nexus and Artifactory.

How do I add an artifact to a local maven repository so that it will properly reference its own set of dependencies?

I am developing an application that depends on a number of legacy JAR files and I want the project to build straight out of version control without other users having to install these JARs in their local repository. I cannot add them to the corporate repository so I have created a repository that is local to this project and I have added these JARs to that repository using maven-install-plugin:install-file and setup the repository entry in the POM file so it knows to search the local repository.
This works exactly the way I want...up to a point. The problem is that most of these legacy JAR files have their own set of dependencies. I would like for them to work just like other artifacts that have their own set of dependencies so that maven can resolve everything and include all the necessary files but I can't find a way to do this with any maven-install-plugin:install-file options (or any other maven commands/plugins). I am pretty new at maven so I am probably just ignorant on this point.
As a work around, I attempted to go into the local repository directory and manually edit the POM file for the artifact to include the dependencies. This didn't cause any errors but it is also not pulling in those dependencies.
Can someone out there give me a clue?
The maven-install-plugin:install-file goal has a pomFile attribute. You can use this to specify a POM file for your legacy jar. You would create a POM file that points to all of the dependencies by artifactId in the <dependencies> section. If you have a remote nexus repository you can use the admin screen for the repository to deploy a jar.
Once you edit POM files in your project specific repository, host it as maven repo using Maven Repository Managers (like sonatype nexus). Add your project nexus repo as one of the maven repo in project pom.xml as below
<repositories>
<repository>
<id>my-project-mvn-repo</id>
<name>my-project-mvn-repo</name>
<url>http://<your project maven repo URL here></url>
</repository>
<repositories>
Now all developers should be able to make build. The legacy jar files POM contains dependency. Maven should take care of automatically pulling dependent jars on developer's workspace.

Resources