bazel: bazel does NOT follow `~/.m2/settings.xml` maven repos

Description of the problem / feature request / question:

I am using bazel in a restricted network environment where maven.org couldn’t be accessed BUT an internal maven mirror/repo is provided. My ~/.m2/settings.xml is correctly configured, and all of gradle, mvn, intellij can download artifacts from internal mirror/repo read from settings.xml without problems.

  • Expectation: bazel can also automatically download artifacts using configurations in ~/.m2/settings.xml.
  • Actual behaviour: bazel still downloads things from https://repo1.maven.org/maven2.

If possible, provide a minimal example to reproduce the problem:

Use a modified version of official https://github.com/bazelbuild/examples/tree/master/java-maven by removing maven_server().

$ git clone https://github.com/bazelbuild/examples/
$ cd examples/java-maven
$ cat > WORKSPACE << EOF
maven_jar(
    name = "com_google_guava_guava",
    artifact = "com.google.guava:guava:18.0",
)
EOF

$ bazel info release
WARNING: ignoring http_proxy in environment.
release 0.6.0rc3
$ bazel build :java-maven
WARNING: ignoring http_proxy in environment.
ERROR: ~/examples/java-maven/BUILD:3:1: no such package '@com_google_guava_guava//jar': Failed to fetch Maven dependency: Could not transfer artifact com.google.guava:guava:jar:18.0 from/to com_google_guava_guava (https://repo1.maven.org/maven2/): connect timed out and referenced by '//:java-maven-lib'
ERROR: Analysis of target '//:java-maven' failed; build aborted
INFO: Elapsed time: 10.749s
FAILED: Build did NOT complete successfully (9 packages loaded)
$ cat ~/.m2/settings.xml # internal repo working for gradle and mvn
<settings>
  <mirrors>
    <mirror>
      <id>internal mirror</id>
      <url>http://maven_internal.example.com/</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  ...
</settings>

if I manually add lines below to WORKSPACE, bazel works. But I don’t want to do this workaround because it should not be checked into version control.

maven_server(
    name = "default",
    url = "http://maven_internal.example.com/",
)

Environment info

  • Operating System:

    Ubuntu 16.04 LTS

  • Bazel version (output of bazel info release):

    $ sudo apt-get install bazel # using official testing repo as of 20170924
    $ bazel info release
    WARNING: ignoring http_proxy in environment.
    release 0.6.0rc3
    

Have you found anything relevant by searching the web?

(e.g. StackOverflow answers, GitHub issues, email threads on the bazel-discuss Google group)

According to official doc, shouldn’t bazel follow ~/.m2/settings.xml??

They seem to be relevant, however I still don’t know how to “fix” the problem:

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (13 by maintainers)

Most upvoted comments

😦 Sorry that wasn’t for starting a flame war, I just want to avoid spreading the usage of bind whereas we are thinking of removing it. (as @ittaiz said there is a long discussion on the pros and cons on #1952 to not start another one here).

So, after a lot of try and error, I finally understood what’s going on here.

bazel provides built-in maven_jar as well as the alternative extension-skylark-based maven_jar. The former one does things faster but not follow maven settings, and the later one directly calls system maven thus as a side effect it reads maven’s ~/.m2/settings.xml as expected.

The solution to my problem is quite simple: load the extension-based maven_jar:

$ cat > WORKSPACE << EOF
load("@bazel_tools//tools/build_defs/repo:maven_rules.bzl",
  "maven_jar", "maven_dependency_plugin")

maven_jar(
    name = "com_google_guava_guava",
    artifact = "com.google.guava:guava:18.0",
)
EOF

Though I found a workaround, I’d like to keep this issue open, as the default behavior is extremely confusing and no any available/useful doc found. At least, I could not find any “global settings” to define maven mirror/repo (and/or, for example, python pip mirror, etc), and the doc here (“A path to a settings.xml file. Used for testing. If unspecified, this defaults to using $M2_HOME/conf/settings.xml for the global settings and $HOME/.m2/settings.xml for the user settings.”) is very confusing – it works only(?) for testing!