use of org.apache.maven.model.building.ModelSource in project bazel by bazelbuild.
the class Resolver method resolveEffectiveModel.
/**
* Resolves all dependencies from a given "model source," which could be either a URL or a local
* file.
* @return the model.
*/
@Nullable
public Model resolveEffectiveModel(ModelSource modelSource, Set<String> exclusions, Rule parent) {
Model model = modelResolver.getEffectiveModel(modelSource, handler);
if (model == null) {
return null;
}
for (Repository repo : model.getRepositories()) {
modelResolver.addRepository(repo);
}
for (Dependency dependency : model.getDependencies()) {
if (!dependency.getScope().equals(COMPILE_SCOPE)) {
continue;
}
if (dependency.isOptional()) {
continue;
}
if (exclusions.contains(dependency.getGroupId() + ":" + dependency.getArtifactId())) {
continue;
}
try {
Rule artifactRule = new Rule(getArtifact(dependency), dependency.getExclusions());
HashSet<String> localDepExclusions = new HashSet<>(exclusions);
localDepExclusions.addAll(artifactRule.getExclusions());
boolean isNewDependency = addArtifact(artifactRule, model.toString());
if (isNewDependency) {
ModelSource depModelSource = modelResolver.resolveModel(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
if (depModelSource != null) {
artifactRule.setRepository(depModelSource.getLocation(), handler);
artifactRule.setSha1(downloadSha1(artifactRule));
resolveEffectiveModel(depModelSource, localDepExclusions, artifactRule);
} else {
handler.handle(Event.error("Could not get a model for " + dependency));
}
}
if (parent != null) {
parent.addDependency(artifactRule);
parent.getDependencies().addAll(artifactRule.getDependencies());
} else {
addArtifact(artifactRule, modelSource.getLocation());
}
} catch (UnresolvableModelException | InvalidArtifactCoordinateException e) {
handler.handle(Event.error("Could not resolve dependency " + dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getVersion() + ": " + e.getMessage()));
}
}
return model;
}
use of org.apache.maven.model.building.ModelSource in project maven-plugins by apache.
the class InstallFileMojo method createMavenProject.
/**
* Creates a Maven project in-memory from the user-supplied groupId, artifactId and version. When a classifier is
* supplied, the packaging must be POM because the project with only have attachments. This project serves as basis
* to attach the artifacts to install to.
*
* @return The created Maven project, never <code>null</code>.
* @throws MojoExecutionException When the model of the project could not be built.
* @throws MojoFailureException When building the project failed.
*/
private MavenProject createMavenProject() throws MojoExecutionException, MojoFailureException {
if (groupId == null || artifactId == null || version == null || packaging == null) {
throw new MojoExecutionException("The artifact information is incomplete: 'groupId', 'artifactId', " + "'version' and 'packaging' are required.");
}
ModelSource modelSource = new StringModelSource("<project><modelVersion>4.0.0</modelVersion><groupId>" + groupId + "</groupId><artifactId>" + artifactId + "</artifactId><version>" + version + "</version><packaging>" + (classifier == null ? packaging : "pom") + "</packaging></project>");
ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
pbr.setProcessPlugins(false);
try {
return projectBuilder.build(modelSource, pbr).getProject();
} catch (ProjectBuildingException e) {
if (e.getCause() instanceof ModelBuildingException) {
throw new MojoExecutionException("The artifact information is not valid:" + Os.LINE_SEP + e.getCause().getMessage());
}
throw new MojoFailureException("Unable to create the project.", e);
}
}
use of org.apache.maven.model.building.ModelSource in project drools by kiegroup.
the class MavenEmbedder method readProject.
// ----------------------------------------------------------------------
// Project
// ----------------------------------------------------------------------
public MavenProject readProject(final InputStream mavenProjectStream) throws ProjectBuildingException, MavenEmbedderException {
ModelSource modelSource = new ModelSource() {
@Override
public InputStream getInputStream() {
return mavenProjectStream;
}
@Override
public String getLocation() {
return "";
}
};
ClassLoader originalCl = Thread.currentThread().getContextClassLoader();
try {
org.eclipse.aether.artifact.Artifact lastArtifact = null;
do {
Thread.currentThread().setContextClassLoader(componentProvider.getSystemClassLoader());
ProjectBuilder projectBuilder = componentProvider.lookup(ProjectBuilder.class);
// BZ-1007894: Check if added dependencies are resolvable.
ProjectBuildingResult result = projectBuilder.build(modelSource, getProjectBuildingRequest());
if (result != null && result.getDependencyResolutionResult() != null && !result.getDependencyResolutionResult().getCollectionErrors().isEmpty()) {
// A dependency resolution error has been produced. It can contains some error. Throw the first one to the client, so the user will fix every one sequentially.
final Exception depedencyResolutionException = result.getDependencyResolutionResult().getCollectionErrors().get(0);
if (depedencyResolutionException instanceof ArtifactDescriptorException) {
final org.eclipse.aether.artifact.Artifact artifact = ((ArtifactDescriptorException) depedencyResolutionException).getResult().getArtifact();
if (!artifact.equals(lastArtifact)) {
tryRemoveLocalArtifact(artifact);
lastArtifact = artifact;
continue;
}
}
if (depedencyResolutionException != null) {
throw new MavenEmbedderException(depedencyResolutionException.getMessage(), depedencyResolutionException);
}
}
return (result == null || result.getProject() == null) ? null : result.getProject();
} while (true);
} catch (ComponentLookupException e) {
throw new MavenEmbedderException(e.getMessage(), e);
} finally {
Thread.currentThread().setContextClassLoader(originalCl);
try {
mavenProjectStream.close();
} catch (IOException e) {
}
}
}
use of org.apache.maven.model.building.ModelSource 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"));
}
}
}
use of org.apache.maven.model.building.ModelSource in project bazel by bazelbuild.
the class Resolver method resolveArtifact.
/**
* Resolves an artifact as a root of a dependency graph.
*/
public void resolveArtifact(String artifactCoord) {
Artifact artifact;
ModelSource modelSource;
try {
artifact = getArtifact(artifactCoord);
modelSource = modelResolver.resolveModel(artifact);
} catch (UnresolvableModelException | InvalidArtifactCoordinateException e) {
handler.handle(Event.error(e.getMessage()));
return;
}
Rule rule = new Rule(artifact);
// add the artifact rule to the workspace
deps.put(rule.name(), rule);
resolveEffectiveModel(modelSource, Sets.<String>newHashSet(), rule);
}
Aggregations