Search in sources :

Example 1 with Rule

use of com.google.devtools.build.workspace.maven.Rule in project bazel by bazelbuild.

the class ResultWriter method writeBuild.

/**
   * Write library rules to depend on the transitive closure of all of these rules.
   */
public void writeBuild(PrintStream outputStream) {
    writeHeader(outputStream);
    for (Rule rule : rules) {
        outputStream.println("java_library(");
        outputStream.println("    name = \"" + rule.name() + "\",");
        outputStream.println("    visibility = [\"//visibility:public\"],");
        outputStream.println("    exports = [");
        outputStream.println("        \"@" + rule.name() + "//jar\",");
        for (Rule r : rule.getDependencies()) {
            outputStream.println("        \"@" + r.name() + "//jar\",");
        }
        outputStream.println("    ],");
        outputStream.println(")");
        outputStream.println();
    }
}
Also used : Rule(com.google.devtools.build.workspace.maven.Rule)

Example 2 with Rule

use of com.google.devtools.build.workspace.maven.Rule in project bazel by bazelbuild.

the class ResultWriterTest method testBuildFile.

@Test
public void testBuildFile() throws Exception {
    Rule rule = new Rule(new DefaultArtifact("x:y:1.2.3"));
    Rule dep1 = new Rule(new DefaultArtifact("dep:dep1:4.5.6"));
    rule.addDependency(dep1);
    Rule dep2 = new Rule(new DefaultArtifact("dep:dep2:7.8.9"));
    rule.addDependency(dep2);
    Set<Rule> rules = ImmutableSet.of(rule, dep1, dep2);
    String content = getBuildFileContent(ImmutableList.<String>of(), rules);
    assertThat(content).contains("java_library(\n" + "    name = \"x_y\",\n" + "    visibility = [\"//visibility:public\"],\n" + "    exports = [\n" + "        \"@x_y//jar\",\n" + "        \"@dep_dep1//jar\",\n" + "        \"@dep_dep2//jar\",\n" + "    ],\n" + ")");
}
Also used : Rule(com.google.devtools.build.workspace.maven.Rule) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Example 3 with Rule

use of com.google.devtools.build.workspace.maven.Rule in project bazel by bazelbuild.

the class ResultWriterTest method testParents.

@Test
public void testParents() throws Exception {
    Rule rule = new Rule(new DefaultArtifact("x:y:1.2.3"));
    rule.addParent("some parent");
    Set<Rule> rules = ImmutableSet.of(rule);
    String content = getWorkspaceFileContent(ImmutableList.<String>of(), rules);
    assertThat(content).contains("# some parent\n" + "maven_jar(\n" + "    name = \"x_y\",\n" + "    artifact = \"x:y:1.2.3\",\n" + ")");
}
Also used : Rule(com.google.devtools.build.workspace.maven.Rule) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Example 4 with Rule

use of com.google.devtools.build.workspace.maven.Rule in project bazel by bazelbuild.

the class WorkspaceResolver method resolveTransitiveDependencies.

/**
   * Calculates transitive dependencies of the given //external package.
   */
public void resolveTransitiveDependencies(Package externalPackage) {
    Location location = Location.fromFile(externalPackage.getFilename());
    for (Target target : externalPackage.getTargets()) {
        // Targets are //external:foo.
        if (target.getTargetKind().startsWith("maven_jar ")) {
            RepositoryName repositoryName;
            try {
                repositoryName = RepositoryName.create("@" + target.getName());
            } catch (LabelSyntaxException e) {
                handler.handle(Event.error(location, "Invalid repository name for " + target + ": " + e.getMessage()));
                return;
            }
            com.google.devtools.build.lib.packages.Rule workspaceRule = externalPackage.getRule(repositoryName.strippedName());
            DefaultModelResolver modelResolver = resolver.getModelResolver();
            AttributeMap attributeMap = AggregatingAttributeMapper.of(workspaceRule);
            Rule rule;
            try {
                rule = new Rule(Resolver.getArtifact(attributeMap.get("artifact", Type.STRING)));
            } catch (InvalidArtifactCoordinateException e) {
                handler.handle(Event.error(location, "Couldn't get attribute: " + e.getMessage()));
                return;
            }
            if (attributeMap.isAttributeValueExplicitlySpecified("repository")) {
                modelResolver.addUserRepository(attributeMap.get("repository", Type.STRING));
                rule.setRepository(attributeMap.get("repository", Type.STRING), handler);
            }
            if (attributeMap.isAttributeValueExplicitlySpecified("sha1")) {
                rule.setSha1(attributeMap.get("sha1", Type.STRING));
            } else {
                rule.setSha1(resolver.downloadSha1(rule));
            }
            ModelSource modelSource;
            try {
                modelSource = modelResolver.resolveModel(rule.groupId(), rule.artifactId(), rule.version());
            } catch (UnresolvableModelException e) {
                handler.handle(Event.error("Could not resolve model for " + target + ": " + e.getMessage()));
                continue;
            }
            resolver.addArtifact(rule, modelSource.getLocation());
            resolver.resolveEffectiveModel(modelSource, Sets.<String>newHashSet(), rule);
        } else if (!target.getTargetKind().startsWith("bind") && !target.getTargetKind().startsWith("source ")) {
            handler.handle(Event.warn(location, "Cannot fetch transitive dependencies for " + target + " yet, skipping"));
        }
    }
}
Also used : LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) DefaultModelResolver(com.google.devtools.build.workspace.maven.DefaultModelResolver) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) ModelSource(org.apache.maven.model.building.ModelSource) InvalidArtifactCoordinateException(com.google.devtools.build.workspace.maven.Resolver.InvalidArtifactCoordinateException) Target(com.google.devtools.build.lib.packages.Target) AttributeMap(com.google.devtools.build.lib.packages.AttributeMap) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) Rule(com.google.devtools.build.workspace.maven.Rule) Location(com.google.devtools.build.lib.events.Location)

Example 5 with Rule

use of com.google.devtools.build.workspace.maven.Rule in project bazel by bazelbuild.

the class ResultWriterTest method testArtifacts.

@Test
public void testArtifacts() throws Exception {
    Set<Rule> rules = ImmutableSet.of(new Rule(new DefaultArtifact("x:y:1.2.3")));
    String content = getWorkspaceFileContent(ImmutableList.<String>of(), rules);
    assertThat(content).contains("maven_jar(\n" + "    name = \"x_y\",\n" + "    artifact = \"x:y:1.2.3\",\n" + ")");
}
Also used : Rule(com.google.devtools.build.workspace.maven.Rule) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Test(org.junit.Test)

Aggregations

Rule (com.google.devtools.build.workspace.maven.Rule)5 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)3 Test (org.junit.Test)3 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)1 Location (com.google.devtools.build.lib.events.Location)1 AttributeMap (com.google.devtools.build.lib.packages.AttributeMap)1 Target (com.google.devtools.build.lib.packages.Target)1 DefaultModelResolver (com.google.devtools.build.workspace.maven.DefaultModelResolver)1 InvalidArtifactCoordinateException (com.google.devtools.build.workspace.maven.Resolver.InvalidArtifactCoordinateException)1 ModelSource (org.apache.maven.model.building.ModelSource)1 UnresolvableModelException (org.apache.maven.model.resolution.UnresolvableModelException)1