Maven In Java and Repositories
Maven in Java
Maven is a build automation tool used primarily for Java projects. Maven can also build and manage projects written in C#, Ruby, Scala, and other languages.
Maven addresses two aspects of building software: how software is built and its dependencies.
It simplifies the build process like ANT. But it is too much more advanced than ANT. In the short term, we can tell Maven is a tool that can be used for building and managing any Java-based project. Maven makes the day-to-day work of Java developers easier and generally helps comprehend any Java-based project.
Maven Lifecycle
Maven is based on the central concept of a build lifecycle. There are three built-in build lifecycles: default, clean, and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, and the site lifecycle takes the creation of your project’s site documentation.
POM
POM is an XML file containing Maven’s project configuration details. It provides all the configurations required for a project. POM means project object model, and, as the name suggests, it defines the model of the project as well.
Artifact Repositories
A repository in Maven holds build artifacts and dependencies of varying types.
There are precisely two types of repositories: local and remote. The local storage is a directory on the computer where Maven runs. It caches remote downloads and contains temporary build artifacts you have not yet released.
Remote repositories refer to any other type of repository accessed by various protocols such as file:// and http://. These repositories might be remote repositories set up by a third party to provide their artifacts for downloading (for example, repo.maven.apache.org and uk.maven.org house Maven’s central repository). Other “remote” repositories may be internal repositories set up on a file or HTTP server within your company, used to share personal artifacts between development teams and for releases.
Local and remote repositories are structured the same way so that scripts can run on either side or be synced for offline use. The layout of the warehouses is entirely transparent to the Maven user.
Using Repositories
You should not need to do anything with the local repository regularly except clean it out if you are short on disk space (or erase it if you are willing to download everything again).
The remote repositories are used for downloading and uploading (if you have permission).
Downloading from a Remote Repository
Downloading in Maven is triggered by a project declaring a dependency not present in the local repository (or for a SNAPSHOT when the remote repository contains one newer). By default, Maven will download from the central repository.
To override this, specify a mirror, as shown in Using Mirrors for Repositories.
You can set this in your settings.xml file to use a specific mirror globally. However, it is common for a project to customize the repository in its pom.xml, and your setting will take precedence. If dependencies are not being found, check that you have not overridden the remote repository.
For more information on dependencies, see Dependency Mechanism.
Using Mirrors for the Central Repository
There are several official Central repositories geographically distributed. You can change your settings.xml file to use one or more mirrors. Instructions for this can be found in the Guide Using Mirrors for Repositories.
Building Offline
If you are temporarily disconnected from the internet, and you need to build your projects offline, you can use the offline switch on the CLI:
- mvn -o package
Many plugins honor the offline setting and do not perform any operations that connect to the internet. Some examples are resolving Javadoc links and link-checking the site.
Uploading to a Remote Repository
While this is possible for any remote repository, you must have permission to do so. To have someone upload to the central Maven repository, see Repository Center.
Internal Repositories
When using Maven, particularly in a corporate environment, connecting to the internet to download dependencies is unacceptable for security, speed, or bandwidth reasons. Therefore, it is desirable to set up an internal repository to house a copy of artifacts and publish personal artifacts.
Such an internal repository can be downloaded using HTTP or the file system (with a file:// URL) and uploaded using SCP, FTP, or a file copy.
As far as Maven is concerned, there is nothing special about this repository: it is another remote repository that contains artifacts to download to a user’s local cache and is a publish destination for artifact releases.
Additionally, you may want to share the repository server with your generated project sites. For more information on creating and deploying sites, see Creating a Site.
Setting up the Internal Repository
Setting up an internal repository requires that you have a place to put it and then copy required artifacts there using the same layout as in remote storage, such as repo.maven.apache.org.
It is not recommended that you scrape or rsync:// a full copy of central as there is a large amount of data there, and doing so will get you banned. You can use a program such as those described on the Repository Management page to run your internal repository’s server, download from the internet as required, and then hold the artifacts in your internal storage for faster downloading later.
The other options available are to download manually and vet releases, then copy them to the internal repository, or to have Maven download them for a user and manually upload the vetted artifacts to the internal storage used for releases. This step is the only one available for artifacts where the license forbids their distribution automatically, such as several J2EE JARs provided by Sun. Refer to the Guide to Coping with SUN JARs document for more information.
It should be noted that Maven intends to include enhanced support for such features in the future, including click-through licenses for downloading and verification of signatures.
Using the Internal Repository
Using the internal repository is quite simple. Simply make a change to add a repositories element:
<project>
…
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://myserver/repo</url>
</repository>
</repositories>
…
</project>
If your internal repository requires authentication, the id element can be used in your settings file to specify login information.
Deploying to the Internal Repository
One of the most important reasons to have one or more internal repositories is to be able to publish your private releases.
To publish to the repository, you must have access via one of SCP, SFTP, FTP, WebDAV, or the filesystem. Connectivity is accomplished with the various wagons. Some wagons may need to be added as extensions to your build.
https://mvnrepository.com is an excellent search engine; you will like
Note that if you search for the details on search.maven.org or mvnrepository.com when you find the project you want, you should see the projects pom.xml, open this, and then search for url. Eventually, you will likely see a tag <distributionManagement><repository> that has the URL details within it. You can then navigate to this url in your browser and see if you can find the required jar files etc. Otherwise, search on the other URLs that are picked up.
You can add this repository to your active profile in settings.xml if you want to go global on the whole m2 repo. Most of these repositories are used in most projects, so adding them individually to every pom is too much.
<profiles>
<profile>
<repositories>
<repository>
<id>mvnrepository</id>
<name>mvnrepository</name>
<url>http://www.mvnrepository.com</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>mvnrepository</activeProfile>
</activeProfiles>
Resources
- http://repo.maven.apache.org/maven2/
- https://maven.apache.org/guides/mini/guide-mirror-settings.html
- https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
- https://maven.apache.org/index.html
- https://maven.apache.org/repository/index.html
- https://maven.apache.org/guides/mini/guide-site.html
- https://mvnrepository.com/