Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a MakeJar to create a plain-old-jar file as a resource #5918

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 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,61 @@
package aQute.bnd.make;

import java.io.ByteArrayOutputStream;
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.AbstractResource;
import aQute.bnd.osgi.Builder;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Jar;
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;
}
boolean noExtra = Boolean.parseBoolean(argumentsOnMake.get("noExtra")); //$NON-NLS-1$
boolean reproducible = Boolean.parseBoolean(argumentsOnMake.get("reproducible")); //$NON-NLS-1$
return new AbstractResource(System.currentTimeMillis()) {

@Override
protected byte[] getBytes() throws Exception {
try (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 (!noExtra) {
mainAttributes.putValue(Constants.CREATED_BY, String.format("%s (%s)", //$NON-NLS-1$
System.getProperty("java.version"), System.getProperty("java.vendor"))); //$NON-NLS-1$ //$NON-NLS-2$
mainAttributes.putValue(Constants.TOOL, "Bnd-" + About.getBndVersion()); //$NON-NLS-1$
if (!reproducible) {
mainAttributes.putValue(Constants.BND_LASTMODIFIED, Long.toString(lastModified()));
}
}
jar.setManifest(manifest);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
jar.write(stream);
return stream.toByteArray();
}
}
};
}

}
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 @@ -1733,6 +1734,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 @@ -1747,6 +1749,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