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);
}
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;
}
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);
}
}
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();
}
}
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);
}
Aggregations