Maven Mirror - how to bypass if mirror host is not available?

半腔热情 提交于 2019-12-01 05:38:30

问题


I have a maven mirror repository (Archiva) e.g.

<settings>
   <mirrors>     
        <mirror>
        <id>archiva</id>
        <mirrorOf>*</mirrorOf>
        <url>http://myMirrorHost:8080/archiva/repository/internal</url>     
    </mirror>
       </mirrors>
       ...

However this is behind a VPN, and sometimes I rather not use it / can't connect to the VPM

The issue is when building outside the VPN, I get this error

myMirrorHost: Unknown host myMirrorHost -> [Help 1]

When I would like it instead to timeout / not use the mirror if not found

Is that possible?


回答1:


Try running the build in off-line mode when not connected to the VPN

mvn -o clean package

Another option is to have a second Maven settings file for use when you're connected to a different network:

mvn -s $HOME/.m2/settings-alternative.xml clean package

In this case I'd also recommend specifying an alternative local repository location (within the settings file), so as to avoid build confusion.




回答2:


I had a similar situation and I changed the mirror settings to mirror central, not *:

<mirrors>     
 <mirror>
  <id>archiva</id>
  <mirrorOf>central</mirrorOf>
  <url>http://myMirrorHost:8080/archiva/repository/internal</url>     
 </mirror>
</mirrors>

Then in a profile, I define another repository, e.g. ibiblio:

<profile>
  <id>myprofile</id>

 <activation>
  <activeByDefault>true</activeByDefault>
 </activation>

  <repositories>
    <repository>
      <id>ibiblio.org</id>
       <name>ibiblio Mirror of http://repo1.maven.org/maven2/</name>
       <url>http://mirrors.ibiblio.org/pub/mirrors/maven2</url> 
    </repository>
  </repositories>  
</profile>

Now when something is not found in the mirror, or the mirror host is not reachable, maven tries the other repository (which is a mirror of central).




回答3:


I've always used a similar approach to Mark's anser which I wrote a script for to handle copying different configurations or removing the one that is there.

Basic usage is to first write a configuration file of the form ~/.m2/settings.<config>.xml where <config> is the name of a configuration, then setting the config by invoking setmvn [config], not specifying will remove the config in place. This will symlink to the configuration file so if your tools modify the current config it will modify the config file currently in place.

#!/bin/bash

[ "$#" -lt 2 ] || {
    echo "Usage: $(basename $0) [profile-name]" >&2
    exit 1
}

if [ -z "$1" ] ; then
    if [ -f ~/.m2/settings.xml ] ; then
        echo "Configuration removed."
        rm -f ~/.m2/settings.xml
    else
        echo "Configuration not in place, nothing to do." >&2
    fi
else
    if [ -f ~/.m2/"settings.$1.xml" ] ; then
        [ -f ~/.m2/settings.xml ] && rm -f ~/.m2/settings.xml
        ln -s ~/.m2/"settings.$1.xml" ~/.m2/settings.xml
        echo "Configuration set to $1."
    else
        echo "Configuration not found: $1" >&2
        exit 2
    fi
fi


来源:https://stackoverflow.com/questions/10627637/maven-mirror-how-to-bypass-if-mirror-host-is-not-available

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!