Skip to content

Commit

Permalink
Make the settings attribute of Skylark maven_jar and maven_aar a label.
Browse files Browse the repository at this point in the history
Fixes bazelbuild#2117.

The settings attribute allows you to optional specify a custom Maven
settings.xml file as a label. Previously, this attribute was an absolute path.
As an absolute path it cannot really be used to specify a file in the workspace
because each developer may install the workspace in a different location.
And if the settings.xml cannot be included in the workspace, the developers may
as well use one of Maven's default locations for settings.xml.

Now the attribute is a label. An example use case is:

    $ cat WORKSPACE
    load("@bazel_tools//tools/build_defs/repo:maven_rules.bzl")
    maven_jar(
        name = "guava",
        artifact = "com.google.guava:guava:19.0",
        settings = "//:my_custom_settings.xml",
    )
    $ cat BUILD
    java_library(
        srcs = glob(["**/*.java"]),
        deps = ["@guava//jar"],
    )
    $ cat my_custom_settings.xml
    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0
                     https://maven.apache.org/xsd/settings-1.0.0.xsd">
      <mirrors>
        <mirror>
          <id>planetmirror.com</id>
          <name>PlanetMirror Australia</name>
          <url>http://downloads.planetmirror.com/pub/maven2</url>
          <mirrorOf>central</mirrorOf>
        </mirror>
      </mirrors>
    </settings>

Note that `settings = "my_custom_settings.xml"` will not work as the workspace
rule will not know to look in the main workspace.

RELNOTES[INC]: Skylark maven_jar and maven_aar settings attribute is now a label so
it can be checked into your workspace.

--
PiperOrigin-RevId: 140861633
MOS_MIGRATED_REVID=140861633
  • Loading branch information
aj-michael authored and kchodorow committed Dec 2, 2016
1 parent 432e83e commit a110ac4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/test/shell/bazel/maven_skylark_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ EOF
}

# This function takes an optional url argument: mirror. If one is passed, a
# mirror of maven central will be set for the url.
# mirror of maven central will be set for the url. It also creates a `BUILD`
# file in the same directory as $local_maven_settings so that it can be used as
# a label in WORKSPACE.
function setup_local_maven_settings_xml() {
local_maven_settings_xml=$(pwd)/settings.xml
local_maven_settings_xml=settings.xml
touch $(pwd)/BUILD
cat > $local_maven_settings_xml <<EOF
<!-- # DO NOT EDIT: automatically generated settings.xml for maven_dependency_plugin -->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
Expand Down Expand Up @@ -89,7 +92,7 @@ maven_jar(
name = 'endangered',
artifact = "com.example.carnivore:carnivore:$version",
sha1 = '$sha1',
settings = '$local_maven_settings_xml',
settings = '//:$local_maven_settings_xml',
)
bind(name = 'mongoose', actual = '@endangered//jar')
Expand Down Expand Up @@ -132,7 +135,7 @@ maven_aar(
name = "herbivore",
artifact = "com.example.carnivore:herbivore:1.21",
sha1 = "$sha1",
settings = "$local_maven_settings_xml",
settings = "//:$local_maven_settings_xml",
)
EOF
bazel build //java/com/app || fail "Expected build to succeed"
Expand All @@ -155,7 +158,7 @@ load("@bazel_tools//tools/build_defs/repo:maven_rules.bzl", "maven_jar")
maven_jar(
name = 'endangered',
artifact = "com.example.carnivore:carnivore:$version",
settings = '$local_maven_settings_xml',
settings = '//:$local_maven_settings_xml',
)
bind(name = 'mongoose', actual = '@endangered//jar')
Expand All @@ -176,7 +179,7 @@ load("@bazel_tools//tools/build_defs/repo:maven_rules.bzl", "maven_jar")
maven_jar(
name = 'endangered',
artifact = "com.example.carnivore:carnivore:$version",
settings = '$local_maven_settings_xml',
settings = '//:$local_maven_settings_xml',
)
bind(name = 'mongoose', actual = '@endangered//jar')
Expand All @@ -202,7 +205,7 @@ maven_jar(
name = 'endangered',
artifact = "com.example.carnivore:carnivore:1.24",
sha1 = '$wrong_sha1',
settings = '$local_maven_settings_xml',
settings = '//:$local_maven_settings_xml',
)
bind(name = 'mongoose', actual = '@endangered//jar')
Expand All @@ -225,7 +228,7 @@ maven_jar(
name = 'endangered',
artifact = "com.example.carnivore:carnivore:$version",
server = "attr_not_implemented",
settings = "$local_maven_settings_xml",
settings = "//:$local_maven_settings_xml",
)
bind(name = 'mongoose', actual = '@endangered//jar')
Expand Down
7 changes: 4 additions & 3 deletions tools/build_defs/repo/maven_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ def _file_exists(ctx, filename):
def _mvn_download(ctx, paths, fully_qualified_name):
# If a custom settings file exists, we'll use that. If not, Maven will use the default settings.
mvn_flags = ""
if hasattr(ctx.attr, "settings") and ctx.attr.settings != "":
mvn_flags += "-s %s " % ctx.attr.settings
if hasattr(ctx.attr, "settings") and ctx.attr.settings != None:
ctx.symlink(ctx.attr.settings, "settings.xml")
mvn_flags += "-s %s " % "settings.xml"

# dependency:get step. Downloads the artifact into the local repository.
mvn_get = MVN_PLUGIN + ":get"
Expand Down Expand Up @@ -262,7 +263,7 @@ _common_maven_rule_attrs = {
mandatory = True,
),
"sha1": attr.string(default = ""),
"settings": attr.string(default = "")
"settings": attr.label(default = None)
}

def _maven_jar_impl(ctx):
Expand Down

0 comments on commit a110ac4

Please sign in to comment.