How to update maven repository manually from the maven build? - maven

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

Related

Maven and dependencies NOT in repository

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.

maven repositories

I'm new to maven. I'm still failing to grasp the concept of it.
For example I'm looking for com.extjs:gxt:jar:2.2.5 or org.syslog4j:syslog4j:jar:0.9.46. I can't find them in any repo. Yet they seem fairly common packages.
Does that mean I have to download them by hand ? Doesn't it defeat the whole idea of maven ?
Where can I find a good repository that will have all these artifacts so that I don't need to download the jars by hand ?
What am I doing wrong when using maven, this definitely does not seem the way to go...
You're not doing anything wrong. The issue is that those artifacts don't exist in maven central repository. By default, that's the only repository maven will download from. You can add additional repositories (see maven docs) to configure repositories that aren't mirrored to central automatically.
As #Michael said, you are not doing anything wrong.
The default Maven central repository is not going to provide every possible artifact on the earth.
Normally you can have two way to solve it:
1) The artifact you use may be provided by some organization, which they provide their own repository to host those artifact. Tell Maven to lookup those repositories so that Maven can retrieve the corresponding artifact.
or
2) Get the JAR etc and put in your local environment.
There are two most commonly used ways for the above work:
A) Have a "local" maven repository/proxy (e.g. Nexus, Artifactory), and make your Maven points to this repository. Adding new remote repository (1) is mostly done by adding extra repo to proxy under your local Maven repo. Manually handling 3rd party artifact (2) is done by deploying the JAR to your local repo.
B) All done locally by your local Maven. Adding new remote repo (1) is done by updating the settings.xml (or your project POM.xml). Manually handling 3rd party artifact (2) is done by installing 3rd party JAR to local repository.
you can use
<dependency>
<groupId>com.extjs</groupId>
<artifactId>gxt</artifactId>
<version>2.3.0-gwt22</version>
</dependency>

Maven fails to find local artifact

Occasionally maven complains that a particular dependency, which is built and packaged locally, cannot be found in the local repository while building another project that has it as a dependency. We get an error like:
Failed to execute goal on project X: Could not resolve dependencies for project X: Failure to find Y in [archiva repository] was cached in the local repository, resolution will not be reattempted until the update interval of internal has elapsed or updates are forced ->
Where X is the project being built, and Y is the supposedly missing artifact. If you look in the local repository, the artifact is there. This artifact is never installed in our archiva repository, so the problem is purely based in the local repository.
We have tried various profiles in settings.xml, and of course "mvn -U". Neither do any good, nor should they because this artifact never goes any further than the local repository.
The only two things that seem to work are to wait a very long time until maven smartens up, or to completely delete the local repository. Presumably the waiting option is related to the aforementioned update interval.
We have experienced this problem with maven 3.0.2 and 3.0.3. We are using Archiva 1.0.3 (but again this shouldn't be a factor). Any help would be greatly appreciated.
The local Maven repo tracks where artifacts originally came from using a file named "_maven.repositories" in the artifact directory. After removing it, the build worked. This answer fixed the problem for me.
As the options here didn't work for me, I'm sharing how I solved it:
My project has a parent project (with its own pom.xml) that has many children modules, one of which (A) has a dependency to another child (B). When I tried mvn package in A, it didn't work because B could not be resolved.
Executing mvn install in the parent directory did the job. After that, I could do mvn package inside of A and only then it could find B.
Even in offline mode, maven will check remote repositories if there is a _remote.repositories marker for the dependency. If you need to operate in offline mode, you may need to delete these files.
The simple shell command below deletes these marker files. This is safe to do if you only use offline mode for the machine. I would NOT do this on a machine that needs to pull files down from the web.
I have used this strategy on a build server that is disconnected from the web. We have to transfer the repository to it, delete the marker files and then run in offline mode.
On Linux / Unix you can delete the remote repository marker files this way:
cd ~/.m2
find . -name "_remote.repositories" -type f -delete
Maven remembers when it didn't find something. The key is "resolution will not be reattempted until the update interval of internal has elapsed or updates are forced ->"
The quick solution is to delete your local "repository" subdirectory for the problem artifact - assuming you have fixed the problem with it. :)
mvn -U will force update from remote repository - again, assuming you have now populated remote with said artifact.
When this happened to me, it was because I'd blindly copied my settings.xml from a template and it still had the blank <localRepository/> element. This means that there's no local repository used when resolving dependencies (though your installed artifacts do still get put in the default location). When I'd replaced that with <localRepository>${user.home}\.m2\repository</localRepository> it started working.
For *nix, that would be <localRepository>${user.home}/.m2/repository</localRepository>, I suppose.
If you have <repositories/> defined in your pom.xml apparently your local repository is ignored.
Catch all. When solutions mentioned here don't work(happend in my case), simply delete all contents from '.m2' folder/directory, and do mvn clean install.
Even I faced this issue and solved it with 2 ways:
1) In your IDE select project and clean all projects then install all the maven dependencies by right clicking on project -> go to maven and Update project dependencies select all projects at once to install the same. Once this is done run the particular project
2) Else What you can do is check in the pom.xml for the dependencies for which you are getting error and "mvn clean install" those dependent project first and the install maven dependencies of the current project in which you facing issue. By this the dependencies of the local project will be build and jars will be created.
I run to the similar problem when my new project depend on oracle jdbc jar(which I have installed in my local repository and work well for other projects). I tried -U option ,deleting .lastupdate file or the whole directory and downlaod again,but it did not work. finally,I deleted the directory and installed it locally again,it works.
One of the errors I found around Maven is when I put my settings.xml file in the wrong directory. It has to be in .m2 folder under your user home dir. Check to make sure that is in the right place (along with settings-security.xml if you are using that).
I had DependencyResolutionException in Ubuntu Linux when I've installed local artifacts via a shell script. The solution was to delete the local artifacts and install them again "manually" - calling mvn install:install-file via terminal.
This happened because I had http instead of https in this:
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
check if if your artifact Y have packaging set to "jar". If you have defined it as "war" by error or copy paste, it will show this strange "was cached in the local repository, resolution will not be reattempted until the update interval of internal has elapsed or updates are forced". I would expect something like "artifact Y is war, jar type expected".
In my case I needed project Y to be a WAR to be deployed through Tomcat, as well as it needed to be a JAR to be able to add it as a dependency in project X.
So in project Y's pom.xml, I added this plugin to create a JAR along with the WAR:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<attachClasses>true</attachClasses>
<classesClassifier>classes</classesClassifier>
</configuration>
</plugin>
And while adding the dependency of project Y in project X's pom.xml, I had to add a classifier:
<dependency>
<groupId>groupId.of.project.Y</groupId>
<artifactId>project.Y</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>classes</classifier>
</dependency>
Note: when you build project Y, you will see 2 packagings in the target folder: project-Y.war and project-Y-classes.jar, so that's why while importing you are specifying the classes classifier to import the JAR and not the WAR.
Here is the long Solution to the problem
(Not Quick fix but will work if no other solution)
You're going to hate me for saying this but this is the truth about open source projects like eclipse. Because Open source is modular and allows you to build and develop a project in many ways with many tools such as maven, spring boot, options for xml or groovy, different eclipse updates & Etc. The problem is that eclipse allows you to run the project with missing maven builds because the IDE is smart enough to resolve dependencies using a remote_repository where it stores and catches the jar files that is not properly built on the project.
Because of this feature, You may actually have local build issues but just like DNS servers; if the solution is not found in the local directory, Eclipse will look for a solution in it's remote cached repository. When you delete the remote_repository and let Maven rebuild it a second time, The project may end up creating more errors and not build a second time or may possibly rebuild a cache that was missing. But that is unlikely.
So the long answer to fix your solution.
This is a project architecture issue!
SOLUTION:
What you need to do is look in to all your dependant project's pom.xml file and the maven dependencies folder in your local project and try to resolve all the missing dependency jars in your maven dependency folder. If you have a referenced library, I suggest moving those jars into your local project's maven dependency folder.
You have to work your way into solving every child project and then navigate into your root project and fix every single project by using Maven -> Build -> clean install (check off "skip tests" & "resolve workspace artifacts") until every project builds with a clean success.
most likely, when you force update your entire solution to all your projects, you will get a list of errors that you have the IDE auto-resolve. The auto-resolve will refer to a easy reference to fix the issue. But to deploy, you have to manually fix the project because Eclipse, Spring & Maven will work well together but there are maybe a few things they don't agree on. So, you have to play diplomat in those situations and figure it out.
That's the sad truth.
All said, I have a list of problems in my project. I have this issue. The war file generated has empty jar folders and the build is not clean without errors unless i force it. The WAR file generate will run a 404 error on tomcat server production and my angular application will throw a Cors-Error when executing the API.
All the errors on my front end project is artificial because the root of all issues is the WAR file generated. It did not generate with dependencies, the Main project did not execute in tomcat and tomcat server cannot run the spring initializer to deploy the cors-policy on the server to allow my angular application to communicate. But all in all, development environment works fine with no issues.
So that is my long ended solution for this thread.
I had the same error from a different cause: I'd created a starter POM containing our "good practice" dependencies, and built & installed it locally to test it. I could "see" it in the repo, but a project that used it got the above error. What I'd done was set the starter POM to pom, so there was no JAR. Maven was quite correct that it wasn't in Nexus -- but I wasn't expecting it to be, so the error was, ummm, unhelpful. Changing the starter POM to normal packaging & reinstalling fixed the issue.
In my case I had to add mavenLocal() in root level gradle dependency
mavenCentral()
mavenLocal()

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.

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