Java Code Examples for org.apache.maven.model.building.ModelSource

The following examples show how to use org.apache.maven.model.building.ModelSource. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: migration-tooling   Source File: Resolver.java    License: Apache License 2.0 6 votes vote down vote up
/** Resolves an artifact as a root of a dependency graph. */
public void resolveArtifact(String artifactCoord) {
  Artifact artifact;
  ModelSource modelSource;
  try {
    artifact = ArtifactBuilder.fromCoords(artifactCoord);
    modelSource = modelResolver.resolveModel(artifact);
  } catch (UnresolvableModelException | InvalidArtifactCoordinateException e) {
    logger.warning(e.getMessage());
    return;
  }

  Rule rule = new Rule(artifact);
  rule.setRepository(modelSource.getLocation());
  rule.setSha1(downloadSha1(rule));
  deps.put(rule.name(), rule); // add the artifact rule to the workspace
  Model model = modelResolver.getEffectiveModel(modelSource);
  if (model != null) {
    traverseDeps(model, Sets.newHashSet(), Sets.newHashSet(), rule);
  }
}
 
Example 2
@Override
public ModelSource resolveModel(String groupId, String artifactId, String version)
        throws UnresolvableModelException {
    Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "", "pom", version);

    try {
        ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, null);
        pomArtifact = system.resolveArtifact(session, request).getArtifact();
    } catch (org.eclipse.aether.resolution.ArtifactResolutionException ex) {
        throw new UnresolvableModelException(ex.getMessage(), groupId, artifactId, version, ex);
    } 

    File pomFile = pomArtifact.getFile();

    return new FileModelSource(pomFile);
}
 
Example 3
Source Project: flatten-maven-plugin   Source File: FlattenModelResolver.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Resolves the POM for the specified parent.
 *
 * @param parent the parent coordinates to resolve, must not be {@code null}
 * @return The source of the requested POM, never {@code null}
 * @since Apache-Maven-3.2.2 (MNG-5639)
 */
public ModelSource resolveModel( Parent parent )
    throws UnresolvableModelException
{

    String groupId = parent.getGroupId();
    String artifactId = parent.getArtifactId();
    String version = parent.getVersion();

    // resolve version range (if present)
    if ( isRestrictedVersionRange( version, groupId, artifactId ) )
    {
        version = resolveParentVersionRange( groupId, artifactId, version );
    }

    return resolveModel( groupId, artifactId, version );
}
 
Example 4
Source Project: archiva   Source File: RepositoryModelResolver.java    License: Apache License 2.0 6 votes vote down vote up
public ModelSource resolveModel(Dependency dependency) throws UnresolvableModelException {
    try {
        Artifact artifact = new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(), "", "pom", dependency.getVersion());
        VersionRangeRequest versionRangeRequest = new VersionRangeRequest(artifact, null, null);
        VersionRangeResult versionRangeResult = this.versionRangeResolver.resolveVersionRange(this.session, versionRangeRequest);
        if (versionRangeResult.getHighestVersion() == null) {
            throw new UnresolvableModelException(String.format("No versions matched the requested dependency version range '%s'", dependency.getVersion()), dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
        } else if (versionRangeResult.getVersionConstraint() != null && versionRangeResult.getVersionConstraint().getRange() != null && versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null) {
            throw new UnresolvableModelException(String.format("The requested dependency version range '%s' does not specify an upper bound", dependency.getVersion()), dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
        } else {
            dependency.setVersion(versionRangeResult.getHighestVersion().toString());
            return this.resolveModel(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
        }
    } catch (VersionRangeResolutionException var5) {
        throw new UnresolvableModelException(var5.getMessage(), dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), var5);
    }
}
 
Example 5
Source Project: furnace   Source File: MavenModelResolver.java    License: Eclipse Public License 1.0 6 votes vote down vote up
@Override
public ModelSource resolveModel(String groupId, String artifactId, String version)
         throws UnresolvableModelException
{
   Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "", "pom", version);
   try
   {
      final ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, null);
      pomArtifact = system.resolveArtifact(session, request).getArtifact();

   }
   catch (ArtifactResolutionException e)
   {
      throw new UnresolvableModelException("Failed to resolve POM for " + groupId + ":" + artifactId + ":"
               + version + " due to " + e.getMessage(), groupId, artifactId, version, e);
   }

   final File pomFile = pomArtifact.getFile();

   return new FileModelSource(pomFile);

}
 
Example 6
Source Project: repairnator   Source File: RepositoryModelResolver.java    License: MIT License 5 votes vote down vote up
@Override
public ModelSource resolveModel(String groupId, String artifactId, String versionId)
        throws UnresolvableModelException {
    File pom = getLocalFile(groupId, artifactId, versionId);

    if (!pom.exists()) {
        try {
            download(pom);
        } catch (IOException e) {
            throw new UnresolvableModelException("Could not download POM", groupId, artifactId, versionId, e);
        }
    }

    return new FileModelSource(pom);
}
 
Example 7
Source Project: flatten-maven-plugin   Source File: FlattenModelResolver.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ModelSource resolveModel( String groupId, String artifactId, String version )
{
    File pomFile = reactorModelPool.find( groupId, artifactId, version );
    if ( pomFile == null )
    {
        Artifact pomArtifact = this.artifactFactory.createProjectArtifact( groupId, artifactId, version );
        pomArtifact = this.localRepository.find( pomArtifact );
        pomFile = pomArtifact.getFile();
    }

    return new FileModelSource( pomFile );
}
 
Example 8
Source Project: migration-tooling   Source File: Resolver.java    License: Apache License 2.0 4 votes vote down vote up
private void addDependency(
    Dependency dependency,
    Model model,
    Set<String> topLevelScopes,
    Set<String> exclusions,
    @Nullable Rule parent) {
  String scope = dependency.getScope();
  // DependencyManagement dependencies don't have scope.
  if (scope != null) {
    if (parent == null) {
      // Top-level scopes get pulled in based on the user-provided scopes.
      if (!topLevelScopes.contains(scope)) {
        return;
      }
    } else {
      // TODO (bazel-devel): Relabel the scope of transitive dependencies so that they match how
      // maven relabels them as described here:
      // https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
      if (!INHERITED_SCOPES.contains(scope)) {
        return;
      }
    }
  }
  if (dependency.isOptional()) {
    return;
  }
  if (exclusions.contains(unversionedCoordinate(dependency))) {
    return;
  }
  try {
    Rule artifactRule =
        new Rule(ArtifactBuilder.fromMavenDependency(dependency, versionResolver));
    HashSet<String> localDepExclusions = Sets.newHashSet(exclusions);
    dependency
        .getExclusions()
        .forEach(exclusion -> localDepExclusions.add(unversionedCoordinate(exclusion)));

    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());
        artifactRule.setSha1(downloadSha1(artifactRule));
        Model depModel = modelResolver.getEffectiveModel(depModelSource);
        if (depModel != null) {
          traverseDeps(depModel, topLevelScopes, localDepExclusions, artifactRule);
        }
      } else {
        logger.warning("Could not get a model for " + dependency);
      }
    }

    if (parent == null) {
      addArtifact(artifactRule, TOP_LEVEL_ARTIFACT);
    } else {
      parent.addDependency(artifactRule);
      parent.getDependencies().addAll(artifactRule.getDependencies());
    }
  } catch (UnresolvableModelException | InvalidArtifactCoordinateException e) {
    logger.warning(
        "Could not resolve dependency "
            + dependency.getGroupId()
            + ":"
            + dependency.getArtifactId()
            + ":"
            + dependency.getVersion()
            + ": "
            + e.getMessage());
  }
}
 
Example 9
Source Project: netbeans   Source File: NBRepositoryModelResolver.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ModelSource resolveModel(Parent parent) throws UnresolvableModelException {
    return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
}
 
Example 10
Source Project: netbeans   Source File: NBRepositoryModelResolver.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public ModelSource resolveModel(Dependency dpndnc) throws UnresolvableModelException {
    return resolveModel(dpndnc.getGroupId(), dpndnc.getArtifactId(), dpndnc.getVersion());
}
 
Example 11
@Override
public ModelSource resolveModel(Parent parent)
    throws UnresolvableModelException {
  return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
  
}
 
Example 12
public ModelSource resolveModel(Dependency dependency)
    throws UnresolvableModelException {
  return resolveModel(dependency.getGroupId(), dependency.getArtifactId(),
      dependency.getVersion());
}
 
Example 13
Source Project: repairnator   Source File: RepositoryModelResolver.java    License: MIT License 4 votes vote down vote up
@Override
public ModelSource resolveModel(Parent parent) throws UnresolvableModelException {
    return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
}
 
Example 14
Source Project: flatten-maven-plugin   Source File: FlattenModelResolver.java    License: Apache License 2.0 4 votes vote down vote up
public ModelSource resolveModel( Dependency dependency ) throws UnresolvableModelException
{
    return resolveModel( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion() );
}
 
Example 15
Source Project: furnace   Source File: MavenModelResolver.java    License: Eclipse Public License 1.0 4 votes vote down vote up
public ModelSource resolveModel(Parent parent)
         throws UnresolvableModelException
{
   // FIXME: Support version range. See DefaultModelResolver
   return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
}
 
Example 16
Source Project: furnace   Source File: MavenModelResolver.java    License: Eclipse Public License 1.0 4 votes vote down vote up
@Override public ModelSource resolveModel(Dependency dependency) throws UnresolvableModelException
{
   return resolveModel(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
}