Skip to content

Commit

Permalink
Add a MakeJar to create a plain-old-jar file as a resource
Browse files Browse the repository at this point in the history
Currently one can already create a new bundle using additional bnd
instructions file but this has some caveats when one only wants to
package some stuff into a jar and also some processing overhead.

This adds a new MakeJar that do what the name suggest and create a dumb
jar from an input directory.

Signed-off-by: Christoph Läubrich <[email protected]>
  • Loading branch information
laeubi committed Dec 3, 2023
1 parent 2983be5 commit b816682
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
49 changes: 49 additions & 0 deletions biz.aQute.bndlib/src/aQute/bnd/make/MakeJar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package aQute.bnd.make;

import java.io.File;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import aQute.bnd.osgi.About;
import aQute.bnd.osgi.Builder;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.JarResource;
import aQute.bnd.osgi.Resource;
import aQute.bnd.service.MakePlugin;

public class MakeJar implements MakePlugin {

@Override
public Resource make(Builder builder, String destination, Map<String, String> argumentsOnMake) throws Exception {
String type = argumentsOnMake.get("type"); //$NON-NLS-1$
if (!"jar".equals(type)) { //$NON-NLS-1$
return null;
}
String input = argumentsOnMake.get("input"); //$NON-NLS-1$
if (input == null) {
builder.error("No input specified on a make instruction for %s, args=%s", destination, argumentsOnMake); //$NON-NLS-1$
return null;
}
File folder = builder.getFile(input);
if (!folder.isDirectory()) {
return null;
}
Jar jar = new Jar(folder);
Manifest manifest = new Manifest();
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); //$NON-NLS-1$
if (!Boolean.parseBoolean(argumentsOnMake.get("noExtra"))) { //$NON-NLS-1$
mainAttributes.putValue(Constants.CREATED_BY,
String.format("%s (%s)", System.getProperty("java.version"), System.getProperty("java.vendor"))); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
mainAttributes.putValue(Constants.TOOL, "Bnd-" + About.getBndVersion()); //$NON-NLS-1$
if (!Boolean.parseBoolean(argumentsOnMake.get("reproducible"))) { //$NON-NLS-1$
mainAttributes.putValue(Constants.BND_LASTMODIFIED, Long.toString(System.currentTimeMillis()));
}
}
jar.setManifest(manifest);
return new JarResource(jar);
}

}
3 changes: 3 additions & 0 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import aQute.bnd.make.Make;
import aQute.bnd.make.MakeBnd;
import aQute.bnd.make.MakeCopy;
import aQute.bnd.make.MakeJar;
import aQute.bnd.make.component.ServiceComponent;
import aQute.bnd.maven.PomPropertiesResource;
import aQute.bnd.maven.PomResource;
Expand Down Expand Up @@ -1732,6 +1733,7 @@ public Pattern getDoNotCopy() {
*/

static MakeBnd makeBnd = new MakeBnd();
static MakeJar makeJar = new MakeJar();
static MakeCopy makeCopy = new MakeCopy();
static ServiceComponent serviceComponent = new ServiceComponent();
static CDIAnnotations cdiAnnotations = new CDIAnnotations();
Expand All @@ -1745,6 +1747,7 @@ public Pattern getDoNotCopy() {
@Override
protected void setTypeSpecificPlugins(PluginsContainer pluginsContainer) {
pluginsContainer.add(makeBnd);
pluginsContainer.add(makeJar);
pluginsContainer.add(makeCopy);
pluginsContainer.add(serviceComponent);
pluginsContainer.add(cdiAnnotations);
Expand Down

0 comments on commit b816682

Please sign in to comment.