Skip to content

Commit d0e20d0

Browse files
authored
Support passing custom environment, OSGIfy jar,generate source Jar.
1 parent d273778 commit d0e20d0

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

build.gradle

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ task testMatlabAll(dependsOn: [
5454
testMatlabInteractive
5555
])
5656

57+
//////////
58+
// OSGI //
59+
//////////
60+
61+
// delete the old manifest to ensure there's no caching or merging going on
62+
jar.doFirst {
63+
project.file('META-INF/MANIFEST.MF').delete()
64+
}
65+
66+
project.version = project.hasProperty('ver') ? project['ver'] : 'none'
67+
jar.manifest {
68+
attributes (
69+
'Bundle-Description': "${project.description}",
70+
'Bundle-DocURL': "https://${project.git_url}",
71+
'Bundle-License': "https://${project.git_url}/blob/v${version}/LICENSE",
72+
'Bundle-ManifestVersion': "${osgi_manifest_ver}",
73+
'Bundle-SymbolicName': "${project.group}.${project.name}",
74+
'Bundle-Vendor': "${project.org}",
75+
'Bundle-Version': "${project.version}",
76+
'Export-Package': "matlabcontrol;version=\"${project.version}\",matlabcontrol.demo;version=\"${project.version}\",matlabcontrol.extensions;uses:=matlabcontrol;version=\"${project.version}\",matlabcontrol.internal;version=\"${project.version}\",matlabcontrol.link;uses:=matlabcontrol;version=\"${project.version}\"",
77+
'Main-Class': 'matlabcontrol.demo.DemoMain',
78+
'Require-Capability': 'osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"'
79+
)
80+
}
81+
5782
////////////
5883
// JAVA 6 //
5984
////////////
@@ -111,9 +136,23 @@ def JMI_STUB = 'com/mathworks/jmi/**'
111136

112137
// make a source jar
113138
task sourcesJar(type: Jar) {
114-
classifier = 'sources'
139+
archiveClassifier = 'sources'
115140
from sourceSets.main.allJava
116141
exclude(JMI_STUB)
142+
manifest {
143+
attributes (
144+
'Bundle-Description': "${project.description}",
145+
'Bundle-DocURL': "https://${project.git_url}",
146+
'Bundle-License': "https://${project.git_url}/blob/v${version}/LICENSE",
147+
'Bundle-ManifestVersion': "${osgi_manifest_ver}",
148+
'Bundle-SymbolicName': "${project.group}.${project.name}",
149+
'Bundle-Vendor': "${project.org}",
150+
'Bundle-Version': "${project.version}",
151+
'Export-Package': "matlabcontrol;version=\"${project.version}\",matlabcontrol.demo;version=\"${project.version}\",matlabcontrol.extensions;uses:=matlabcontrol;version=\"${project.version}\",matlabcontrol.internal;version=\"${project.version}\",matlabcontrol.link;uses:=matlabcontrol;version=\"${project.version}\"",
152+
'Main-Class': 'matlabcontrol.demo.DemoMain',
153+
'Require-Capability': 'osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"'
154+
)
155+
}
117156
}
118157

119158
// Where it's possible to name parameters and methods clearly enough
@@ -131,10 +170,14 @@ javadoc {
131170
}
132171

133172
task javadocJar(type: Jar, dependsOn: javadoc) {
134-
classifier = 'javadoc'
173+
archiveClassifier = 'javadoc'
135174
from javadoc.destinationDir
136175
}
137176

177+
artifacts {
178+
archives sourcesJar
179+
}
180+
138181
// it all needs to get published and formatted
139182
apply from: 干.file('base/maven.gradle')
140183
apply from: 干.file('base/sonatype.gradle')

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ group=com.diffplug.matsim
55
description=MatConsoleCtl - control MATLAB from Java
66
org=diffplug
77

8+
osgi_manifest_ver=2
9+
810
git_url=github.com/diffplug/matconsolectl
911
maven_group=com.diffplug.matsim
1012
maven_name=matconsolectl

src/main/java/matlabcontrol/MatlabProxyFactoryOptions.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.concurrent.atomic.AtomicLong;
1111

1212
import matlabcontrol.MatlabProxyFactory.CopyPasteCallback;
13+
import java.util.Map;
1314

1415
/**
1516
* Options that configure how a factory operates. Use a {@link Builder} to create an instance of this class.
@@ -35,6 +36,7 @@ public class MatlabProxyFactoryOptions {
3536
private final int _port;
3637
private final Writer _outputWriter;
3738
private final Writer _errorWriter;
39+
private final Map<String, String> _matlabEnvironment;
3840

3941
private MatlabProxyFactoryOptions(Builder options) {
4042
_matlabLocation = options._matlabLocation;
@@ -51,6 +53,7 @@ private MatlabProxyFactoryOptions(Builder options) {
5153
_port = options._port;
5254
_outputWriter = options._outputWriter;
5355
_errorWriter = options._errorWriter;
56+
_matlabEnvironment = options._matlabEnvironment;
5457
}
5558

5659
String getMatlabLocation() {
@@ -109,6 +112,10 @@ Writer getErrorWriter() {
109112
return _errorWriter;
110113
}
111114

115+
Map<String, String> getMatlabEnvironment() {
116+
return _matlabEnvironment;
117+
}
118+
112119
/**
113120
* Creates instances of {@link MatlabProxyFactoryOptions}. Any and all of these properties may be left unset, if so
114121
* then a default will be used. Depending on how the factory operates, not all properties may be used. Currently all
@@ -142,6 +149,7 @@ public static class Builder {
142149
private volatile int _port = 2100;
143150
private volatile Writer _outputWriter = null;
144151
private volatile Writer _errorWriter = null;
152+
private volatile Map<String, String> _matlabEnvironment = null;
145153

146154
//Assigning to a long is not atomic, so use an AtomicLong so that a thread always sees an intended value
147155
private final AtomicLong _proxyTimeout = new AtomicLong(180000L);
@@ -426,6 +434,18 @@ public final Builder setErrorWriter(Writer errorWriter) {
426434
return this;
427435
}
428436

437+
/**
438+
* Sets the matlab environment as a key-value pair mapping.
439+
* This map is appended to the environment map of the spawned matlab proxy instance.
440+
*
441+
* @param matlabEnvironment
442+
*/
443+
public final Builder setMatlabEnvironment(Map<String, String> matlabEnvironment) {
444+
_matlabEnvironment = matlabEnvironment;
445+
446+
return this;
447+
}
448+
429449
/**
430450
* Builds a {@code MatlabProxyFactoryOptions} instance.
431451
*

src/main/java/matlabcontrol/RemoteMatlabProxyFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.UUID;
2525
import java.util.concurrent.Callable;
2626
import java.util.concurrent.CopyOnWriteArrayList;
27+
import java.util.Map;
2728

2829
import matlabcontrol.MatlabProxy.Identifier;
2930
import matlabcontrol.MatlabProxyFactory.Request;
@@ -268,6 +269,10 @@ private Process createProcess(RemoteRequestReceiver receiver) throws MatlabConne
268269
//Create process
269270
ProcessBuilder builder = new ProcessBuilder(processArguments);
270271
builder.directory(_options.getStartingDirectory());
272+
Map<String, String> env = builder.environment();
273+
if (_options.getMatlabEnvironment() != null && !_options.getMatlabEnvironment().isEmpty()) {
274+
env.putAll(_options.getMatlabEnvironment());
275+
}
271276

272277
try {
273278
Process process = builder.start();

0 commit comments

Comments
 (0)