Search in sources :

Example 1 with Builder

use of aQute.bnd.osgi.Builder in project felix by apache.

the class AntPlugin method execute.

@Override
protected void execute(MavenProject currentProject, DependencyNode dependencyGraph, Map<String, String> originalInstructions, Properties properties, Jar[] classpath) throws MojoExecutionException {
    final String artifactId = getProject().getArtifactId();
    final String baseDir = getProject().getBasedir().getPath();
    try {
        // assemble bundle as usual, but don't save it - this way we have all the instructions we need
        Builder builder = buildOSGiBundle(currentProject, dependencyGraph, originalInstructions, properties, classpath);
        Properties bndProperties = builder.getProperties();
        // cleanup and remove all non-strings from the builder properties
        for (Iterator i = bndProperties.values().iterator(); i.hasNext(); ) {
            if (!(i.next() instanceof String)) {
                i.remove();
            }
        }
        // save the BND generated bundle to the same output directory that maven uses
        bndProperties.setProperty("-output", "${maven.build.dir}/${maven.build.finalName}.jar");
        OutputStream out = buildContext.newFileOutputStream(new File(baseDir + BUILD_BND));
        bndProperties.store(out, " Merged BND Instructions");
        IOUtil.close(out);
        // modify build template
        String buildXml = IOUtil.toString(getClass().getResourceAsStream(BUILD_XML));
        buildXml = StringUtils.replace(buildXml, "BND_VERSION", builder.getVersion());
        buildXml = StringUtils.replace(buildXml, "ARTIFACT_ID", artifactId);
        FileUtils.fileWrite(baseDir + BUILD_XML, buildXml);
        // cleanup...
        builder.close();
    } catch (Exception e) {
        throw new MojoExecutionException("Problem creating Ant script", e);
    }
    getLog().info("Wrote Ant bundle project for " + artifactId + " to " + baseDir);
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Builder(aQute.bnd.osgi.Builder) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Properties(java.util.Properties) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 2 with Builder

use of aQute.bnd.osgi.Builder in project felix by apache.

the class BundlePlugin method getOSGiBuilder.

protected Builder getOSGiBuilder(MavenProject currentProject, Map<String, String> originalInstructions, Properties properties, Jar[] classpath) throws Exception {
    properties.putAll(getDefaultProperties(currentProject));
    properties.putAll(transformDirectives(originalInstructions));
    // process overrides from project
    final Map<String, String> addProps = new HashMap<String, String>();
    final Iterator<Map.Entry<Object, Object>> iter = currentProject.getProperties().entrySet().iterator();
    while (iter.hasNext()) {
        final Map.Entry<Object, Object> entry = iter.next();
        final String key = entry.getKey().toString();
        if (key.startsWith(BUNDLE_PLUGIN_EXTENSION)) {
            final String oKey = key.substring(BUNDLE_PLUGIN_EXTENSION.length());
            final String currentValue = properties.getProperty(oKey);
            if (currentValue == null) {
                addProps.put(oKey, entry.getValue().toString());
            } else {
                addProps.put(oKey, currentValue + ',' + entry.getValue());
            }
        }
        if (key.startsWith(BUNDLE_PLUGIN_PREPEND_EXTENSION)) {
            final String oKey = key.substring(BUNDLE_PLUGIN_PREPEND_EXTENSION.length());
            final String currentValue = properties.getProperty(oKey);
            if (currentValue == null) {
                addProps.put(oKey, entry.getValue().toString());
            } else {
                addProps.put(oKey, entry.getValue() + "," + currentValue);
            }
        }
    }
    properties.putAll(addProps);
    final Iterator<String> keyIter = addProps.keySet().iterator();
    while (keyIter.hasNext()) {
        Object key = keyIter.next();
        properties.remove(BUNDLE_PLUGIN_EXTENSION + key);
        properties.remove(BUNDLE_PLUGIN_PREPEND_EXTENSION + key);
    }
    if (properties.getProperty("Bundle-Activator") != null && properties.getProperty("Bundle-Activator").isEmpty()) {
        properties.remove("Bundle-Activator");
    }
    if (properties.containsKey("-disable-plugin")) {
        String[] disabled = properties.remove("-disable-plugin").toString().replaceAll(" ", "").split(",");
        String[] enabled = properties.getProperty(Analyzer.PLUGIN, "").replaceAll(" ", "").split(",");
        Set<String> plugin = new LinkedHashSet<String>();
        plugin.addAll(Arrays.asList(enabled));
        plugin.removeAll(Arrays.asList(disabled));
        StringBuilder sb = new StringBuilder();
        for (String s : plugin) {
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(s);
        }
        properties.setProperty(Analyzer.PLUGIN, sb.toString());
    }
    Builder builder = new Builder();
    synchronized (// protect setBase...getBndLastModified which uses static DateFormat
    BundlePlugin.class) {
        builder.setBase(getBase(currentProject));
    }
    builder.setProperties(sanitize(properties));
    if (classpath != null) {
        builder.setClasspath(classpath);
    }
    return builder;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MavenProjectBuilder(org.apache.maven.project.MavenProjectBuilder) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) Builder(aQute.bnd.osgi.Builder) DependencyTreeBuilder(org.apache.maven.shared.dependency.tree.DependencyTreeBuilder) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 3 with Builder

use of aQute.bnd.osgi.Builder in project felix by apache.

the class BundlePlugin method execute.

protected void execute(MavenProject currentProject, DependencyNode dependencyGraph, Map<String, String> originalInstructions, Properties properties, Jar[] classpath) throws MojoExecutionException {
    try {
        File jarFile = new File(getBuildDirectory(), getBundleName(currentProject));
        Builder builder = buildOSGiBundle(currentProject, dependencyGraph, originalInstructions, properties, classpath);
        boolean hasErrors = reportErrors("Bundle " + currentProject.getArtifact(), builder);
        if (hasErrors) {
            String failok = builder.getProperty("-failok");
            if (null == failok || "false".equalsIgnoreCase(failok)) {
                jarFile.delete();
                throw new MojoFailureException("Error(s) found in bundle configuration");
            }
        }
        // attach bundle to maven project
        jarFile.getParentFile().mkdirs();
        builder.getJar().write(jarFile);
        Artifact mainArtifact = currentProject.getArtifact();
        if ("bundle".equals(mainArtifact.getType())) {
            // workaround for MNG-1682: force maven to install artifact using the "jar" handler
            mainArtifact.setArtifactHandler(m_artifactHandlerManager.getArtifactHandler("jar"));
        }
        boolean customClassifier = null != classifier && classifier.trim().length() > 0;
        boolean customPackaging = null != packaging && packaging.trim().length() > 0;
        if (customClassifier && customPackaging) {
            m_projectHelper.attachArtifact(currentProject, packaging, classifier, jarFile);
        } else if (customClassifier) {
            m_projectHelper.attachArtifact(currentProject, jarFile, classifier);
        } else if (customPackaging) {
            m_projectHelper.attachArtifact(currentProject, packaging, jarFile);
        } else {
            mainArtifact.setFile(jarFile);
        }
        if (unpackBundle) {
            unpackBundle(jarFile);
        }
        if (manifestLocation != null) {
            File outputFile = new File(manifestLocation, "MANIFEST.MF");
            try {
                ManifestPlugin.writeManifest(builder, outputFile, niceManifest, exportScr, scrLocation, buildContext, getLog());
            } catch (IOException e) {
                getLog().error("Error trying to write Manifest to file " + outputFile, e);
            }
        }
        // cleanup...
        builder.close();
    } catch (MojoFailureException e) {
        getLog().error(e.getLocalizedMessage());
        throw new MojoExecutionException("Error(s) found in bundle configuration", e);
    } catch (Exception e) {
        getLog().error("An internal error occurred", e);
        throw new MojoExecutionException("Internal error in maven-bundle-plugin", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenProjectBuilder(org.apache.maven.project.MavenProjectBuilder) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) Builder(aQute.bnd.osgi.Builder) DependencyTreeBuilder(org.apache.maven.shared.dependency.tree.DependencyTreeBuilder) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact) DependencyTreeBuilderException(org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 4 with Builder

use of aQute.bnd.osgi.Builder in project felix by apache.

the class BlueprintComponentTest method test.

protected void test(String mode) throws Exception {
    MavenProjectStub project = new MavenProjectStub() {

        private final List resources = new ArrayList();

        @Override
        public void addResource(Resource resource) {
            resources.add(resource);
        }

        @Override
        public List getResources() {
            return resources;
        }

        @Override
        public File getBasedir() {
            return new File("target/tmp/basedir");
        }
    };
    project.setGroupId("group");
    project.setArtifactId("artifact");
    project.setVersion("1.1.0.0");
    VersionRange versionRange = VersionRange.createFromVersion(project.getVersion());
    ArtifactHandler artifactHandler = new DefaultArtifactHandler("jar");
    Artifact artifact = new DefaultArtifact(project.getGroupId(), project.getArtifactId(), versionRange, null, "jar", null, artifactHandler);
    project.setArtifact(artifact);
    ProjectBuilderConfiguration projectBuilderConfiguration = new DefaultProjectBuilderConfiguration();
    projectBuilderConfiguration.setLocalRepository(null);
    project.setProjectBuilderConfiguration(projectBuilderConfiguration);
    Resource r = new Resource();
    r.setDirectory(new File("src/test/resources").getAbsoluteFile().getCanonicalPath());
    r.setIncludes(Arrays.asList("**/*.*"));
    project.addResource(r);
    project.addCompileSourceRoot(new File("src/test/resources").getAbsoluteFile().getCanonicalPath());
    ManifestPlugin plugin = new ManifestPlugin();
    plugin.setBuildDirectory("target/tmp/basedir/target");
    plugin.setOutputDirectory(new File("target/tmp/basedir/target/classes"));
    setVariableValueToObject(plugin, "m_dependencyGraphBuilder", lookup(DependencyGraphBuilder.class.getName(), "default"));
    Map instructions = new HashMap();
    instructions.put("service_mode", mode);
    instructions.put("Test", "Foo");
    instructions.put("nsh_interface", "foo.bar.Namespace");
    instructions.put("nsh_namespace", "ns");
    instructions.put("Export-Service", "p7.Foo;mk=mv");
    instructions.put("Import-Service", "org.osgi.service.cm.ConfigurationAdmin;availability:=optional");
    Properties props = new Properties();
    DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
    Builder builder = plugin.buildOSGiBundle(project, dependencyGraph, instructions, props, plugin.getClasspath(project, dependencyGraph));
    Manifest manifest = builder.getJar().getManifest();
    String impSvc = manifest.getMainAttributes().getValue(Constants.IMPORT_SERVICE);
    if ("service".equals(mode)) {
        String expSvc = manifest.getMainAttributes().getValue(Constants.EXPORT_SERVICE);
        assertNotNull(expSvc);
        assertTrue(expSvc.contains("beanRef.Foo;osgi.service.blueprint.compname=myBean"));
    } else {
        String prvCap = manifest.getMainAttributes().getValue(Constants.PROVIDE_CAPABILITY);
        assertNotNull(prvCap);
        assertTrue(prvCap.contains("osgi.service;effective:=active;objectClass=\"beanRef.Foo\";osgi.service.blueprint.compname=myBean"));
    }
    assertNotNull(impSvc);
    String impPkg = manifest.getMainAttributes().getValue(Constants.IMPORT_PACKAGE);
    List<String> pkgs = Create.list();
    for (Clause clause : Parser.parseHeader(impPkg)) {
        pkgs.add(clause.getName());
    }
    for (int i = 1; i <= 14; i++) {
        assertTrue(pkgs.contains("p" + i));
    }
    try (Verifier verifier = new Verifier(builder)) {
        verifier.verify();
    }
}
Also used : HashMap(java.util.HashMap) DefaultProjectBuilderConfiguration(org.apache.maven.project.DefaultProjectBuilderConfiguration) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) Builder(aQute.bnd.osgi.Builder) ArrayList(java.util.ArrayList) Resource(org.apache.maven.model.Resource) VersionRange(org.apache.maven.artifact.versioning.VersionRange) Properties(java.util.Properties) Manifest(java.util.jar.Manifest) Verifier(aQute.bnd.osgi.Verifier) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) ProjectBuilderConfiguration(org.apache.maven.project.ProjectBuilderConfiguration) DefaultProjectBuilderConfiguration(org.apache.maven.project.DefaultProjectBuilderConfiguration) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) MavenProjectStub(org.apache.maven.plugin.testing.stubs.MavenProjectStub) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) ArrayList(java.util.ArrayList) List(java.util.List) Clause(org.apache.felix.utils.manifest.Clause) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 5 with Builder

use of aQute.bnd.osgi.Builder in project felix by apache.

the class BundlePluginTest method testEmbedDependencyDuplicateKeys.

public void testEmbedDependencyDuplicateKeys() throws Exception {
    ArtifactStubFactory artifactFactory = new ArtifactStubFactory(plugin.getOutputDirectory(), true);
    Set artifacts = new LinkedHashSet();
    artifacts.addAll(artifactFactory.getClassifiedArtifacts());
    artifacts.addAll(artifactFactory.getScopedArtifacts());
    artifacts.addAll(artifactFactory.getTypedArtifacts());
    MavenProject project = getMavenProjectStub();
    project.setDependencyArtifacts(artifacts);
    Map instructions = new HashMap();
    instructions.put(DependencyEmbedder.EMBED_DEPENDENCY, "c;type=jar,c;type=sources");
    Properties props = new Properties();
    DependencyNode dependencyGraph = plugin.buildDependencyGraph(project);
    Builder builder = plugin.buildOSGiBundle(project, dependencyGraph, instructions, props, plugin.getClasspath(project, dependencyGraph));
    Manifest manifest = builder.getJar().getManifest();
    String bcp = manifest.getMainAttributes().getValue(Constants.BUNDLE_CLASSPATH);
    assertEquals(".," + "c-1.0-three.jar," + "c-1.0.sources", bcp);
    String eas = manifest.getMainAttributes().getValue("Embedded-Artifacts");
    assertEquals("c-1.0-three.jar;g=\"g\";a=\"c\";v=\"1.0\";c=\"three\"," + "c-1.0.sources;g=\"g\";a=\"c\";v=\"1.0\"", eas);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) MavenProject(org.apache.maven.project.MavenProject) HashMap(java.util.HashMap) DependencyNode(org.apache.maven.shared.dependency.graph.DependencyNode) Builder(aQute.bnd.osgi.Builder) DependencyGraphBuilder(org.apache.maven.shared.dependency.graph.DependencyGraphBuilder) Properties(java.util.Properties) Manifest(java.util.jar.Manifest) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

Builder (aQute.bnd.osgi.Builder)440 Jar (aQute.bnd.osgi.Jar)228 File (java.io.File)203 Properties (java.util.Properties)99 Manifest (java.util.jar.Manifest)99 Resource (aQute.bnd.osgi.Resource)83 Attributes (java.util.jar.Attributes)43 DocumentBuilder (javax.xml.parsers.DocumentBuilder)41 XmlTester (aQute.bnd.test.XmlTester)39 Parameters (aQute.bnd.header.Parameters)35 Document (org.w3c.dom.Document)27 Attrs (aQute.bnd.header.Attrs)21 JarResource (aQute.bnd.osgi.JarResource)20 LogService (org.osgi.service.log.LogService)17 ProjectBuilder (aQute.bnd.build.ProjectBuilder)16 Domain (aQute.bnd.osgi.Domain)16 ArrayList (java.util.ArrayList)14 DependencyGraphBuilder (org.apache.maven.shared.dependency.graph.DependencyGraphBuilder)13 HashMap (java.util.HashMap)12 Project (aQute.bnd.build.Project)10