org.apache.maven.model.building.FileModelSource Java Examples

The following examples show how to use org.apache.maven.model.building.FileModelSource. 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 File: Resolver.java    From migration-tooling with Apache License 2.0 6 votes vote down vote up
/**
 * Given a local path to a Maven project, this attempts to find the transitive dependencies of the
 * project.
 *
 * @param projectPath The path to search for Maven projects.
 * @param scopes The scopes to look up dependencies in.
 */
public String resolvePomDependencies(String projectPath, Set<String> scopes) {
  DefaultModelProcessor processor = new DefaultModelProcessor();
  processor.setModelLocator(new DefaultModelLocator());
  processor.setModelReader(new DefaultModelReader());
  File pom = processor.locatePom(new File(projectPath));
  FileModelSource pomSource = new FileModelSource(pom);
  // First resolve the model source locations.
  resolveSourceLocations(pomSource);
  // Next, fully resolve the models.
  Model model = modelResolver.getEffectiveModel(pomSource);
  if (model != null) {
    traverseDeps(model, scopes, Sets.newHashSet(), null);
  }
  return pom.getAbsolutePath();
}
 
Example #2
Source File: SimpleModelResolver.java    From gate-core with GNU Lesser General Public License v3.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 {
        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 File: POMParser.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
@Override
public ModelSource2 resolveModel(
    final String groupId, final String artifactId, final String version)
    throws UnresolvableModelException {
  final Config config = Config.load();
  final String localRepository = config.getMavenLocalRepository();
  final String parent = StringUtils.replace(groupId, ".", File.separator);
  final String path =
      Joiner.on(File.separator).join(localRepository, parent, artifactId, version);
  final String file = artifactId + '-' + version + ".pom";
  final File pom = new File(path, file);
  final boolean exists = pom.exists();

  loaded.add(pom);
  if (exists) {
    return new FileModelSource(pom);
  }
  return null;
}
 
Example #4
Source File: MavenModelResolver.java    From furnace with 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 #5
Source File: Resolver.java    From migration-tooling with Apache License 2.0 5 votes vote down vote up
/** Find the POM files for a given pom's parent(s) and submodules. */
private void resolveSourceLocations(FileModelSource fileModelSource) {
  Model model = modelResolver.getRawModel(fileModelSource);
  if (model == null) {
    return;
  }

  // Self.
  Parent parent = model.getParent();
  if (model.getGroupId() == null) {
    model.setGroupId(parent.getGroupId());
  }
  if (!modelResolver.putModelSource(model.getGroupId(), model.getArtifactId(), fileModelSource)) {
    return;
  }

  // Parent.
  File pomDirectory = new File(fileModelSource.getLocation()).getParentFile();
  if (parent != null && !parent.getArtifactId().equals(model.getArtifactId())) {
    File parentPom;
    try {
      parentPom = new File(pomDirectory, parent.getRelativePath()).getCanonicalFile();
    } catch (IOException e) {
      logger.warning(
          "Unable to get canonical path of " + pomDirectory + " and " + parent.getRelativePath());
      return;
    }
    if (parentPom.exists()) {
      resolveSourceLocations(new FileModelSource(parentPom));
    }
  }

  // Submodules.
  for (String module : model.getModules()) {
    resolveSourceLocations(new FileModelSource(new File(pomDirectory, module + "/pom.xml")));
  }
}
 
Example #6
Source File: RepositoryModelResolver.java    From repairnator with 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 File: FlattenModelResolver.java    From flatten-maven-plugin with 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 File: CustomModelProcessor.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public Model read(Reader reader, Map<String, ?> options) throws IOException, ModelParseException {
    FileModelSource source = (options != null) ? (FileModelSource) options.get(SOURCE) : null;
    if (source != null && source.getLocation().endsWith(JSON_EXT)) {
        Model model = objectMapper.readValue(reader, Model.class);
        return model;
    }
    //It's a normal maven project with a pom.xml file
    return modelReader.read(reader, options);
}