Copy Maven repository to another computer - java

Is there some index or cache that needs to be refreshed or deleted when I copy over the Maven repository from another computer?
I would like to copy the local Maven repository from my laptop for use on another computer that is effectively offline. After copying the files from ~/.m2/repository it does not work as expected. When I execute mvn package -o I get an error that it cannot find one of the artifacts from the new repository:
[ERROR] Failed to execute goal on project [...]: Could not resolve dependencies for project [...]:
Cannot access central (https://repo.maven.apache.org/maven2) in offline mode and the artifact com.oracle:ojdbc6:jar:11.2.0.3.0 has not been downloaded from it before.
But I do have the artifact in the folder at ~/.m2/repository/com/oracle/ojdbc6/11.2.0.3.0 that was copied in from the other computer. (This particular one is not public; it is stored on our company Nexus server which cannot be reached from the target system.)

Maven uses the "update policy" in the settings.xml file to decide whether to download the artifact or to use the one in the local repository if available.
If you don't want Maven to update your dependencies, you should configure <updatePolicy>never</updatePolicy>. You also need to use the same repository <id>s in both locations.
So if you are only using Maven's Central Repository, your repository configuration would look something like this:
<repository>
<id>central</id>
<name>Maven Central</name>
<url>https://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>

Related

mvn archetype:generate using internal maven repo

I wanted to create a maven project using "mvn archetype:generate ..." to create a maven project Skeleton. I looked up online and come up with this:
in settings.xml
<profile>
<id>PlatMigr</id>
<repositories>
<repository>
<id>internal repo</id>
<name>IT Maven Central</name>
<url>https://<internal maven repo></url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
From command line:
mvn archetype:generate -DarchetypeCatalog=internal -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.myproj -DartifactId=ProjName
However maven still tried to download artifacts from official central maven repo:
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
We have a firewall installed to stop internet access, that's why I struggle to use internal repo in the first place. Anyone can advise on how to setup and use internal repo to create maven projects?
Edit you settings.xml in m2_home, add mirror to you nexus and create & set to active profile without central repositories

Different repository in DistributionManagement and Repositories

In <DistributionManagement> ... </DistributionManagement>
and <Repositories> ... <Repositories> sections, there can be a
<Repository> ... </Repository>
definition. What's the difference between the two definition? This is one example:
<distributionManagement>
<downloadUrl>https://github.com/marytts/marytts/releases</downloadUrl>
<repository>
<id>bintray</id>
<url>https://api.bintray.com/maven/marytts/marytts/marytts</url>
</repository>
<snapshotRepository>
<id>bintray</id>
<url>http://oss.jfrog.org/artifactory/oss-snapshot-local</url>
</snapshotRepository>
</distributionManagement>
And
<repositories>
<repository>
<id>marytts-dependencies</id>
<name>marytts-dependencies</name>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>file://${project.local.repository.path}</url>
</repository>
<repository>
<id>central</id>
<name>jcenter</name>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
Distribution Management
Distribution management acts precisely as it sounds: it manages the
distribution of the artifact and supporting files generated throughout
the build process. Starting with the last elements first:
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.
Deploy using the repository layout
To deploy your file using the maven layout you should define the distribution management location :
<project>
...
<distributionManagement>
<repository>
<id>myrepository</id>
<url>file:D:/repository/</url>
</repository>
</distributionManagement>
</project>
Then you just need to execute the following command to get you artifact copied in your file system location
Maven command to deploy a file in the local file system
mvn deploy
Site Distribution
More than distribution to the repositories, distributionManagement is
responsible for defining how to deploy the project's site and
documentation.
In pom.xml, configure where to deploy your site within distributionManagement tag.
<distributionManagement>
<site>
<id>mkyongserver</id>
<url>dav:http://127.0.0.1/sites/</url>
</site>
</distributionManagement>
Relocation
Projects are not static; they are living things (or dying things, as
the case may be). A common thing that happens as projects grow, is
that they are forced to move to more suitable quarters. For example,
when your next wildly successful open source project moves under the
Apache umbrella, it would be good to give your users as heads-up that
the project is being renamed to org.apache:my-project:1.0. Besides
specifying the new address, it is also good form to provide a message
explaining why.
Repositories
Repositories are collections of artifacts which adhere to the Maven
repository directory layout. In order to be a Maven repository
artifact, a POM file must live within the structure
$BASE_REPO/groupId/artifactId/version/artifactId-version.pom.
$BASE_REPO can be local (file structure) or remote (base URL); the
remaining layout will be the same. Repositories exist as a place to
collect and store artifacts. Whenever a project has a dependency upon
an artifact, Maven will first attempt to use a local copy of the
specified artifact. If that artifact does not exist in the local
repository, it will then attempt to download from a remote repository.
The repository elements within a POM specify those alternate
repositories to search.
The repository is one of the most powerful features of the Maven
community. The default central Maven repository lives on
http://repo.maven.apache.org/maven2/. Another source for artifacts not
yet in iBiblio is the Codehaus snapshots repo.
Be remember you can add only one <repository> and one <snapshotRepository> child inside <distributionManagement>
How to declare proxy
Just go to Maven-> conf-> setting.xml file and add proxy
<proxies>
<proxy>
<id>myproxy</id>
<active>true</active>
<protocol>http</protocol>
<host>global.proxy.mycompany.com</host>
<port>8000</port>
<username></username>
<password></password>
<nonProxyHosts>localhost,127.0.0.1</nonProxyHosts>
</proxy>
</proxies>

Cannot install any new libraries in Eclipse (Luna) Maven

This error just started appearing, with any new maven dependencies I install. The dependency shows up in the Maven dependencies tree empty, and the project reports, for example:
Description Resource Path Location Type Archive for required library: '~/.m2/repository/com/ning/async-http-client/1.8.14/async-http-client-1.8.14.jar' in project 'LB' cannot be read or is not a valid ZIP file LB Build path Build Path Problem
I viewed the POM file for each new dependency, and they all have this in them:
<HTML>
<BODY>
This repo has moved to repo.boundlessgeo.com. Please update your settings.
</BODY>
</HTML>
So i discovered that the OpenGeo Maven Repo has moved, based on this:
<!-- <repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>opengeo</id>
<name>OpenGeo Maven Repo</name>
<url>http://repo.opengeo.org</url>
</repository> -->
Replaced with this:
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>boundless</id>
<name>Boundless Maven Repository</name>
<url>http://repo.boundlessgeo.com/main</url>
</repository>
Even though I fixed this issue, all other dependencies still fail to install. Any idea why the POM files for any other repo keep getting the HTML notice from OpenGeo? How can I fix?
Here's the entire repo section of my pom.xml
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repo</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repo</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>opengeo</id>
<name>OpenGeo Maven Repo</name>
<url>http://repo.opengeo.org</url>
</repository>
<repository>
<id>central</id>
<name>The Central Repository</name>
<url>http://repo1.maven.org/maven2/</url>
</repository>
Updated answer:
If you have a repository defined in your pom.xml which provides static content instead of a proper Maven repository (in this case http://repo.opengeo.org, which has moved to http://repo.boundlessgeo.org), Maven will download the static content as the pom, jar, etc and complain about it being corrupted later. These invalid files will remain in your local repository until you delete them, even after you fix the problem repository.
The simplest way to get rid of the invalid files would be to delete your entire ~/.m2/repository and download all artifacts fresh. However, you can also just delete the problem files (adjust the grepped message according to the static content in your invalid files):
grep -lrIZ "This repo has moved to repo.boundlessgeo.com" ~/.m2/repository \
| xargs -r0 rm
Then do a normal mvn clean install and valid artifacts should be downloaded from the corrected repository.
See the original question for details on updating http://repo.opengeo.org to http://repo.boundlessgeo.org.
Note: Maven is likely downloading the invalid static content because repo.opengeo.org is returning a status code of 200.
Original answer:
Is it possible that you've somehow configured the old OpenGeo Maven Repo to be a mirror for all respositories? Maybe in your local settings.xml, or in a Nexus artifact repository?
I ran into something similar, where our Nexus server somehow had a bunch of non-OpenGeo artifacts in our proxied OpenGeo repository. Once the OpenGeo/BoundlessGeo repo moved, I got the "This repo has moved" message until I deleted the non-OpenGeo artifacts from the proxy repository.

Maven: metadata xml files downloaded often from remote repositories

I'm using Maven to handle a Java project. I thought that Internet connectivity was only needed in the 1st compile to download the required libraries from the remote repositories, but I get several download messages whenever I compile code. Messages like these:
Downloading: http://repo.maven.apache.org/maven2/org/eclipse/core/resources/maven-metadata.xml
Downloading: http://repository.springsource.com/maven/bundles/external/org/eclipse/core/resources/maven-metadata.xml
Downloading: http://repository.springsource.com/maven/bundles/release/org/eclipse/core/resources/maven-metadata.xml
Downloading: https://oss.sonatype.org/content/repositories/snapshots/org/eclipse/core/resources/maven-metadata.xml
Why that happens and how I could prevent it?
This usually happens when no version information is specified for the artifact.
maven-metadata.xml is a file which contains the <groupId>, <artifactId> and the versioning information about the various versions available for the dependency.
If the version of the artifact is not specified in the pom.xml, maven downloads this metadata file to check if the local repository contains the latest version.
So, you can avoid this download by specifying the version information of the artifact in pom.xml file, instead of changing the update policy which might affect the update process of other jar files in future.
The most important thing is to start using a repository manager furthermore check the configuration in your settings.xml file which can be configured to check the remote repositories (update policy).
1) Check that you indeed have these artifacts in your local repo
2) Check your repository configuration so you are using your repos only to download releases.
<project>
...
<repositories>
<repository>
<id>my-repo1</id>
<name>your custom repo</name>
<url>http://jarsm2.dyndns.dk</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
...
</project>
3) You can force maven to only use your local repo with the -o option:
mvn -o clean package

How do I get Maven to use the correct repositories?

I have just checked out some projects and need to build them, however I installed Maven quite some time ago (6 months maybe?) and really haven't used it since - the pom.xml for the project I have doesn't have this "http://repo1.maven.org/myurlhere" anywhere in it - it has the absolute url where the Maven repo is for the project, but Maven is still trying to download from the general Maven repo:
Macintosh:trunk$ mvn clean install
[INFO] Scanning for projects...
Downloading: http://repo1.maven.org/url/project/project/x.x/project-x.x.pom
[INFO] Unable to find resource 'url.project:project:pom:x.x' in repository central (http://repo1.maven.org/)
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
GroupId: url.project
ArtifactId: project
Version: x.x
Reason: Unable to download the artifact from any repository
url.project:project:pom:x.x
from the specified remote repositories:
central (http://repo1.maven.org/)
Can anyone help me with what I'm not doing right?
Basically, I have just checked the projects out from the command line, cd-ed into the directory and ran mvn clean install - nothing else.
Any help is greatly appreciated.
the pom.xml for the project I have doesn't have this "http://repo1.maven.org/myurlhere" anywhere in it
All projects have http://repo1.maven.org/ declared as <repository> (and <pluginRepository>) by default. This repository, which is called the central repository, is inherited like others default settings from the "Super POM" (all projects inherit from the Super POM). So a POM is actually a combination of the Super POM, any parent POMs and the current POM. This combination is called the "effective POM" and can be printed using the effective-pom goal of the Maven Help plugin (useful for debugging).
And indeed, if you run:
mvn help:effective-pom
You'll see at least the following:
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Plugin Repository</name>
<url>http://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
it has the absolute url where the maven repo is for the project but maven is still trying to download from the general maven repo
Maven will try to find dependencies in all repositories declared, including in the central one which is there by default as we saw. But, according to the trace you are showing, you only have one repository defined (the central repository) or maven would print something like this:
Reason: Unable to download the artifact from any repository
url.project:project:pom:x.x
from the specified remote repositories:
central (http://repo1.maven.org/),
another-repository (http://another/repository)
So, basically, maven is unable to find the url.project:project:pom:x.x because it is not available in central.
But without knowing which project you've checked out (it has maybe specific instructions) or which dependency is missing (it can maybe be found in another repository), it's impossible to help you further.
By default, Maven will always look in the official Maven repository, which is http://repo1.maven.org.
When Maven tries to build a project, it will look in your local repository (by default ~/.m2/repository but you can configure it by changing the <localRepository> value in your ~/.m2/settings.xml) to find any dependency, plugin or report defined in your pom.xml. If the adequate artifact is not found in your local repository, it will look in all external repositories configured, starting with the default one, http://repo1.maven.org.
You can configure Maven to avoid this default repository by setting a mirror in your settings.xml file:
<mirrors>
<mirror>
<id>repoMirror</id>
<name>Our mirror for Maven repository</name>
<url>http://the/server/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
This way, instead of contacting http://repo1.maven.org, Maven will contact your entreprise repository (http://the/server in this example).
If you want to add another repository, you can define a new one in your settings.xml file:
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>foo.bar</id>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<url>http://new/repository/server</url>
</repository>
</repositories>
You can see the complete settings.xml model here.
Concerning the clean process, you can ask Maven to run it offline. In this case, Maven will not try to reach any external repositories:
mvn -o clean
tl;dr
All maven POMs inherit from a base Super POM.
The snippet below is part of the Super POM for Maven 3.5.4.
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
I think what you have missed here is this:
https://maven.apache.org/settings.html#Servers
The repositories for download and deployment are defined by the repositories and distributionManagement elements of the POM. However, certain settings such as username and password should not be distributed along with the pom.xml. This type of information should exist on the build server in the settings.xml.
This is the prefered way of using custom repos. So probably what is happening is that the url of this repo is in settings.xml of the build server.
Once you get hold of the url and credentials, you can put them in your machine here: ~/.m2/settings.xml like this:
<settings ...>
.
.
.
<servers>
<server>
<id>internal-repository-group</id>
<username>YOUR-USERNAME-HERE</username>
<password>YOUR-PASSWORD-HERE</password>
</server>
</servers>
</settings>
EDIT:
You then need to refer this repository into project POM. The id internal-repository-group can be used in every project. You can setup multiple repos and credentials setting using different IDs in settings xml.
The advantage of this approach is that project can be shared without worrying about the credentials and don't have to mention the credentials in every project.
Following is a sample pom of a project using "internal-repository-group"
<repositories>
<repository>
<id>internal-repository-group</id>
<name>repo-name</name>
<url>http://project.com/yourrepourl/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
Basically, all Maven is telling you is that certain dependencies in your project are not available in the central maven repository. The default is to look in your local .m2 folder (local repository), and then any configured repositories in your POM, and then the central maven repository. Look at the repositories section of the Maven reference.
The problem is that the project that was checked in didn't configure the POM in such a way that all the dependencies could be found and the project could be built from scratch.

Categories