Search in sources :

Example 31 with ArtifactResolutionException

use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project maven-archetype by apache.

the class ArchetypeTest method getContextClassloader.

// Gets the classloader for this artifact's file.
private ClassLoader getContextClassloader(Artifact archetypeArtifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories) throws Exception {
    ArtifactResolver artifactResolver = (ArtifactResolver) lookup(ArtifactResolver.class.getName());
    try {
        artifactResolver.resolve(archetypeArtifact, remoteRepositories, localRepository);
    } catch (ArtifactResolutionException e) {
        throw new ArchetypeDescriptorException("Error attempting to download archetype: " + e.getMessage(), e);
    } catch (ArtifactNotFoundException e) {
        throw new ArchetypeNotFoundException("OldArchetype does not exist: " + e.getMessage(), e);
    }
    URLClassLoader archetypeJarLoader;
    try {
        URL[] urls = new URL[1];
        urls[0] = archetypeArtifact.getFile().toURI().toURL();
        archetypeJarLoader = new URLClassLoader(urls);
    } catch (IOException e) {
        throw new ArchetypeDescriptorException("Error reading the " + OldArchetype.ARCHETYPE_DESCRIPTOR + " descriptor.", e);
    }
    return archetypeJarLoader;
}
Also used : ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) URLClassLoader(java.net.URLClassLoader) IOException(java.io.IOException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) URL(java.net.URL)

Example 32 with ArtifactResolutionException

use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project wso2-synapse by wso2.

the class AbstractXarMojo method getSynapseRuntimeArtifacts.

/**
 * Get the set of artifacts that are provided by Synapse at runtime.
 *
 * @return
 * @throws MojoExecutionException
 */
private Set<Artifact> getSynapseRuntimeArtifacts() throws MojoExecutionException {
    Log log = getLog();
    log.debug("Looking for synapse-core artifact in XAR project dependencies ...");
    Artifact synapseCore = null;
    for (Iterator<?> it = project.getDependencyArtifacts().iterator(); it.hasNext(); ) {
        Artifact artifact = (Artifact) it.next();
        if (artifact.getGroupId().equals("org.apache.synapse") && artifact.getArtifactId().equals("synapse-core")) {
            synapseCore = artifact;
            break;
        }
    }
    if (synapseCore == null) {
        throw new MojoExecutionException("Could not locate dependency on synapse-core");
    }
    log.debug("Loading project data for " + synapseCore + " ...");
    MavenProject synapseCoreProject;
    try {
        synapseCoreProject = projectBuilder.buildFromRepository(synapseCore, remoteArtifactRepositories, localRepository);
    } catch (ProjectBuildingException e) {
        throw new MojoExecutionException("Unable to retrieve project information for " + synapseCore, e);
    }
    Set<Artifact> synapseRuntimeDeps;
    try {
        synapseRuntimeDeps = synapseCoreProject.createArtifacts(artifactFactory, Artifact.SCOPE_RUNTIME, new TypeArtifactFilter("jar"));
    } catch (InvalidDependencyVersionException e) {
        throw new MojoExecutionException("Unable to get project dependencies for " + synapseCore, e);
    }
    log.debug("Direct runtime dependencies for " + synapseCore + " :");
    logArtifacts(synapseRuntimeDeps);
    log.debug("Resolving transitive dependencies for " + synapseCore + " ...");
    try {
        synapseRuntimeDeps = artifactCollector.collect(synapseRuntimeDeps, synapseCoreProject.getArtifact(), synapseCoreProject.getManagedVersionMap(), localRepository, remoteArtifactRepositories, artifactMetadataSource, null, Collections.singletonList(new DebugResolutionListener(logger))).getArtifacts();
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to resolve transitive dependencies for " + synapseCore);
    }
    log.debug("All runtime dependencies for " + synapseCore + " :");
    logArtifacts(synapseRuntimeDeps);
    return synapseRuntimeDeps;
}
Also used : TypeArtifactFilter(org.apache.maven.artifact.resolver.filter.TypeArtifactFilter) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) DebugResolutionListener(org.apache.maven.artifact.resolver.DebugResolutionListener) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenProject(org.apache.maven.project.MavenProject) Log(org.apache.maven.plugin.logging.Log) InvalidDependencyVersionException(org.apache.maven.project.artifact.InvalidDependencyVersionException) Artifact(org.apache.maven.artifact.Artifact)

Example 33 with ArtifactResolutionException

use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project karaf by apache.

the class GenerateDescriptorMojo method writeFeatures.

/*
     * Write all project dependencies as feature
     */
private void writeFeatures(PrintStream out) throws ArtifactResolutionException, ArtifactNotFoundException, IOException, JAXBException, SAXException, ParserConfigurationException, XMLStreamException, MojoExecutionException {
    getLog().info("Generating feature descriptor file " + outputFile.getAbsolutePath());
    // read in an existing feature.xml
    ObjectFactory objectFactory = new ObjectFactory();
    Features features;
    if (inputFile.exists()) {
        filter(inputFile, filteredInputFile);
        features = readFeaturesFile(filteredInputFile);
    } else {
        features = objectFactory.createFeaturesRoot();
    }
    if (features.getName() == null) {
        features.setName(project.getArtifactId());
    }
    Feature feature = null;
    for (Feature test : features.getFeature()) {
        if (test.getName().equals(primaryFeatureName)) {
            feature = test;
        }
    }
    if (feature == null) {
        feature = objectFactory.createFeature();
        feature.setName(primaryFeatureName);
    }
    if (!feature.hasVersion()) {
        feature.setVersion(project.getArtifact().getBaseVersion());
    }
    if (feature.getDescription() == null) {
        feature.setDescription(project.getName());
    }
    if (installMode != null) {
        feature.setInstall(installMode);
    }
    if (project.getDescription() != null && feature.getDetails() == null) {
        feature.setDetails(project.getDescription());
    }
    if (includeProjectArtifact) {
        Bundle bundle = objectFactory.createBundle();
        bundle.setLocation(this.dependencyHelper.artifactToMvn(project.getArtifact(), project.getVersion()));
        if (startLevel != null) {
            bundle.setStartLevel(startLevel);
        }
        feature.getBundle().add(bundle);
    }
    boolean needWrap = false;
    // First pass to look for features
    // Track other features we depend on and their repositories (we track repositories instead of building them from
    // the feature's Maven artifact to allow for multi-feature repositories)
    // TODO Initialise the repositories from the existing feature file if any
    Map<Dependency, Feature> otherFeatures = new HashMap<>();
    Map<Feature, String> featureRepositories = new HashMap<>();
    FeaturesCache cache = new FeaturesCache(featuresCacheSize, artifactCacheSize);
    for (final LocalDependency entry : localDependencies) {
        Object artifact = entry.getArtifact();
        if (excludedArtifactIds.contains(this.dependencyHelper.getArtifactId(artifact))) {
            continue;
        }
        processFeatureArtifact(features, feature, otherFeatures, featureRepositories, cache, artifact, entry.getParent(), true);
    }
    // Do not retain cache beyond this point
    cache = null;
    // Second pass to look for bundles
    if (addBundlesToPrimaryFeature) {
        localDependency: for (final LocalDependency entry : localDependencies) {
            Object artifact = entry.getArtifact();
            if (excludedArtifactIds.contains(this.dependencyHelper.getArtifactId(artifact))) {
                continue;
            }
            if (!this.dependencyHelper.isArtifactAFeature(artifact)) {
                String bundleName = this.dependencyHelper.artifactToMvn(artifact, getVersionOrRange(entry.getParent(), artifact));
                for (ConfigFile cf : feature.getConfigfile()) {
                    if (bundleName.equals(cf.getLocation().replace('\n', ' ').trim())) {
                        // The bundle matches a configfile, ignore it
                        continue localDependency;
                    }
                }
                File bundleFile = this.dependencyHelper.resolve(artifact, getLog());
                Manifest manifest = getManifest(bundleFile);
                boolean bundleNeedsWrapping = false;
                if (manifest == null || !ManifestUtils.isBundle(manifest)) {
                    bundleName = "wrap:" + bundleName;
                    bundleNeedsWrapping = true;
                }
                Bundle bundle = null;
                for (Bundle b : feature.getBundle()) {
                    if (bundleName.equals(b.getLocation())) {
                        bundle = b;
                        break;
                    }
                }
                if (bundle == null) {
                    bundle = objectFactory.createBundle();
                    bundle.setLocation(bundleName);
                    // Check the features this feature depends on don't already contain the dependency
                    // TODO Perhaps only for transitive dependencies?
                    boolean includedTransitively = simplifyBundleDependencies && isBundleIncludedTransitively(feature, otherFeatures, bundle);
                    if (!includedTransitively && (!"provided".equals(entry.getScope()) || !ignoreScopeProvided)) {
                        feature.getBundle().add(bundle);
                        needWrap |= bundleNeedsWrapping;
                    }
                    if ((markRuntimeScopeAsDependency && "runtime".equals(entry.getScope())) || (markTransitiveAsDependency && entry.isTransitive())) {
                        bundle.setDependency(true);
                    }
                }
                if (startLevel != null && bundle.getStartLevel() == 0) {
                    bundle.setStartLevel(startLevel);
                }
            }
        }
    }
    if (needWrap) {
        Dependency wrapDependency = new Dependency();
        wrapDependency.setName("wrap");
        wrapDependency.setDependency(false);
        wrapDependency.setPrerequisite(true);
        feature.getFeature().add(wrapDependency);
    }
    if ((!feature.getBundle().isEmpty() || !feature.getFeature().isEmpty()) && !features.getFeature().contains(feature)) {
        features.getFeature().add(feature);
    }
    // Add any missing repositories for the included features
    for (Feature includedFeature : features.getFeature()) {
        for (Dependency dependency : includedFeature.getFeature()) {
            Feature dependedFeature = otherFeatures.get(dependency);
            if (dependedFeature != null && !features.getFeature().contains(dependedFeature)) {
                String repository = featureRepositories.get(dependedFeature);
                if (repository != null && !features.getRepository().contains(repository)) {
                    features.getRepository().add(repository);
                }
            }
        }
    }
    if (useJson) {
        try {
            JacksonUtil.marshal(features, out);
        } catch (Exception e) {
            throw new MojoExecutionException("Can't create features json", e);
        }
    } else {
        JaxbUtil.marshal(features, out);
    }
    try {
        checkChanges(features, objectFactory);
    } catch (Exception e) {
        throw new MojoExecutionException("Features contents have changed", e);
    }
    getLog().info("...done!");
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) LocalDependency(org.apache.karaf.tooling.utils.LocalDependency) Manifest(java.util.jar.Manifest) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MavenFilteringException(org.apache.maven.shared.filtering.MavenFilteringException) XMLStreamException(javax.xml.stream.XMLStreamException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LocalDependency(org.apache.karaf.tooling.utils.LocalDependency)

Example 34 with ArtifactResolutionException

use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project karaf by apache.

the class KarMojo method resolveFile.

private File resolveFile(String file) {
    File fileResolved = null;
    if (isMavenUrl(file)) {
        fileResolved = new File(fromMaven(file));
        try {
            Artifact artifactTemp = resourceToArtifact(file, false);
            if (!fileResolved.exists()) {
                try {
                    artifactResolver.resolve(artifactTemp, remoteRepos, localRepo);
                    fileResolved = artifactTemp.getFile();
                } catch (ArtifactResolutionException e) {
                    getLog().error("Artifact was not resolved", e);
                } catch (ArtifactNotFoundException e) {
                    getLog().error("Artifact was not found", e);
                }
            }
        } catch (MojoExecutionException e) {
            getLog().error(e);
        }
    } else {
        fileResolved = new File(file);
    }
    return fileResolved;
}
Also used : ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) Artifact(org.apache.maven.artifact.Artifact)

Example 35 with ArtifactResolutionException

use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project karaf by apache.

the class RunMojo method resolveFile.

private File resolveFile(String file) {
    File fileResolved = null;
    if (isMavenUrl(file)) {
        fileResolved = new File(fromMaven(file));
        try {
            Artifact artifactTemp = resourceToArtifact(file, false);
            if (!fileResolved.exists()) {
                try {
                    artifactResolver.resolve(artifactTemp, remoteRepos, localRepo);
                    fileResolved = artifactTemp.getFile();
                } catch (ArtifactResolutionException e) {
                    getLog().error("Artifact was not resolved", e);
                } catch (ArtifactNotFoundException e) {
                    getLog().error("Artifact was not found", e);
                }
            }
        } catch (MojoExecutionException e) {
            getLog().error(e);
        }
    } else {
        fileResolved = new File(file);
    }
    return fileResolved;
}
Also used : ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) Artifact(org.apache.maven.artifact.Artifact)

Aggregations

ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)35 Artifact (org.apache.maven.artifact.Artifact)27 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)27 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)15 ArrayList (java.util.ArrayList)12 File (java.io.File)10 IOException (java.io.IOException)8 ArtifactIncludeFilterTransformer (org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer)7 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)6 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)5 ArtifactResolutionResult (org.apache.maven.artifact.resolver.ArtifactResolutionResult)5 MojoFailureException (org.apache.maven.plugin.MojoFailureException)5 TransformableFilter (org.apache.maven.shared.artifact.filter.resolve.TransformableFilter)5 VersionRange (org.apache.maven.artifact.versioning.VersionRange)4 Dependency (org.apache.maven.model.Dependency)4 HashMap (java.util.HashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 ArtifactMetadataRetrievalException (org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException)3 MavenProject (org.apache.maven.project.MavenProject)3 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)3