Created by James on Sun, Jun, 7, 2009 and filed under
Maven (2)
0 comments -
Add a comment
Whilst in most cases it is sufficient to rely solely on caching in the
.m2 directory, this is not always perfect. In our case we have a central build server which can create an entirely new build environment for each build - a feature which is often very useful. In these cases it is very helpful to install a proxy for Maven. Hopefully, this article will (at least slightly) illuminate that process...
The init script does not use a pid file but at least it does check whether stuff has already been loaded.
Creating the directory structure
The directories do not necessarily have to follow this pattern, but if they don't you'll need to change the configuration file later.
sudo
mkdir /etc/maven-proxy/
sudo
mv ~/maven-proxy-standalone-0.2.zip /etc/maven-proxy/
cd /etc/maven-proxy/
sudo unzip maven-proxy-standalone-0.2.zip
sudo
mkdir target
sudo
mkdir target/repo
sudo
mkdir target/repo-local
Adding the configuration file and owning the directory
Create the following permissions file:
################ GLOBAL SETTINGS# This is where maven-proxy stores files it has downloadedrepo.local.store=/opt/maven-proxy/target/repo#The port to listen on - not used if loaded as a webappport=9999#This is the base area that all files are loaded from. While it is possible to leave this blank, this behaviour#is deprecated and will be disabled in version 2.0. There are too many namespace conflicts caused by not using#a prefix.#The repository will be shown at http://localhost:9999/repository/#for the .war loaded into a webapp server, the default prefix is "repository" (edit the web.xml to change)# As maven doesn't like a trailing slash, this address shouldn't have one either.prefix=repository#This is the simple date format used to display the last modified date while browsing the repository.lastModifiedDateFormat=yyyy/MM/dd HH:mm:ss################ SNAPSHOT HANDLING#If you want the proxy to look for newer snapshots, set to truesnapshot.update=true################ M2 METADATA HANDLING#If you want the proxy to prevent looking for newer metadata, set to false (default is true)#metadata.update=false################ M2 POM HANDLING#If you want the proxy to look for newer POMs, set to true (default is false)pom.update=true################ PROMOTION HANDLING IS NOT CURRENTLY IMPLEMENTEDpromotion=false################ WEB INTERFACE# This defines the absolute URL the server should use to identify itself.# This can often be determined automatically, but we recommend you specify# it explicitly to prevent problems during startup.# The prefix will be added to this for the actual repository# i.e. proxy available at http://localhost:9999/, repository at http://localhost:9999/repositoryserverName=http://localhost:9999#If true, the repository can be browsedbrowsable=true#If true, the repository can be searchedsearchable=true#Not currently implemented. Will allow webdav access to the repository at some point.webdav=true#Stylesheet - if configured, will override the default stylesheet shipped with maven-proxy - absolute URLs only#eg. /maven-proxy/style.css, http://www.example.com/style.css#stylesheet=/maven-proxy/style.css#bgColor / bgColorHighlight are replaced in the built in stylesheet to produce a simple color scheme.#If a stylesheet is set, these are not used.bgColor=#14BbgColorHighlight=#94B#rowColor / rowColorHighlight are replaced in the built in stylesheet to produce a simple color scheme.#If a stylesheet is set, these are not used.rowColor=#CCFrowColorHighlight=#DDF################ PROXIES#This is just a hack, it should auto discover themproxy.list=################# REPOSITORIES#This is not just a hack, it specifies the order repositories should be checked#Note that the proxy adds a "/" which is why the urls aren't suffixed with a "/"repo.list=www-ibiblio-org,dist-codehaus-org#www.ibiblio.orgrepo.www-ibiblio-org.url=http://mirrors.ibiblio.org/pub/mirrors/maven2repo.dist-codehaus-org.proxy=repo.www-ibiblio-org.description=www.ibiblio.orgrepo.www-ibiblio-org.hardfail=truerepo.www-ibiblio-org.cache.period=3600repo.www-ibiblio-org.cache.failures=false#dist.codehaus.orgrepo.dist-codehaus-org.url=http://dist.codehaus.org/repo.dist-codehaus-org.proxy=repo.dist-codehaus-org.hardfail=falserepo.dist-codehaus-org.cache.period=3600repo.dist-codehaus-org.cache.failures=false
Change the user who owns the repository:
chmod -R root:root *
Creating the startup script
Update
- nohup was added to the command. This completely disassociates the process from the invoking terminal. If this isn't performed then we can't run as a daemon from a desktop launcher.
Create a script "/etc/init.d/maven-proxy":
. /etc/rc.d/init.d/functions[ -f /opt/maven-proxy/maven-proxy-standalone-0.2-app.jar ] ||
exit 5
prog=
"nohup java -jar /opt/maven-proxy/maven-proxy-standalone-0.2-app.jar /opt/maven-proxy/maven-proxy.properties"
pid=
`ps aux | grep maven-proxy-standalone-0.2-app.jar | grep -v grep | awk '{print $2}'`start() {
if [ $pid ]
then
echo -n $
"Proxy is already running - pid: $pid" echo_failure
RETVAL=1
else
echo -n $
"Starting maven proxy:"
cd / $prog 2>&1 > /var/log/maven-proxy.log & echo_success
RETVAL=$?
fi
echo}stop() {
echo
"Stopping proxy"
if [ $pid ]
then
echo -n $
"Killing process: $pid"
kill $pid
if [ $? -eq 0 ]
then echo_success
else echo_failure
fi
else
echo -n $
"Proxy is not running." echo_failure
fi
echo}
case
"$1"
in start)start;; stop)stop
RETVAL=$?;; restart) start[ $? -eq 0 ] && stop
RETVAL=$? ;; *)echo $
"Usage: $0 {start|stop|restart}"
RETVAL=2
esac
exit $RETVAL
Add the script into "chkconfig"
sudo chkconfig maven-proxy --add
Its now possible to configure maven-proxy using:
sudo chkconfig --levels 235 maven-proxy on
If you've got SELinux installed its useful to add a set of rules for the proxy. Especially as it seems to try some interesting, and seemingly insecure, call-outs occasionally
There are high chance your company is set up a firewall and force the developers to connect internet via using HTTP proxy. If you are using HTTP proxy, Maven may not able to download the dependency libraries outside. To bypass it, you have to enable the proxy setting in Maven configuration file “settings.xml“.
1. Maven configuration file
Find your Maven configuration file – e.g, {M2_HOME}/conf/settings.xml
2. Edit it “settings.xml”
Find the following pattern
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>
Comment out the proxy setting and fill in your proxy information.
<proxies>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.yourcompany.com</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>
4. Saved it, Done.
Apache Maven is able to connect to Internet via proxy server now.
P.S No restart is required, how do you restart Maven ? Maven is just a command , when you call it, it will read this file again.