Description:
Java programmers often choose Maven as a build tool because of its ability to pull together Jar file (and other artifact) dependencies from a Maven.org server on the internet.
The specific version of the Java Jar file can be specified and easily upgraded.
This dependency management helps reduce the practice of holding Jar file dependencies in a CM system (ie Git, Subversion, CVS) which is best used to manage changes in text files and not binary files.
Multiple options are available to host your own maven network repository server:
Server | Description | Web management interface |
Standard Maven | Use the Apache httpd server to front-end a Maven repository. | No |
Artifactory | A Tomcat based web app with web based support tools. | Yes |
Archiva | The Apache Archiva project is a Tomcat based front-end a generic artifact repository capable of operating as a Maven repository. | Yes |
Dead Simple Maven Proxy (DSMP) | A repository server which also acts as a filter when Maven accesses the internet. | No |
This tutorial describes how to configure a "Standard Maven" repository server using an Apache http server.
Server Configuration
Configure the web server to be able to index and traverse the file system.
The location can be any where you choose on the file system. For this example we chose /srv/maven2/
Configure Apache httpd:
File:
/etc/httpd/conf.d/maven2.conf
01 | Alias /maven2 /srv/maven2 |
02 | < Directory /srv/maven2> |
03 | Options Indexes FollowSymLinks |
04 | AllowOverride AuthConfig |
07 | ReadmeName README.html |
08 | HeaderName HEADER.html |
09 | IndexIgnore HEADER* README* |
10 | IndexOptions Charset=UTF-8 |
11 | IndexOptions FancyIndexing VersionSort NameWidth=* HTMLTable |
12 | AddDescription "XML data files" *.xml |
13 | AddDescription "Java jar files" *.jar |
14 | AddDescription "Zip compressed files" *.zip |
15 | AddDescription "Gzip compressed tar ball files" *.tar.gz |
18 | # AuthName "Megacorp directory services login" |
19 | # AuthBasicProvider ldap |
20 | # AuthzLDAPAuthoritative on |
22 | # Include authorized-users.txt |
Generate the Maven 2 directory structure:
Here we populate the repository by copying Apache commons-lang3 from the maven.org repository into our repository:
Generate the directory structure, the Maven metadata and support files:
cd /srv/maven2/
mkdir -p org/apache/commons/commons-lang3
cd org/apache/commons/commons-lang3
wget http://repo1.maven.org/maven2/org/apache/commons/commons-lang3/maven-metadata.xml
md5sum maven-metadata.xml > maven-metadata.xml.md5
sha1sum maven-metadata.xml > maven-metadata.xml.sha1
Edit maven-metadata.xml to reflect the proposed archive in your repository.
In this case we intend to only store release version 3.1 and no others:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
03 | < groupId >org.apache.commons</ groupId > |
04 | < artifactId >commons-lang3</ artifactId > |
07 | < release >3.1</ release > |
09 | < version >3.1</ version > |
11 | < lastUpdated >20170612113459</ lastUpdated > |
Populate the repository with the artifacts (JAR files):
mkdir 3.1
cd 3.1
wget -e robots=off --user-agent=Mozilla/5.0 --reject="index.html*" -nd -r --no-parent http://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.1/
cd /srv/maven2/
chown -R apache.apache *
chmod ugo+r -R *
Wget is used to copy the artifact contents from the maven.org repo.
Note that ownership and permissions are modified so that the Apache httpd webs server can read the files.
Using the Maven Command to Load the Repository:
Specify the JAR file, version number and repository location to generate the repository directory tree and locate contents.
$ ls
myJarLib.jar
$ mvn install:install-file -DgroupId=com.megacorp.projectx -DartifactId=myJarLib -Dpackaging=jar -Dversion=0.9.0-SNAPSHOT -Dfile=myJarLib.jar -DlocalRepositoryPath=/srv/maven2/
This will generate:
- /src/maven2/com/corp/pkga/myJarLib/0.9.0-SNAPSHOT/maven-metadata-local.xml - rename this to maven-metadata.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
03 | < groupId >com.megacorp.projectx</ groupId > |
04 | < artifactId >myJarLib</ artifactId > |
07 | < version >0.9.0-SNAPSHOT</ version > |
09 | < lastUpdated >20170705233506</ lastUpdated > |
- /src/maven2/com/megacorp/projectx/myJarLib/0.9.0-SNAPSHOT/myJarLib-0.9.0-SNAPSHOT.jar - copies and renames the file
- /src/maven2/com/megacorp/projectx/myJarLib/0.9.0-SNAPSHOT/myJarLib-0.9.0-SNAPSHOT.pom
1 | <? xml version = "1.0" encoding = "UTF-8" ?> |
4 | < modelVersion >4.0.0</ modelVersion > |
5 | < groupId >com.megacorp.projectx</ groupId > |
6 | < artifactId >myJarLib</ artifactId > |
7 | < version >0.9.0-SNAPSHOT</ version > |
8 | < description >POM was created from install:install-file</ description > |
- /src/maven2/com/megacorp/projectx/myJarLib/0.9.0-SNAPSHOT/maven-metadata-local.xml - rename this to maven-metadata.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < metadata modelVersion = "1.1.0" > |
03 | < groupId >com.megacorp.projecx</ groupId > |
04 | < artifactId >myJarLib</ artifactId > |
05 | < version >0.9.0-SNAPSHOT</ version > |
08 | < localCopy >true</ localCopy > |
10 | < lastUpdated >20170705233506</ lastUpdated > |
13 | < extension >jar</ extension > |
14 | < value >0.9.0-SNAPSHOT</ value > |
15 | < updated >20170705233506</ updated > |
18 | < extension >pom</ extension > |
19 | < value >0.9.0-SNAPSHOT</ value > |
20 | < updated >20170705233506</ updated > |
Using your Maven repository:
Specify the Maven2 repository server in your $HOME/.m2/settings.xml Maven configuration file:
07 | < name >Megacorp Repository</ name > |
09 | < layout >default</ layout > |
11 | < enabled >true</ enabled > |
12 | < updatePolicy >always</ updatePolicy > |
13 | < checksumPolicy >fail</ checksumPolicy > |
18 | < name >Megacorp Repository</ name > |
20 | < layout >default</ layout > |
22 | < enabled >true</ enabled > |
23 | < updatePolicy >always</ updatePolicy > |
24 | < checksumPolicy >fail</ checksumPolicy > |
Specify the dependency in your pom.xml Maven file. Repositories can also be listed:
05 | < modelVersion >4.0.0</ modelVersion > |
06 | < groupId >test</ groupId > |
07 | < artifactId >test</ artifactId > |
08 | < packaging >jar</ packaging > |
09 | < version >1.0-SNAPSHOT</ version > |
16 | < layout >default</ layout > |
18 | < enabled >true</ enabled > |
19 | < updatePolicy >always</ updatePolicy > |
20 | < checksumPolicy >fail</ checksumPolicy > |
26 | < layout >default</ layout > |
28 | < enabled >true</ enabled > |
29 | < updatePolicy >always</ updatePolicy > |
30 | < checksumPolicy >fail</ checksumPolicy > |
36 | < groupId >org.apache.commons</ groupId > |
37 | < artifactId >commons-lang3</ artifactId > |
38 | < version >3.1</ version > |

Books:
 |
"Maven Essentials"
by Prabath Siriwardena
ISBN # 178398676X, Packt Publishing
For Java developers
|
|