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

Extracted meta inf #693

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtilRt;
import com.intellij.util.containers.ContainerUtil;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.builders.logging.ProjectBuilderLogger;
Expand All @@ -28,8 +29,13 @@
import org.jetbrains.osgi.jps.util.OsgiBuildUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

Expand All @@ -38,6 +44,9 @@
public class OsgiBuildSession implements Reporter {
private static final Logger LOG = Logger.getInstance(OsgiBuildSession.class);

private static final String META_INF = "META-INF";
private static final String OSGI_INF = "OSGI-INF";

private OsmorcBuildTarget myTarget;
private CompileContext myContext;
private JpsOsmorcModuleExtension myExtension;
Expand Down Expand Up @@ -81,9 +90,70 @@ public void build(@NotNull OsmorcBuildTarget target, @NotNull CompileContext con
logger.logCompiledFiles(myOutputJarFiles, OsmorcBuilder.ID, "Built OSGi bundles:");
}

if (myExtension.isExtractMetaInfOsgIInfToTargetClasses()) {

extractJarToTargetClasses();
}

context.processMessage(DoneSomethingNotification.INSTANCE);
}

private void extractJarToTargetClasses() throws IOException {
for (File file : myOutputJarFiles) {
try (JarFile jarFile = new JarFile(file)) {

for (JarEntry entry : Collections.list(jarFile.entries())) {

if (entry.getName().startsWith(META_INF)) {
extractEntry(jarFile, entry);
}

if (entry.getName().startsWith(OSGI_INF)) {
extractEntry(jarFile, entry);
}


}
}

}
}

private void extractEntry(JarFile jarFile, JarEntry entry) throws IOException {

try (InputStream is = jarFile.getInputStream(entry)) {

File targetFile = new File(myModuleOutputDir, entry.getName());

if (entry.isDirectory()) {
if (!targetFile.exists()) {
targetFile.mkdirs();
}

} else {
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
try (FileOutputStream fos = new FileOutputStream(targetFile)) {
// Allocate a buffer for reading the entry data.
byte[] buffer = new byte[1024];
int bytesRead;

// Read the entry data and write it to the output file.

while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fos.flush();
}
}
} catch (IOException e) {
error(e.getMessage(), e.getCause(), jarFile.getName(), -1);
throw e;

}
}

private void prepare() throws OsgiBuildException {
myModuleOutputDir = JpsJavaExtensionService.getInstance().getOutputDirectory(myModule, false);
if (myModuleOutputDir == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public interface JpsOsmorcModuleExtension extends JpsElement {

boolean isAlwaysRebuildBundleJar();

boolean isExtractMetaInfOsgIInfToTargetClasses();

@NotNull
List<OsmorcJarContentEntry> getAdditionalJarContents();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ public boolean isAlwaysRebuildBundleJar() {
return myProperties.myAlwaysRebuildBundleJar;
}

@Override
public boolean isExtractMetaInfOsgIInfToTargetClasses() {
return myProperties.myExtractMetaInfOsgIInfToTargetClasses;
}

@NotNull
@Override
public List<OsmorcJarContentEntry> getAdditionalJarContents() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class OsmorcModuleExtensionProperties {
@XCollection(propertyElementName = "additionalJARContents")
public List<OsmorcJarContentEntry> myAdditionalJARContents = new ArrayList<>();

@Attribute("extractMetaInfOsgiInfToTargetClasses")
public boolean myExtractMetaInfOsgIInfToTargetClasses = true;

@Attribute("ignoreFilePattern")
public String myIgnoreFilePattern;

Expand Down
1 change: 1 addition & 0 deletions osmorc/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<li>Add the OSGi framework that you want to work with (Equinox, Felix, etc.).</li>
<li>Add the OSGi facet to any module that should be an OSGi bundle.</li>
<li>You can either write your own manifest or let the plugin calculate the manifest for you.</li>
<li>Introduced option to extract META-INF and OSGI-INF folder from generated jar file into modules output folder.</li>
</ol>

<p>
Expand Down
13 changes: 13 additions & 0 deletions osmorc/src/org/osmorc/facet/OsmorcFacetConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class OsmorcFacetConfiguration implements FacetConfiguration, Modificatio
private static final String ADDITIONAL_PROPERTIES = "additionalProperties";
private static final String IGNORE_FILE_PATTERN = "ignoreFilePattern";
private static final String ALWAYS_REBUILD_BUNDLE_JAR = "alwaysRebuildBundleJAR";
private static final String EXTRACT_META_INF_OSGI_INF_TO_TARGET_CLASSES = "extractMetaInfOsgiInfToTargetClasses";
private static final String DO_NOT_SYNCHRONIZE_WITH_MAVEN = "doNotSynchronizeWithMaven";
private static final String OUTPUT_PATH_TYPE = "outputPathType";
private static final String PROPERTY = "property";
Expand All @@ -108,6 +109,7 @@ public class OsmorcFacetConfiguration implements FacetConfiguration, Modificatio
private String myBundlorFileLocation;
private String myIgnoreFilePattern;
private boolean myAlwaysRebuildBundleJAR;
private boolean myExtractMetaInfOsgIInfToTargetClasses = true;
private OutputPathType myOutputPathType;
private ManifestGenerationMode myManifestGenerationMode = ManifestGenerationMode.OsmorcControlled;

Expand Down Expand Up @@ -176,6 +178,7 @@ else if (!osmorcControlsManifest && !useBndFile) {
setIgnoreFilePattern(element.getAttributeValue(IGNORE_FILE_PATTERN));
setUseProjectDefaultManifestFileLocation(Boolean.parseBoolean(element.getAttributeValue(USE_PROJECT_DEFAULT_MANIFEST_FILE_LOCATION, "true")));
setAlwaysRebuildBundleJAR(Boolean.parseBoolean(element.getAttributeValue(ALWAYS_REBUILD_BUNDLE_JAR, "false")));
setExtractMetaInfOsgIInfToTargetClasses(Boolean.parseBoolean(element.getAttributeValue(EXTRACT_META_INF_OSGI_INF_TO_TARGET_CLASSES, "true")));
setDoNotSynchronizeWithMaven(Boolean.parseBoolean(element.getAttributeValue(DO_NOT_SYNCHRONIZE_WITH_MAVEN, "false")));

Element props = element.getChild(ADDITIONAL_PROPERTIES);
Expand Down Expand Up @@ -224,6 +227,7 @@ public void writeExternal(Element element) throws WriteExternalException {
element.setAttribute(IGNORE_FILE_PATTERN, getIgnoreFilePattern());
element.setAttribute(USE_PROJECT_DEFAULT_MANIFEST_FILE_LOCATION, String.valueOf(isUseProjectDefaultManifestFileLocation()));
element.setAttribute(ALWAYS_REBUILD_BUNDLE_JAR, String.valueOf(isAlwaysRebuildBundleJAR()));
element.setAttribute(EXTRACT_META_INF_OSGI_INF_TO_TARGET_CLASSES, String.valueOf(isExtractMetaInfOsgIInfToTargetClasses()));
element.setAttribute(DO_NOT_SYNCHRONIZE_WITH_MAVEN, String.valueOf(myDoNotSynchronizeWithMaven));

Element props = new Element(ADDITIONAL_PROPERTIES);
Expand Down Expand Up @@ -559,6 +563,15 @@ public void setAlwaysRebuildBundleJAR(boolean alwaysRebuildBundleJAR) {
myModificationCount++;
}

public boolean isExtractMetaInfOsgIInfToTargetClasses() {
return myExtractMetaInfOsgIInfToTargetClasses;
}

public void setExtractMetaInfOsgIInfToTargetClasses(boolean extractMetaInfOsgIInfToTargetClasses) {
myExtractMetaInfOsgIInfToTargetClasses = extractMetaInfOsgIInfToTargetClasses;
myModificationCount++;
}

public boolean isDoNotSynchronizeWithMaven() {
return myDoNotSynchronizeWithMaven;
}
Expand Down
29 changes: 17 additions & 12 deletions osmorc/src/org/osmorc/facet/ui/OsmorcFacetJAREditorTab.form
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.osmorc.facet.ui.OsmorcFacetJAREditorTab">
<grid id="bbf5b" binding="myRoot" layout-manager="GridLayoutManager" row-count="7" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="bbf5b" binding="myRoot" layout-manager="GridLayoutManager" row-count="8" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="638" height="405"/>
Expand All @@ -21,15 +21,15 @@
</component>
<component id="d6235" class="javax.swing.JCheckBox" binding="myAlwaysRebuildBundleJARCheckBox" default-binding="true">
<constraints>
<grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Always rebuild bundle .jar"/>
</properties>
</component>
<component id="ca0f8" class="javax.swing.JTextField" binding="myJarFileTextField" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="0" column="3" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
Expand All @@ -45,43 +45,40 @@
</component>
<component id="ad914" class="javax.swing.JRadioButton" binding="myPlaceInCompilerOutputPathRadioButton" default-binding="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="3" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Place in module's output path"/>
</properties>
</component>
<component id="9016" class="javax.swing.JRadioButton" binding="myPlaceInProjectWideRadioButton" default-binding="true">
<constraints>
<grid row="2" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="2" column="3" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Place in project-wide OSGi bundle output path"/>
</properties>
</component>
<component id="f3f8" class="javax.swing.JRadioButton" binding="myPlaceInThisPathRadioButton" default-binding="true">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Place in this path:"/>
</properties>
</component>
<grid id="17691" binding="myAdditionalJarContentsPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="5" column="0" row-span="1" col-span="3" vsize-policy="7" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="6" column="0" row-span="1" col-span="5" vsize-policy="7" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<clientProperties>
<BorderFactoryClass class="java.lang.String" value="com.intellij.ui.IdeBorderFactory$PlainSmallWithoutIndent"/>
</clientProperties>
<border type="etched" title="Additional Contents"/>
<children/>
</grid>
<grid id="c02f5" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="6" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="7" column="0" row-span="1" col-span="5" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
Expand All @@ -106,10 +103,18 @@
</grid>
<component id="33cce" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="myJarOutputPathChooser">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="59fb" class="javax.swing.JCheckBox" binding="myExtractMetaInfOSGIInfoToTargetClassesCheckBox">
<constraints>
<grid row="5" column="0" row-span="1" col-span="5" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Extract META-INF and OSGI-INF to module's output path"/>
</properties>
</component>
</children>
</grid>
<buttonGroups>
Expand Down
3 changes: 3 additions & 0 deletions osmorc/src/org/osmorc/facet/ui/OsmorcFacetJAREditorTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class OsmorcFacetJAREditorTab extends FacetEditorTab {
private JRadioButton myPlaceInThisPathRadioButton;
private TextFieldWithBrowseButton myJarOutputPathChooser;
private JPanel myAdditionalJarContentsPanel;
private JCheckBox myExtractMetaInfOSGIInfoToTargetClassesCheckBox;

private final FacetEditorContext myEditorContext;
private final FacetValidatorsManager myValidatorsManager;
Expand Down Expand Up @@ -337,6 +338,7 @@ public void apply() throws ConfigurationException {
}
configuration.setIgnoreFilePattern(myIgnoreFilePatternTextField.getText());
configuration.setAlwaysRebuildBundleJAR(myAlwaysRebuildBundleJARCheckBox.isSelected());
configuration.setExtractMetaInfOsgIInfToTargetClasses(myExtractMetaInfOSGIInfoToTargetClassesCheckBox.isSelected());
configuration.setAdditionalJARContents(myAdditionalJARContentsTableModel.getAdditionalContents());
myModified = false;
}
Expand All @@ -361,6 +363,7 @@ public void reset() {
myAdditionalJARContentsTableModel.replaceContent(configuration.getAdditionalJARContents());
myIgnoreFilePatternTextField.setText(configuration.getIgnoreFilePattern());
myAlwaysRebuildBundleJARCheckBox.setSelected(configuration.isAlwaysRebuildBundleJAR());
myExtractMetaInfOSGIInfoToTargetClassesCheckBox.setSelected(configuration.isExtractMetaInfOsgIInfToTargetClasses());

updateGui();
myModified = false;
Expand Down