Maven Local Repository, Two machines and Git: missing artifact - java

I am building a Java project using Maven. Since I need to add one jar file that has not been deployed on the central Maven repository (OpenCV), I created on the first machine, named M1, a local repository and deployed (mvn deploy:deploy-file) the external jar lib.jar file into that repository.
In the project pom.xml file I added the following lines:
<repositories>
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>file://${project.basedir}/libs</url>
</repository>
</repositories>
On machine M1 everything is fine.
I use git both for versioning and for syncing two development environments on two different machines, M1 and M2 (I do not think it's relevant, but M1 has OS X and M2 has linux).
However, after a git pull on M2, if I run maven on M2 I get an error message caused by the missing artifact lib.jar from the local repo.
M1 has maven 3.2.2, M2 has maven 3.0.4.
Is there any way to move local repositories using git or another software?
Thanks.

Solution is to create a remote repo, not local repo:
setup a Sonatype Nexus or Jfrog Artifactory instance
upload your jar into it
edit your settings.xml file to mirror it
That will allow any maven build you run to retrieve the jar regardless of machine.

Related

Maven does not update/download SNAPSHOTS jars when executed with '-U,--update-snapshots' parameter

I execute maven build with 'mvn clean deploy' on a SNAPSHOT version of my, say, dependency-lib. The build was successful and the artifact got successfully deployed in the artifactory.
Then, I execute maven with arguments "mvn -U clean package" on my, say, dependee-proj, it does not download newest deployed version of dependency-lib. It just downloads the maven-metadata.xml from the artifactory and skips the downloading of the jar files. I verified the local maven repository, and there just some xmls got updated and no new updates of jar files.
Is it something wrong which i am doing?
PS: Deleting the dependency-lib worked but, somehow I feel that this is not a sane thing to do.
Put an updatePolicy for the repository where you push your snapshots to. Ideally this is how snapshots are updated and later pulled by maven.
<repositories>
<repository>
<id>you-snapshots</id>
<url>http://host/repos/snapshots</url>
<snapshots>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
Infact you can also do mvn -U, --update-snapshots
Maven is made up of phases. These phases are:
validate - validate the project is correct and all necessary
information is available
compile - compile the source code of the project
test - test the compiled source code using a suitable unit testing
framework. These tests should not require the code be packaged or
deployed
package - take the compiled code and package it in its distributable
format, such as a JAR.
verify - run any checks on results of integration tests to ensure
quality criteria are met
install - install the package into the local repository, for use as a
dependency in other projects locally
deploy - done in the build environment, copies the final package to
the remote repository for sharing with other developers and projects.
Since deploy phase is the executed last, it means that before it all preceding phases have been executed, including the install phase which installs the artifact on your local repository.
So when deploy finishes, your local repository and remote have the latest version that you just uploaded so there is no need to download the latest version from the remote since it is already present on your local repository.
Is it something wrong which i am doing?
Finally, to answer this, nothing strange is happening, just the normal behavior.
Deleting the artifact from the local repository of course forces maven to download the artifact from the remote, since that's the way maven works.

Jenkins : Add custom jar in maven project

I am trying to add custom jar which is not available in Maven repository. When I am executing in local I am pointing to the Jar path, So no issues. But when I execute with Jenkins I am facing issue as Could not resolve dependencies for project. Also it list down the two Jar file missing which I added custom.
So help me how to add the custom Jar wile executing using Jenkins. Also I am using Nexus for binary storage. After Jenkins executes successfully the image file will be posted to the nexus directory with versioning.
You have 2 options
1) If your have access to the local Nexus/Artifactory repo that your Jenkins uses you can add the jar to that repository and it will be downloaded as normal.
2) You can have a local repository where you check the jar into SCM under your project. You would then access it like this in your pom. See http://doduck.com/adding-local-jar-in-maven-local-repository/
<repositories>
<repository>
<id>localrepository</id>
<url>file://${basedir}/repo</url>
</repository>
</repositories>

How to import a deployed jar "SNAPSHOT" from local Artifactory repository into another project?

I am a newbie in Artifactory, I have 2 projects one depends on another...
I set up Artifactory on a server and deploy the first jar into libs-snapshot....and change the C:\Users.m2\setting.xml and add this tag in the pom of the deployed project:
<distributionManagement>
<snapshotRepository>
<id>serverId</id>
<name>serverName</name>
<url>serverUrl/artifactory/libs-snapshot/</url>
</snapshotRepository>
</distributionManagement>
how can I access the first project from the second one via Artifactory repository
I am working on Netbean8.2, glassfish 4 and artifactory 4
By default, maven doesn't know to look anywhere except your local repo and maven central. You'll need to tell it the additional repos it can look in either via a pom setting or settings.xml.
You can see some example and additional details in the Maven docs.

How to load/add third party jar files to bitbucket?

I want to load some jar file that are not present in maven repository or any other such repository.
For that I have created a repository named as "maven-repo" on bitbucket and cloned it to my local machine.
Now I am using command
mvn install:install-file -DgroupId=groupid -DartifactId=myid
-Dversion=5.1 -Dfile=/path to file/filename.jar -Dpackaging=jar -DgeneratePom=true -DlocalRepositoryPath=./repository -DcreateChecksum=true
After that I push that folder/repository to bitbucket server.
Now I want to add it to my maven project's pom.xml.
I added like:
<repository>
<id>Hyperv </id>
<url>https://myurl/repository/</url>
</repository>
But it gives me error failed to load a jar.
How to include a jar file and import classes in project???
Deploying with Maven
From official documentation (I cannot link, this is a popup)
Bintray adds a new layer to traditional Maven repositories in the form of a package.
A package acts as container for managing metadata about your project and its versions and may contain a collections of artifacts with different group ids.
When uploading files to a repository, you can associate them with specific package and version information in various ways:
Deploying to this repository
Please go into a specific package in this repository to see how to set up Maven to deploy artifacts to that package.
In Maven’s setting.xml file, add the following section to declare your Bintray credentials. Use your API key as your password (not your login password, please!):
<server>
<id>bintray-user-package-package</id>
<username>user</username>
<password>**********</password>
</server>
Add the the following Distribution Management section to your project’s pom.xml file to tell Maven to deploy into this package using the credentials you configured in the previous step:
<distributionManagement>
<repository>
<id>bintray-user-repo-package</id>
<name>user-repo-package</name>
<url>https://api.bintray.com/maven/user/repo/package</url>
</repository>
</distributionManagement>
Manual deploy
Prepare artifact folder as is described in How Do I Upload My Stuff to Bintray?
And deploy using commannd like this
mvn deploy:deploy-file -DpomFile=myfile-0.1.pom -Dfile=myfile-0.1.jar -DrepositoryId=bintray -Durl=https://api.bintray.com/maven/user/repo/package
In general deploying with maven is much easier

multiple repositories with the same artifact in maven

I have made some modifications to log4j and would like my project to use my local version rather than that from the remote maven repo, so I declared my project as a local repo in pom.xml in addition to my remote repo for other dependencies:
<repository>
<id>my-log4j</id>
<name>my log4j</name>
<url>file:///...</url>
</repository>
<repository>
<id>remote</id>
<name>remote repo</name>
<url>http://...</url>
</repository>
maven copied the files from my local repo as expected, but then it downloaded log4j again from the remote repo and overwrote the earlier files. Is there a way to exclude certain artifacts from being downloaded from the remote repo?
Also, how does maven detect changes to my-log4j? How can I make maven copy the my-log4j artifacts each time during compilation?
If you make a custom version of something, you give it a custom version number.
For example, if you modify log4j-1.2.17 for your own use, give it the version 1.2.17.JRR.1 and following numbers as you work on it.
You build them on your computer and when you run the install goal, it will put them in your local repo. If you have a shared repo for your group, it can be deployed there as well and never confused with the Apache releases.
This will never be found in the remote repo, just in yours.
If maven looks for artifacts, it always looks in your local repository first, you do not have to specify it (you can specify the location of your local repository in your settings.xml).
You answered your question already: If you had to change a third-party artifact, rename it (already in the pom.xml) like my-log4j or log4j-my-patch. Then it won't collide with the original artifacts.

Categories

Resources