Search in sources :

Example 6 with Dependency

use of org.apache.karaf.features.internal.model.Dependency 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<>();
    for (final LocalDependency entry : localDependencies) {
        Object artifact = entry.getArtifact();
        if (excludedArtifactIds.contains(this.dependencyHelper.getArtifactId(artifact))) {
            continue;
        }
        processFeatureArtifact(features, feature, otherFeatures, featureRepositories, artifact, entry.getParent(), true);
    }
    // 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));
                File bundleFile = this.dependencyHelper.resolve(artifact, getLog());
                Manifest manifest = getManifest(bundleFile);
                for (ConfigFile cf : feature.getConfigfile()) {
                    if (bundleName.equals(cf.getLocation().replace('\n', ' ').trim())) {
                        // The bundle matches a configfile, ignore it
                        continue localDependency;
                    }
                }
                if (manifest == null || !ManifestUtils.isBundle(getManifest(bundleFile))) {
                    bundleName = "wrap:" + bundleName;
                    needWrap = 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);
                    }
                }
                if ("runtime".equals(entry.getScope())) {
                    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);
                }
            }
        }
    }
    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) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) Bundle(org.apache.karaf.features.internal.model.Bundle) Dependency(org.apache.karaf.features.internal.model.Dependency) LocalDependency(org.apache.karaf.tooling.utils.LocalDependency) Manifest(java.util.jar.Manifest) Feature(org.apache.karaf.features.internal.model.Feature) 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) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LocalDependency(org.apache.karaf.tooling.utils.LocalDependency) ObjectFactory(org.apache.karaf.features.internal.model.ObjectFactory) Features(org.apache.karaf.features.internal.model.Features) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) File(java.io.File)

Example 7 with Dependency

use of org.apache.karaf.features.internal.model.Dependency in project karaf by apache.

the class GenerateDescriptorMojo method processFeatureArtifact.

private void processFeatureArtifact(Features features, Feature feature, Map<Dependency, Feature> otherFeatures, Map<Feature, String> featureRepositories, Object artifact, Object parent, boolean add) throws MojoExecutionException, XMLStreamException, JAXBException, IOException {
    if (this.dependencyHelper.isArtifactAFeature(artifact) && FEATURE_CLASSIFIER.equals(this.dependencyHelper.getClassifier(artifact))) {
        File featuresFile = this.dependencyHelper.resolve(artifact, getLog());
        if (featuresFile == null || !featuresFile.exists()) {
            throw new MojoExecutionException("Cannot locate file for feature: " + artifact + " at " + featuresFile);
        }
        Features includedFeatures = readFeaturesFile(featuresFile);
        for (String repository : includedFeatures.getRepository()) {
            processFeatureArtifact(features, feature, otherFeatures, featureRepositories, new DefaultArtifact(MavenUtil.mvnToAether(repository)), parent, false);
        }
        for (Feature includedFeature : includedFeatures.getFeature()) {
            Dependency dependency = new Dependency(includedFeature.getName(), includedFeature.getVersion());
            dependency.setPrerequisite(prerequisiteFeatures.contains(dependency.getName()));
            dependency.setDependency(dependencyFeatures.contains(dependency.getName()));
            // Determine what dependency we're actually going to use
            Dependency matchingDependency = findMatchingDependency(feature.getFeature(), dependency);
            if (matchingDependency != null) {
                // The feature already has a matching dependency, merge
                mergeDependencies(matchingDependency, dependency);
                dependency = matchingDependency;
            }
            // We mustn't de-duplicate here, we may have seen a feature in !add mode
            otherFeatures.put(dependency, includedFeature);
            if (add) {
                if (!feature.getFeature().contains(dependency)) {
                    feature.getFeature().add(dependency);
                }
                if (aggregateFeatures) {
                    features.getFeature().add(includedFeature);
                }
            }
            if (!featureRepositories.containsKey(includedFeature)) {
                featureRepositories.put(includedFeature, this.dependencyHelper.artifactToMvn(artifact, getVersionOrRange(parent, artifact)));
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Features(org.apache.karaf.features.internal.model.Features) Dependency(org.apache.karaf.features.internal.model.Dependency) LocalDependency(org.apache.karaf.tooling.utils.LocalDependency) ConfigFile(org.apache.karaf.features.internal.model.ConfigFile) File(java.io.File) Feature(org.apache.karaf.features.internal.model.Feature) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Aggregations

Dependency (org.apache.karaf.features.internal.model.Dependency)7 Feature (org.apache.karaf.features.internal.model.Feature)6 Features (org.apache.karaf.features.internal.model.Features)5 File (java.io.File)4 Bundle (org.apache.karaf.features.internal.model.Bundle)4 ConfigFile (org.apache.karaf.features.internal.model.ConfigFile)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LocalDependency (org.apache.karaf.tooling.utils.LocalDependency)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Manifest (java.util.jar.Manifest)2 Properties (org.apache.felix.utils.properties.Properties)2