Section 5.2. Creating Your Own Remote Maven Repository


5.2. Creating Your Own Remote Maven Repository

By default, when you install Maven, it is configured to use http://www.ibiblio.org/maven as the remote repository; ibiblio provides a comprehensive selection of free and open source artifacts. This repository is appropriate if you're developing a free and open source product, but what if your project needs to depend on either commercial artifacts or artifacts internal to your company? For example, if you need to depend on a commercial JDBC driver, you will need to set up your own remote Maven repository.

5.2.1. How do I do that?

This is really simple: all you need is a web server. You can use any web server you like, be it Apache, Tomcat, Microsoft IIS, etc. The only recommendation is that it supports the if-modified-since HTTP header (but we think they all do!), as this is the internal mechanism used by Maven to decide whether an artifact needs to be downloaded to your local repository.

Let's use Tomcat. Download it from http://jakarta.apache.org/site/downloads/downloads_tomcat.html (at the time of this writing the latest version available in the 4.x series was Tomcat 4.1.31). Install it anywhere you wishsay, in C:\apps\jakarta-tomcat-4.1.31 if you're on Windowsand start it using bin/startup.bat (for Windows) or bin/startup.sh (for Unix). By default, Tomcat will start on port 8080. If you're already using this port, edit the config/server.xml file and modify the port. Verify that you have it working by opening a browser and typing http://localhost:8080 (you should also try to access it from another machine using http://[hostname]:8080).

Now, create the repository directory. Go in webapps/ROOT (this directory will be created the first time you start Tomcat). It is mapped to the / context, which means that this is the directory served when you type http://localhost:8080. Create a subdirectory called repository/. This will be our remote Maven repository location which will be accessed with the http://localhost:8080/repository URL. You should have a directory structure similar to the one in Figure 5-2. Now you can copy any artifact in your newly created remote repository. Its directory structure is the same as that of your local repository.

Figure 5-2. Your new remote Maven repository showing proper directory structure


Now that you have your new remote repository set up, let's discover how to use it for your Maven projects. A Maven property named maven.repo.remote controls where Maven looks for its remote repository. It is hardcoded to be http://www.ibiblio.org/maven by default. There are several recommended places where you could change it:

  • In your ~user/build.properties file

  • In a build.properties file in your project's directory

  • In your project's project.properties file if you wish to share this location with other team members

Modify your build.properties file so that it contains the following line:

maven.repo.remote = http://[hostname]:8080/repository

You can also specify more than one remote repository. Maven will try to find the artifacts in the first repository. If it doesn't find them, it will look in the second repository, etc. For example, if you wish to define your own repository in addition to ibiblio, you would write:

maven.repo.remote = http://[hostname]:8080/repository, http://www.ibiblio.org/maven

If your project needs to use a commercial artifact, such as a commercial JDBC driver from Oracle, you would put that commercially licensed product's JAR files in your own remote repository and continue to use the ibiblio Maven repository for open source dependencies.


Tip: Some well-known JARs in the Java world, such as activation, javamail, jaxp, jca, jdbc, and jndi, are used by lots of projects. You might be surprised not to find them on ibiblio. The reason is that their licensing does not allow for their redistribution[1]. Putting them in your own remote repository is thus a very good idea.

5.2.2. What just happened?

You have discovered how to create your own Maven remote repository by hand. There are three ways you can use it:

  • As a placeholder for your own private artifacts (i.e., all artifacts that cannot be found on ibiblio). In this case you would usually define a maven.repo.remote property pointing to both your private remote repository and the ibiblio one.

  • As a way to control all the artifacts that are allowed. In this case, you would define a single remote repository in your maven.repo.remote property (your private repository). You would populate it manually with all the artifacts from ibiblio that you need. Whenever a new artifact is required by a project you would add it to your remote repository. This strategy is well suited for companies that wish to control the artifacts they have in their remote repositories.

  • As a brute force approach to caching an existing remote repository (such as ibiblio). A more evolved solution would be to use maven-proxy (http://maven-proxy.codehaus.org/).

5.2.3. What about...

...momentarily disabling the remote repository to work in offline mode?

If you're on the road or simply if you don't have network or Internet access, you can tell Maven to work in offline mode by running Maven with the -o option (maven -o <goals>) or by setting the maven.mode.online property to false. We mentioned this in Chapter 1, but it is such a common question we thought we'd bring it up again. If you need to use Maven while disconnected from the Internet, use the -o flag, and Maven will not attempt to connect to any remote repositories. Be aware that in this case Maven will only use the artifacts present in the local repository, and the build will fail if a required dependency is not there.

If you are working in a café without WiFi, use the -o flag to work "off-line."



Maven. A Developer's Notebook
Maven: A Developers Notebook (Developers Notebooks)
ISBN: 0596007507
EAN: 2147483647
Year: 2003
Pages: 125

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net