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.
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/
File: /etc/httpd/conf.d/maven2.confAlias /maven2 /srv/maven2 <Directory /srv/maven2> Options Indexes FollowSymLinks AllowOverride AuthConfig Order allow,deny Allow from all ReadmeName README.html HeaderName HEADER.html IndexIgnore HEADER* README* IndexOptions Charset=UTF-8 IndexOptions FancyIndexing VersionSort NameWidth=* HTMLTable AddDescription "XML data files" *.xml AddDescription "Java jar files" *.jar AddDescription "Zip compressed files" *.zip AddDescription "Gzip compressed tar ball files" *.tar.gz # AuthType Basic # AuthName "Megacorp directory services login" # AuthBasicProvider ldap # AuthzLDAPAuthoritative on # AuthLDAPURL "ldap://ldap.megacorp.com:389/ou=person,o=megacorp.com,c=us?uid?sub" # Include authorized-users.txt </Directory>
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:
<?xml version="1.0" encoding="UTF-8"?> <metadata> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <versioning> <latest>3.1</latest> <release>3.1</release> <versions> <version>3.1</version> </versions> <lastUpdated>20170612113459</lastUpdated> </versioning> </metadata>
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
<?xml version="1.0" encoding="UTF-8"?> <metadata> <groupId>com.megacorp.projectx</groupId> <artifactId>myJarLib</artifactId> <versioning> <versions> <version>0.9.0-SNAPSHOT</version> </versions> <lastUpdated>20170705233506</lastUpdated> </versioning> </metadata>
- /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
<?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.megacorp.projectx</groupId> <artifactId>myJarLib</artifactId> <version>0.9.0-SNAPSHOT</version> <description>POM was created from install:install-file</description> </project>
- /src/maven2/com/megacorp/projectx/myJarLib/0.9.0-SNAPSHOT/maven-metadata-local.xml - rename this to maven-metadata.xml
<?xml version="1.0" encoding="UTF-8"?> <metadata modelVersion="1.1.0"> <groupId>com.megacorp.projecx</groupId> <artifactId>myJarLib</artifactId> <version>0.9.0-SNAPSHOT</version> <versioning> <snapshot> <localCopy>true</localCopy> </snapshot> <lastUpdated>20170705233506</lastUpdated> <snapshotVersions> <snapshotVersion> <extension>jar</extension> <value>0.9.0-SNAPSHOT</value> <updated>20170705233506</updated> </snapshotVersion> <snapshotVersion> <extension>pom</extension> <value>0.9.0-SNAPSHOT</value> <updated>20170705233506</updated> </snapshotVersio>n </snapshotVersions> </versioning> </metadata>
Specify the Maven2 repository server in your $HOME/.m2/settings.xml Maven configuration file:
<profiles> <profile> <id>dev</id> <repositories> <repository> <id>central</id> <name>Megacorp Repository</name> <url>http://maven2.megacorp.com/maven2</url> <layout>default</layout> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> <repository> <id>snapshots</id> <name>Megacorp Repository</name> <url>http://maven2.megacorp.com/maven2</url> <layout>default</layout> <releases> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </releases> </repository> </repositories> </profile> </profiles>
Specify the dependency in your pom.xml Maven file. Repositories can also be listed:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>test</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>test</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>central</id> <url>http://maven2.megacorp.com/maven2</url> <layout>default</layout> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> <repository> <id>snapshots</id> <url>http://maven2.megacorp.com/maven2</url> <layout>default</layout> <releases> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </releases> </repository> </repositories> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency> </dependencies> </project>
"Maven Essentials"
by Prabath Siriwardena ISBN # 178398676X, Packt Publishing For Java developers |
|