Search in sources :

Example 51 with Artifact

use of org.apache.maven.artifact.Artifact in project karaf by apache.

the class MojoSupport method resourceToArtifact.

/**
     * Convert a feature resourceLocation (bundle or configuration file) into an artifact.
     *
     * @param resourceLocation The feature resource location (bundle or configuration file).
     * @param skipNonMavenProtocols A flag to skip protocol different than mvn:
     * @return The artifact corresponding to the resource.
     * @throws MojoExecutionException If the plugin execution fails.
     */
protected Artifact resourceToArtifact(String resourceLocation, boolean skipNonMavenProtocols) throws MojoExecutionException {
    resourceLocation = resourceLocation.replace("\r\n", "").replace("\n", "").replace(" ", "").replace("\t", "");
    final int index = resourceLocation.indexOf("mvn:");
    if (index < 0) {
        if (skipNonMavenProtocols) {
            return null;
        }
        throw new MojoExecutionException("Resource URL is not a Maven URL: " + resourceLocation);
    } else {
        resourceLocation = resourceLocation.substring(index + "mvn:".length());
    }
    // Truncate the URL when a '#', a '?' or a '$' is encountered
    final int index1 = resourceLocation.indexOf('?');
    final int index2 = resourceLocation.indexOf('#');
    int endIndex = -1;
    if (index1 > 0) {
        if (index2 > 0) {
            endIndex = Math.min(index1, index2);
        } else {
            endIndex = index1;
        }
    } else if (index2 > 0) {
        endIndex = index2;
    }
    if (endIndex >= 0) {
        resourceLocation = resourceLocation.substring(0, endIndex);
    }
    final int index3 = resourceLocation.indexOf('$');
    if (index3 > 0) {
        resourceLocation = resourceLocation.substring(0, index3);
    }
    //check if the resourceLocation descriptor contains also remote repository information.
    ArtifactRepository repo = null;
    if (resourceLocation.startsWith("http://")) {
        final int repoDelimIntex = resourceLocation.indexOf('!');
        String repoUrl = resourceLocation.substring(0, repoDelimIntex);
        repo = new DefaultArtifactRepository(repoUrl, repoUrl, new DefaultRepositoryLayout());
        org.apache.maven.repository.Proxy mavenProxy = configureProxyToInlineRepo();
        if (mavenProxy != null) {
            repo.setProxy(mavenProxy);
        }
        resourceLocation = resourceLocation.substring(repoDelimIntex + 1);
    }
    String[] parts = resourceLocation.split("/");
    String groupId = parts[0];
    String artifactId = parts[1];
    String version = null;
    String classifier = null;
    String type = "jar";
    if (parts.length > 2) {
        version = parts[2];
        if (parts.length > 3) {
            type = parts[3];
            if (parts.length > 4) {
                classifier = parts[4];
            }
        }
    } else {
        Dependency dep = findDependency(project.getDependencies(), artifactId, groupId);
        if (dep == null && project.getDependencyManagement() != null) {
            dep = findDependency(project.getDependencyManagement().getDependencies(), artifactId, groupId);
        }
        if (dep != null) {
            version = dep.getVersion();
            classifier = dep.getClassifier();
            type = dep.getType();
        }
    }
    if (version == null || version.isEmpty()) {
        throw new MojoExecutionException("Cannot find version for: " + resourceLocation);
    }
    Artifact artifact = factory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier);
    artifact.setRepository(repo);
    return artifact;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) DefaultRepositoryLayout(org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) DefaultArtifactRepository(org.apache.maven.artifact.repository.DefaultArtifactRepository) Dependency(org.apache.maven.model.Dependency) Artifact(org.apache.maven.artifact.Artifact)

Example 52 with Artifact

use of org.apache.maven.artifact.Artifact in project karaf by apache.

the class MojoSupport method createManagedVersionMap.

protected Map createManagedVersionMap(String projectId, DependencyManagement dependencyManagement) throws ProjectBuildingException {
    Map map;
    if (dependencyManagement != null && dependencyManagement.getDependencies() != null) {
        map = new HashMap();
        for (Dependency d : dependencyManagement.getDependencies()) {
            try {
                VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion());
                Artifact artifact = factory.createDependencyArtifact(d.getGroupId(), d.getArtifactId(), versionRange, d.getType(), d.getClassifier(), d.getScope());
                map.put(d.getManagementKey(), artifact);
            } catch (InvalidVersionSpecificationException e) {
                throw new ProjectBuildingException(projectId, "Unable to parse version '" + d.getVersion() + "' for dependency '" + d.getManagementKey() + "': " + e.getMessage(), e);
            }
        }
    } else {
        map = Collections.EMPTY_MAP;
    }
    return map;
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) HashMap(java.util.HashMap) VersionRange(org.apache.maven.artifact.versioning.VersionRange) Dependency(org.apache.maven.model.Dependency) HashMap(java.util.HashMap) Map(java.util.Map) Artifact(org.apache.maven.artifact.Artifact)

Example 53 with Artifact

use of org.apache.maven.artifact.Artifact in project karaf by apache.

the class AddToRepositoryMojo method copyConfigFilesToDestRepository.

private void copyConfigFilesToDestRepository(List<? extends ConfigFile> artifactRefs) throws MojoExecutionException {
    for (ConfigFile artifactRef : artifactRefs) {
        Artifact artifact = resourceToArtifact(artifactRef.getLocation(), skipNonMavenProtocols);
        // Avoid getting NPE on artifact.getFile in some cases
        resolveArtifact(artifact, remoteRepos);
        if (artifact != null) {
            copy(artifact, repository);
        }
    }
}
Also used : ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) Artifact(org.apache.maven.artifact.Artifact)

Example 54 with Artifact

use of org.apache.maven.artifact.Artifact in project karaf by apache.

the class AbstractFeatureMojo method retrieveDescriptorsRecursively.

protected void retrieveDescriptorsRecursively(String uri, Set<String> bundles, Map<String, Feature> featuresMap) {
    // let's ensure a mvn: based url is sitting in the local repo before we try reading it
    Artifact descriptor;
    try {
        descriptor = resourceToArtifact(uri, true);
    } catch (MojoExecutionException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    if (descriptor != null) {
        resolveArtifact(descriptor, remoteRepos);
        descriptorArtifacts.add(descriptor);
    }
    if (includeMvnBasedDescriptors) {
        bundles.add(uri);
    }
    Features repo = JaxbUtil.unmarshal(translateFromMaven(uri.replaceAll(" ", "%20")), true);
    for (Feature f : repo.getFeature()) {
        featuresMap.put(f.getId(), f);
    }
    if (resolveDefinedRepositoriesRecursively) {
        for (String r : repo.getRepository()) {
            retrieveDescriptorsRecursively(r, bundles, featuresMap);
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Features(org.apache.karaf.features.internal.model.Features) Feature(org.apache.karaf.features.internal.model.Feature) Artifact(org.apache.maven.artifact.Artifact)

Example 55 with Artifact

use of org.apache.maven.artifact.Artifact in project karaf by apache.

the class AbstractFeatureMojo method addFeatureRepo.

protected void addFeatureRepo(String featureUrl) throws MojoExecutionException {
    Artifact featureDescArtifact = resourceToArtifact(featureUrl, true);
    if (featureDescArtifact == null) {
        return;
    }
    try {
        resolveArtifact(featureDescArtifact, remoteRepos);
        descriptors.add(0, featureUrl);
    } catch (Exception e) {
        getLog().warn("Can't add " + featureUrl + " in the descriptors set");
        getLog().debug(e);
    }
}
Also used : Artifact(org.apache.maven.artifact.Artifact) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

Artifact (org.apache.maven.artifact.Artifact)450 File (java.io.File)175 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)92 ArrayList (java.util.ArrayList)91 MavenProject (org.apache.maven.project.MavenProject)63 IOException (java.io.IOException)50 HashSet (java.util.HashSet)42 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)32 LinkedHashSet (java.util.LinkedHashSet)29 MojoFailureException (org.apache.maven.plugin.MojoFailureException)24 HashMap (java.util.HashMap)22 Set (java.util.Set)22 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)22 ScopeArtifactFilter (org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter)21 URL (java.net.URL)20 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)20 Test (org.junit.Test)20 MalformedURLException (java.net.MalformedURLException)18 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)17 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)16