aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/MavenServerRule.java
blob: 2252400fd3083f17cf4e69018f84da23668d73e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.bazel.rules.workspace;

import static com.google.devtools.build.lib.packages.Attribute.attr;

import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder;
import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType;
import com.google.devtools.build.lib.syntax.Type;

/**
 * Rule definition for the maven_jar rule.
 */
public class MavenServerRule implements RuleDefinition {

  public static final String NAME = "maven_server";

  @Override
  public RuleClass build(Builder builder, RuleDefinitionEnvironment environment) {
    return builder
        /* <!-- #BLAZE_RULE(maven_server).ATTRIBUTE(url) -->
        A URL for accessing the server.
        ${SYNOPSIS}

        <p>For example, Maven Central (which is the default and does not need to be defined) would
        be specified as <code>url = "http://central.maven.org/maven2/"</code>.</p>
        <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
        .add(attr("url", Type.STRING))
        /* <!-- #BLAZE_RULE(maven_server).ATTRIBUTE(settings_file) -->
        A path to a settings.xml file.  Used for testing. If unspecified, this defaults to using
        <code>$M2_HOME/conf/settings.xml</code> for the global settings and
        <code>$HOME/.m2/settings.xml</code> for the user settings.
        ${SYNOPSIS}
        <!-- #END_BLAZE_RULE.ATTRIBUTE --> */
        .add(attr("settings_file", Type.STRING))
        .setWorkspaceOnly()
        .build();
  }

  @Override
  public Metadata getMetadata() {
    return RuleDefinition.Metadata.builder()
        .name(MavenServerRule.NAME)
        .type(RuleClassType.WORKSPACE)
        .ancestors(WorkspaceBaseRule.class)
        .factoryClass(WorkspaceConfiguredTargetFactory.class)
        .build();
  }
}
/*<!-- #BLAZE_RULE (NAME = maven_server, TYPE = OTHER, FAMILY = Workspace)[GENERIC_RULE] -->

${ATTRIBUTE_SIGNATURE}

<p>How to access a Maven repository.</p>

${ATTRIBUTE_DEFINITION}

<p>This is a combination of a &lt;repository&gt; definition from a pom.xml file and a
&lt;server&lt; definition from a settings.xml file.</p>

<h4>Using <code>maven_server</code></h4>

<p><code>maven_jar</code> rules can specify the name of a <code>maven_server</code> in their
<code>server</code> field. For example, suppose we have the following WORKSPACE file:</p>

<pre>
maven_jar(
    name = "junit",
    artifact = "junit:junit-dep:4.10",
    server = "my-server",
)

maven_server(
    name = "my-server",
    url = "http://intranet.mycorp.net",
)
</pre>

This specifies that junit should be downloaded from http://intranet.mycorp.net using the
authentication information found in ~/.m2/settings.xml (specifically, the settings
for the server with the id <code>my-server</code>).

<h4>Specifying a default server</h4>

<p>If you create a <code>maven_server</code> with the <code>name</code> "default" it will be
used for any <code>maven_jar</code> that does not specify a <code>server</code> nor
<code>repository</code>. If there is no <code>maven_server</code> named default, the
default will be fetching from Maven Central with no authentication enabled.</p>

<!-- #END_BLAZE_RULE -->*/