question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
damienmgcommented, Sep 28, 2017

😦 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).

1reaction
fyearscommented, Sep 26, 2017

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

bazel tell external dependency maven to use local jar
One workaround is to use your own little reverse proxy, which reads from ~/.m2/repository and tell Bazel to download from localhost .
Read more >
Settings Reference - Maven
The settings element in the settings.xml file contains elements used to define values which configure Maven execution in various ways, like the pom.xml...
Read more >
Configure generate_workspace with Maven's settings.xml
Does anyone know if it's possible to configure Bazel with custom Maven settings? Basically I'd just want Bazel to read my ~/.m2/settings.xml file...
Read more >
Migrating from Maven to Bazel
Note: While Bazel supports downloading and publishing Maven artifacts with rules_jvm_external, it does not directly support Maven-based ...
Read more >
Migrating to Bazel as a Build Tool - Why We Left Maven
But Maven executes all unit tests, no matter what is the change in code ... We chose Bazel as our build system for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found