Skip to content

Commit

Permalink
Enable custom repositories. (bazelbuild#71)
Browse files Browse the repository at this point in the history
* Add sha1 and repo to root dependency

* Enable custom repositories in transitive_maven_jar

* Update generate_worspace_deploy.jar for transitive_maven_jar func
  • Loading branch information
mweiden authored and petroseskinder committed Nov 12, 2017
1 parent 0f25a7e commit a5e6874
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ java_library(
"//third_party:aether",
"//third_party:com_google_guava_guava",
"//third_party/jcommander",
"//third_party:maven_model",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@
import java.util.Set;
import java.util.logging.Logger;

/**
* Generates a WORKSPACE file for Bazel from other types of dependency trackers.
*/
/** Generates a WORKSPACE file for Bazel from other types of dependency trackers. */
public class GenerateWorkspace {

private final static Logger logger = Logger.getLogger(
MethodHandles.lookup().lookupClass().getName());
private static final Logger logger =
Logger.getLogger(MethodHandles.lookup().lookupClass().getName());

private final Resolver resolver;
private final List<String> inputs;
Expand All @@ -58,8 +56,13 @@ public static void main(String[] args) throws InterruptedException {
}

try {
GenerateWorkspace workspaceFileGenerator = new GenerateWorkspace(
args, options.outputDir, options.directToWorkspace, options.aliases);
GenerateWorkspace workspaceFileGenerator =
new GenerateWorkspace(
args,
options.outputDir,
options.directToWorkspace,
options.repositories,
options.aliases);
workspaceFileGenerator.generateFromPom(options.mavenProjects, options.scopes);
workspaceFileGenerator.generateFromArtifacts(options.artifacts);
workspaceFileGenerator.writeResults();
Expand All @@ -69,13 +72,20 @@ public static void main(String[] args) throws InterruptedException {
}
}

private GenerateWorkspace(String[] args, String outputDirStr, boolean directToWorkspace, List<Rule> aliases)
private GenerateWorkspace(
String[] args,
String outputDirStr,
boolean directToWorkspace,
List<String> repositories,
List<Rule> aliases)
throws IOException {
this.resolver = new Resolver(new DefaultModelResolver(), aliases);
DefaultModelResolver defaultModelResolver = new DefaultModelResolver(repositories);
this.resolver = new Resolver(defaultModelResolver, aliases);
this.inputs = Lists.newArrayList();
this.resultWriter = directToWorkspace
? new WorkspaceWriter(args, outputDirStr)
: new BzlWriter(args, outputDirStr);
this.resultWriter =
directToWorkspace
? new WorkspaceWriter(args, outputDirStr)
: new BzlWriter(args, outputDirStr);
}

private void generateFromPom(List<String> projects, Set<String> scopes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,92 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
import com.beust.jcommander.converters.CommaParameterSplitter;
import com.beust.jcommander.converters.IParameterSplitter;
import com.google.common.collect.Sets;
import com.google.devtools.build.workspace.maven.ArtifactBuilder;
import com.google.devtools.build.workspace.maven.ArtifactBuilder.InvalidArtifactCoordinateException;
import com.google.devtools.build.workspace.maven.Rule;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.util.artifact.JavaScopes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.util.artifact.JavaScopes;

/**
* Command-line options for generate_workspace tool.
*/
/** Command-line options for generate_workspace tool. */
@Parameters(separators = "=")
public class GenerateWorkspaceOptions {

private static final String MIN_VERSION = "0";

@Parameter(
names = "--help",
description = "This message.",
help = true
)
@Parameter(names = "--help", description = "This message.", help = true)
private boolean help;

@Parameter(
names = {"--maven_project", "-m"},
description = "Directory containing a Maven project."
names = {"--maven_project", "-m"},
description = "Directory containing a Maven project."
)
public List<String> mavenProjects = new ArrayList<>();

@Parameter(
names = {"--artifact", "-a"},
splitter = NoSplitter.class,
description = "Maven artifact coordinates (e.g. groupId:artifactId:version)."
names = {"--repositories", "-r"},
splitter = CommaParameterSplitter.class,
description = "List of remote maven repositories to query for artifacts."
)
public List<String> repositories = new ArrayList<>();

@Parameter(
names = {"--artifact", "-a"},
splitter = NoSplitter.class,
description = "Maven artifact coordinates (e.g. groupId:artifactId:version)."
)
public List<String> artifacts = new ArrayList<>();

@Parameter(
names = {"--output_dir", "-o"},
description = "Output directory to write the output file(s). If unspecified, uses the"
+ " current directory."
names = {"--output_dir", "-o"},
description =
"Output directory to write the output file(s). If unspecified, uses the"
+ " current directory."
)
public String outputDir = "";

@Parameter(
names = {"--direct-to-ws"},
description = "Write generated rules to files named WORKSPACE and BUILD, respectively."
+ " If not specified, generate_workspace will write generated rules to a .bzl"
+ " file that can be loaded from hand-written WORKSPACE and BUILD files."
names = {"--direct-to-ws"},
description =
"Write generated rules to files named WORKSPACE and BUILD, respectively."
+ " If not specified, generate_workspace will write generated rules to a .bzl"
+ " file that can be loaded from hand-written WORKSPACE and BUILD files."
)
public boolean directToWorkspace = false;

@Parameter(
names = {"--scope"},
description = "Maven scope(s) in to look up dependencies in. May be repeated to list scopes."
names = {"--scope"},
description = "Maven scope(s) in to look up dependencies in. May be repeated to list scopes."
)
public Set<String> scopes =
Sets.newHashSet(JavaScopes.COMPILE, JavaScopes.PROVIDED, JavaScopes.TEST, JavaScopes.RUNTIME);

@Parameter(
names = {"--declare"},
description = "Reuse a repository that is already defined elsewhere. For example, "
+ "--declare=org.mockito:mockito-all=mockito will use @mockito instead of "
+ "@org_mockito_mockito_all for references to Mockito. You can also leave off the "
+ "\"=mockito\" to use the default generated name (--declare=org.mockito:mockito-all "
+ "will use @org_mockito_mockito_all for references). If this option is specified, it "
+ "is assumed that you already have the repository defined somewhere else, so a "
+ "maven_jar is not created for it.",
listConverter = AliasSplitter.class
names = {"--declare"},
description =
"Reuse a repository that is already defined elsewhere. For example, "
+ "--declare=org.mockito:mockito-all=mockito will use @mockito instead of "
+ "@org_mockito_mockito_all for references to Mockito. You can also leave off the "
+ "\"=mockito\" to use the default generated name (--declare=org.mockito:mockito-all "
+ "will use @org_mockito_mockito_all for references). If this option is specified, it "
+ "is assumed that you already have the repository defined somewhere else, so a "
+ "maven_jar is not created for it.",
listConverter = AliasSplitter.class
)
public List<Rule> aliases = new ArrayList<>();

/**
* Jcommander defaults to splitting each parameter by comma. For example,
* --a=group:artifact:[x1,x2] is parsed as two items 'group:artifact:[x1' and 'x2]',
* instead of the intended 'group:artifact:[x1,x2]'
* --a=group:artifact:[x1,x2] is parsed as two items 'group:artifact:[x1' and 'x2]', instead of
* the intended 'group:artifact:[x1,x2]'
*
* For more information: http://jcommander.org/#_splitting
* <p>For more information: http://jcommander.org/#_splitting
*/
private static class NoSplitter implements IParameterSplitter {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ private Aether(RepositorySystem repositorySystem,
this.remoteRepositories = remoteRepositories;
}

public void addRemoteRepository(RemoteRepository repo) {
this.remoteRepositories.add(repo);
}

public List<RemoteRepository> getRemoteRepositories() {
return this.remoteRepositories;
}

/** Given an artifacts requests a version range for it. */
List<String> requestVersionRange(Artifact artifact) throws VersionRangeResolutionException {
VersionRangeRequest rangeRequest = new VersionRangeRequest(artifact, remoteRepositories, null);
Expand Down
Loading

0 comments on commit a5e6874

Please sign in to comment.