Search in sources :

Example 1 with FileModelSource

use of org.apache.maven.model.building.FileModelSource in project bazel by bazelbuild.

the class Resolver method resolvePomDependencies.

/**
   * 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.
   */
public String resolvePomDependencies(String projectPath) {
    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.
    resolveEffectiveModel(pomSource, Sets.<String>newHashSet(), null);
    return pom.getAbsolutePath();
}
Also used : DefaultModelReader(org.apache.maven.model.io.DefaultModelReader) DefaultModelProcessor(org.apache.maven.model.building.DefaultModelProcessor) FileModelSource(org.apache.maven.model.building.FileModelSource) DefaultModelLocator(org.apache.maven.model.locator.DefaultModelLocator) File(java.io.File)

Example 2 with FileModelSource

use of org.apache.maven.model.building.FileModelSource in project intellij-community by JetBrains.

the class Maven3ServerEmbedderImpl method readModel.

@Override
@Nullable
public MavenModel readModel(File file) throws RemoteException {
    Map<String, Object> inputOptions = new HashMap<String, Object>();
    inputOptions.put(ModelProcessor.SOURCE, new FileModelSource(file));
    ModelReader reader = null;
    if (!StringUtil.endsWithIgnoreCase(file.getName(), "xml")) {
        try {
            Object polyglotManager = myContainer.lookup("org.sonatype.maven.polyglot.PolyglotModelManager");
            if (polyglotManager != null) {
                Method getReaderFor = ReflectionUtil.getMethod(polyglotManager.getClass(), "getReaderFor", Map.class);
                if (getReaderFor != null) {
                    reader = (ModelReader) getReaderFor.invoke(polyglotManager, inputOptions);
                }
            }
        } catch (ComponentLookupException ignore) {
        } catch (Throwable e) {
            Maven3ServerGlobals.getLogger().warn(e);
        }
    }
    if (reader == null) {
        try {
            reader = myContainer.lookup(ModelReader.class);
        } catch (ComponentLookupException ignore) {
        }
    }
    if (reader != null) {
        try {
            Model model = reader.read(file, inputOptions);
            return MavenModelConverter.convertModel(model, null);
        } catch (Exception e) {
            Maven3ServerGlobals.getLogger().warn(e);
        }
    }
    return null;
}
Also used : ModelReader(org.apache.maven.model.io.ModelReader) THashMap(gnu.trove.THashMap) FileModelSource(org.apache.maven.model.building.FileModelSource) Model(org.apache.maven.model.Model) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) Method(java.lang.reflect.Method) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) InitializationException(org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException) ModelInterpolationException(org.apache.maven.project.interpolation.ModelInterpolationException) RemoteException(java.rmi.RemoteException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ContextException(org.codehaus.plexus.context.ContextException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) InvalidRepositoryException(org.apache.maven.artifact.InvalidRepositoryException) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with FileModelSource

use of org.apache.maven.model.building.FileModelSource in project bazel by bazelbuild.

the class Resolver method resolveSourceLocations.

/**
   * Find the POM files for a given pom's parent(s) and submodules.
   */
private void resolveSourceLocations(FileModelSource fileModelSource) {
    Model model = modelResolver.getRawModel(fileModelSource, handler);
    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) {
            handler.handle(Event.error("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")));
    }
}
Also used : Parent(org.apache.maven.model.Parent) FileModelSource(org.apache.maven.model.building.FileModelSource) Model(org.apache.maven.model.Model) IOException(java.io.IOException) File(java.io.File)

Aggregations

FileModelSource (org.apache.maven.model.building.FileModelSource)3 File (java.io.File)2 Model (org.apache.maven.model.Model)2 THashMap (gnu.trove.THashMap)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 RemoteException (java.rmi.RemoteException)1 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)1 InvalidRepositoryException (org.apache.maven.artifact.InvalidRepositoryException)1 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)1 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)1 Parent (org.apache.maven.model.Parent)1 DefaultModelProcessor (org.apache.maven.model.building.DefaultModelProcessor)1 DefaultModelReader (org.apache.maven.model.io.DefaultModelReader)1 ModelReader (org.apache.maven.model.io.ModelReader)1 DefaultModelLocator (org.apache.maven.model.locator.DefaultModelLocator)1 ModelInterpolationException (org.apache.maven.project.interpolation.ModelInterpolationException)1 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)1 ContextException (org.codehaus.plexus.context.ContextException)1 InitializationException (org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException)1