Maven local repository in settings.xml vs pom.xml - maven

I am new to Maven. I would like to use the following setup:
1) a remote repository to download public artifact
2) a shared local repository for downloaded artifacts which works nicely by adding the following to settings.xml:
<localRepository>C:/m2repo</localRepository>
3) Now I would also like to have a repository for artifacts referenced
from a single project, like an old-fashioned "lib" folder.
So I have added the following to the project's pom.xml:
<repository>
<id>repo</id>
<name>repo</name>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<url>${project.baseUri}repo</url>
</repository>
I have deployed some jars to the "repo" folder, but when I add them as dependencies in pom.xml, they are not resolved. Maven only looks to the shared repository and the remote repository. When I copy the contents of "repo" to the shared "m2repo", it works. Am I missing something or is this setup not possible at all?

Though possible, personally I do not think that it is a good practice to do that. Depending on your usage this post will be helpful: http://blog.sonatype.com/people/2009/02/why-putting-repositories-in-your-poms-is-a-bad-idea. You also want a reproducible and portable project, this question discusses this related topic. Please don't check in those jars into your VCS just for portability sake!
I will try to help if you still insist. To make your model to work, your "repo folder" must follow the structure defined here.
Even though your "repo folder" is a local file structure, Maven will still treat it as if it is a remote repository, just like maven central. This means, whenever you are building your project, Maven will try to download the jars from your "repo folder" to your .m2 repository too:
Downloading: file:/j/my-project/my-local-repo/com/google/guava/guava/14.0.1/guava-14.0.1.pom
Downloaded: file:/j/my-project/my-local-repo/com/google/guava/guava/14.0.1/guava-14.0.1.pom (6 KB at 276.3 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/com/google/google/1/google-1.pom
Downloaded: http://repo.maven.apache.org/maven2/com/google/google/1/google-1.pom (2 KB at 10.4 KB/sec)
You can see that the guava jar will be downloaded from the "repo folder" to my .m2 repository since it is available from my "repo folder".

The cause of my problem was the mirroring of repositories in my settings.xml, I have mirroring set to all repositories:
<mirror>
<mirrorOf>*</mirrorOf>
</mirror>
which I changed to only external repositories:
<mirror>
<mirrorOf>external:*</mirrorOf>
</mirror>
Now my artifacts are found in the "project" repository. However, Maven behaves differently than I expected, as ceilfors writes, the filesystem repository is treated the same as remote repositories - artifacts are copied from there to the local repository, which leads to duplication of jars between the local repository and other filesystem repositories. So I see the setting is not ideal. However, in my case, I still find it the quickiest way to mavenize the project.

Related

How to publish all jar for an existing project in a nexus/sonartype?

I have a maven project, and I have already all my dependencies in my local host. I want to push all these dependencies into nexus/sonartype.
I don't want to push my jar one by one by uploading via nexus interface.
So I copied the content of my maven repository ($MAVEN_HOME/repository) into {nexus-data}/storage/public
Nexus have to recalculate index after restarting?
You might want to use Distribution Management -
<distributionManagement>
<repository>
<id>some-artifactory</id>
<name>Artifactory Name</name>
<url>http://your.artifactory.address/releases</url>
</repository>
<snapshotRepository>
<id>other-artifactory</id>
<name>Other Artifactory Name</name>
<url>http://your.artifactory.address/snapshots</url>
</snapshotRepository>
</distributionManagement>
Repository
Where as the repositories element specifies in the POM the location
and manner in which Maven may download remote artifacts for use by the
current project, distributionManagement specifies where (and how) this
project will get to a remote repository when it is deployed. The
repository elements will be used for snapshot distribution if the
snapshotRepository is not defined.
So I copied the content of my maven repository ($MAVEN_HOME/repository) into {nexus-data}/storage/public
Don't do that. In Nexus 3, Nexus doesn't use raw files so relying on this approach isn't going to work for you anyway.
You should be using distribution management as described by #nullpointer. If you can't for some reason, you could write a script to use Nexus' REST API to upload many artifacts. That's more useful when you aren't building using Maven. For example, maybe you have a whole pile of legacy jar files to upload.

How can I add maven artifact into an existing maven project

How can I add maven artifact into an existing maven project.I understand that I can build a jar locally and use file: protocol but this is possible using maven also.
For example I have a basic maven project
https://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project and the artifact:
<repositories>
<repository>
<id>myrepo.org</id>
<name>MyRepository</name>
<url>http://mywork.com/repository</url>
</repository>
</repositories>
<dependency>
<groupId>org.ethereum</groupId>
<artifactId>ethereumj-core</artifactId>
<version>1.1.0-RELEASE</version>
I tried adding the code above to the project pom.xml fails because dependency is not in central maven repo.
mvn clean install
I then tried editing my settings.xml by adding the tag, that also failed because dependency was not found.
Im overlooking something pretty basic here.
Maven works on the concept of local and remote repositories.
The local repository refers to a copy on your own installation that is a cache of the remote downloads, and also contains the temporary build artifacts that you have not yet released.
Remote repository is repository you access the artifacts via file or http / ftp protocols , it can be an internal repo or a remote public hosted.
When you add dependency maven search that artifact in local if not found then remote repo will be searched. Still not found then error is reported.
https://maven.apache.org/guides/introduction/introduction-to-repositories.html
In your case, 'ethereumj-core' can not be located neither of location, you need find this jar and do a manual install to local repo.
mvn install:install-file -Dfile=< folder >\ethereumj-core.1.1.0-RELEASE.jar -DgroupId=org.ethereum
-DartifactId=ethereumj-core -Dversion=1.1.0-RELEASE -Dpackaging=jar
once properly installed maven should be able find this artifact when you add this as dependency to any of the projects (in pom.xml).
<dependency>
<groupId>org.ethereum</groupId>
<artifactId>ethereumj-core</artifactId>
<version>1.1.0-RELEASE</version>
</dependency>

Can I resolve one Maven dependency from one repo and another dependency from second repo in same POM?

We have a project with multiple parent/child POMs. All the POMs are pointing to a single repository for resolving all Maven dependencies.
Now I have a need like: In a single POM, one jar has to be downloaded from repo1 and rest 4-5 jars from repo2.
How can you do that?
In a word - yes. Maven's dependency resolution mechanism is completely separate from the repository mechanism. Theoretically, you could have every single jar delivered from its own repository (however ridiculous it may to actually do this).
What I have understand the,
I can setup maven repo in artifactory/nexus
e.g. http://localhost:18081/artifactory/ --> L1
1. create remote(R1) repository in artifactory, which can point URL to outside repository, hosted by artifactory/nexus
e.g http://remotehost:18081/artifactory/remote-repo1
2. create a "virtual" repository(V1) in my artifactory and add remote(R1) in to this V1.
3. Let all my poms points to my local artifactory virtual repository(V1),
e.g.>http://localhost:18081/artifactory/virtual
that way, maven will look
a. local .m2 folder
b. then look for jars in virtual repo of my artifactory
hence virtual will look
b1. all local repo
b2. all remote cache repo
b3. all remote repo --> e.g.http://remotehost:18081/artifactory/remote-repo1
I am experimenting this,once succeed, i will update
EDIT :
This has worked for me, the only hiccup I faced was my ~/.m2/settings.xml
the snapshot was false, and my jar in remote repo is a snapshot jar.
After changing this value to true, now its fetching the jars :)
</profile>
<profile>
<id>virtual-repo</id>
<repositories>
<repository>
<id>central</id>
<url><repo_url></url>
<snapshots>
**<enabled>true</enabled>**
</snapshots>
</repository>
</repositories>

Maven: unresolvable build extension

I've been looking at google and nothing really points to this problem. When I run "mvn clean install" it returns the following error.
[ERROR] Unresolveable build extension: Plugin
org.sonatype.flexmojos:flexmojos-maven-plugin:3.8 or one of its
dependencies could not be resolved: Failed to collect dependencies for
org.sonatype.flexmojos:flexmojos-maven-plugin:jar:3.8 ()
I'm trying to figure out how to import the maven plugin flexmojos but there are no clear directions on how to do this.
How would I import this plugin into my project?
The dependency you are looking for does exist.
To troubleshoot this problem further we'd need to see your POM and the rest of your build output.
Taking a stab in the dark:
Is this the first time you're running this build on this machine? If so, a very common "gotcha" is a corporate firewall preventing access to Maven Central. The solution in this case is setup a Maven repository manager like Nexus, or configure Maven to use a HTTP proxy.
I had a similar problem. I set up the proxy in Eclipse then I discovered I also set up the proxy in my settings.xml. I deleted the proxy from settings.xml and all worked out.
I hope my situation will help!
I solved this by adding another profile parameter from my pom.xml to my mvn command, e.g. "-Pprofile-name" pointing to a non-Maven repo definition embedded inside that profile, since Maven might be looking for repo definitions to be standing alone in a settings.xml, which isn't always the case.
I resolved this by adding a settings.xml file in the ~\.m2 directory, with the appropriate configuration pointing to our internal libraries.
If this Problem is displayed in the Eclipse Environment, this is because the m2e Connector tries to download the Plugins in to your ~\.m2\ repository
to solve this, open your Eclipse Settings: Window->Preferences and go to the ->Maven->User Settings Section.
Check if either Global Settings or User Settings is connected to the settings.xml File that your Maven uses.
Generally: Maven or your m2e connector tries to download these plugins via the plugin-repositories configured in your settings.xml it's can't find them because the repository is unknown or not reachable because you are behind a proxy or so:
08.11.18, 15:54:47 MEZ: [WARN] Failed to build parent project for com.xxx.xxx.xxx:eclipse-plugin:1.0.0-SNAPSHOT
08.11.18, 15:54:47 MEZ: [WARN] Failure to transfer org.apache.maven.plugins:maven-site-plugin/maven-metadata.xml from http://repository.sonatype.org/content/groups/sonatype-public-grid was cached in the local repository, resolution will not be reattempted until the update interval of tycho has elapsed or updates are forced. Original error: Could not transfer metadata org.apache.maven.plugins:maven-site-plugin/maven-metadata.xml from/to tycho (http://repository.sonatype.org/content/groups/sonatype-public-grid): repository.sonatype.org
go to your settings.xml file and add:
Mirrors (in this example: central repository. Do so for any other repos accordingly e.g. tycho):
<mirror>
<id>central</id>
<name>Our mirror for central repo</name>
<url>http://<your host to>/nexus/content/repositories/central/</url>
<mirrorOf>central</mirrorOf>
</mirror>
Repository:
<repository>
<id>central</id>
<url>http://<your host to>/nexus/content/groups/public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
</repository>
this should fix your problems:
Eclipse Specific: if Errors prevails (e.g. unknown packaging) add lifecycle mappings via Window->Preferences->Maven->Discovery->Open Catalog and add Tycho Connector
Further you can add the lifecycle-mapping plugin that handles these lifecycle mappings in the eclipse environment: eclipse m2e lifecycle mappings
Maybe you end up here because you are running nexus for a long time.
I finally found the error Summary tab in the Repositories page.
At some point Maven Central decided to require https: and the URL listed in my configuration still was using http:.
Update the URL to use https:, Save and everything worked smoothly again!

Maven: Manually install a Jar from local folder and then go online and grab & install all its dependencies

In my project's pom file I have a dependency that I can see in Maven central repository, javax.enterprise:cdi-api:jar:1.0-SP4 but for some reason I am getting on some of my computers the following error while building the project (hence the build fails):
[INFO] Unable to find resource 'javax.enterprise:cdi-api:jar:1.0-SP4' in repository central (http://repo1.maven.org/maven2)
Any idea why I get this error only on some of my computers (I can say all the computers are using the same network)?
Why do I get this error at all? As far as I can see the jar IS in the maven central repository.
Even though maven could not locate/download the jar on some of the computers, I was able to download it manually through the browser on these computers. How could it be?
Okay, so I've downloaded the jar manually through the browser at one of the problematic computers. I want to install it manually to the local repository on this computer. So from the command line I do:
mvn install:install-file -DgroupId=javax.enterprise -DartifactId=cdi-api -Dversion=1.0-SP4 -Dpackaging=jar -Dfile=path/to/file
...but none of its dependencies has been downloaded as well. Is there some maven plugin to install the file to the local repository AND download & install all its dependencies to the local repository?
If so, please show me how to use it.
Please check in the computers where the jar are not downloaded if the settings.xml file present in .m2 folder is having the url of the jar needed. If not add them.
You are not able to download because maven searches in the urls defined in the settings.xml file only. So if the url is not present there, even if the jar is in the central repository it wont be downloaded.
In your browser you can navigate to that link and download. Maven wont be able to do it for the reason given in the second point
Ok, looks like you need to mention your repository path in your pom file, What is the url of the repository? is it local to your network or some open url? for example: http://repo2.mvn.org/???
Sample example would be like this::
<repositories>
<repository>
<id>codehausSnapshots</id>
<name>Codehaus Snapshots</name>
<releases>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<url>http://snapshots.maven.codehaus.org/maven2</url>
<layout>default</layout>
</repository>
</repositories>
If you could paste the pom file, it would be easier to help you.
To answer your 3 point: Check the settings.xml file in .m2\settings.xml, make sure that same content is present. Sometimes settings.xml are modified directly in maven installation directory. so you better check in that location also.
To Answer your 4th point: mvn install:install installs (in simple terms copies) an artifact (jar/ear/zip/any artifact that meant for distribution) into maven local repository or into remote repository. So in your command, you just tried to install only cdi-api jar.
Whenever you run mvn compile, mvn package, mvn install, maven will scan your pom file and downloads all dependency mentioned into your local repository. I would suggest you to look your local repository and see how is it creating directory structure.

Resources