Skip to content

Commit a1d66be

Browse files
authored
feat: Support custom task to export jar (#350)
1 parent 6ac6bef commit a1d66be

18 files changed

+582
-138
lines changed

jdtls.ext/com.microsoft.jdtls.ext.core/plugin.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<command id="java.project.list"/>
88
<command id="java.getPackageData"/>
99
<command id="java.resolvePath" />
10-
<command id="java.project.getMainMethod" />
10+
<command id="java.project.getMainClasses" />
1111
<command id="java.project.generateJar" />
1212
</delegateCommandHandler>
1313
</extension>

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/CommandHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
3131
return PackageCommand.getChildren(arguments, monitor);
3232
case "java.resolvePath":
3333
return PackageCommand.resolvePath(arguments, monitor);
34-
case "java.project.getMainMethod":
35-
return ProjectCommand.getMainMethod(arguments, monitor);
34+
case "java.project.getMainClasses":
35+
return ProjectCommand.getMainClasses(arguments, monitor);
3636
case "java.project.generateJar":
3737
return ProjectCommand.exportJar(arguments, monitor);
3838
default:

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/ProjectCommand.java

+23-32
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.File;
1818
import java.io.FileOutputStream;
19+
import java.io.IOException;
1920
import java.util.ArrayList;
2021
import java.util.Collections;
2122
import java.util.HashSet;
@@ -67,9 +68,7 @@
6768
public final class ProjectCommand {
6869

6970
private static class MainClassInfo {
70-
7171
public String name;
72-
7372
public String path;
7473

7574
public MainClassInfo(String name, String path) {
@@ -78,6 +77,12 @@ public MainClassInfo(String name, String path) {
7877
}
7978
}
8079

80+
private static class Classpath {
81+
public String source;
82+
public String destination;
83+
public boolean isArtifact;
84+
}
85+
8186
private static class ExportResult {
8287
public boolean result;
8388
public String message;
@@ -150,50 +155,36 @@ public static ExportResult exportJar(List<Object> arguments, IProgressMonitor mo
150155
if (arguments.size() < 3) {
151156
return new ExportResult(false, "Invalid export Arguments");
152157
}
153-
String mainMethod = gson.fromJson(gson.toJson(arguments.get(0)), String.class);
154-
String[] classpaths = gson.fromJson(gson.toJson(arguments.get(1)), String[].class);
158+
String mainClass = gson.fromJson(gson.toJson(arguments.get(0)), String.class);
159+
Classpath[] classpaths = gson.fromJson(gson.toJson(arguments.get(1)), Classpath[].class);
155160
String destination = gson.fromJson(gson.toJson(arguments.get(2)), String.class);
156161
Manifest manifest = new Manifest();
157162
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
158-
if (mainMethod.length() > 0) {
159-
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainMethod);
163+
if (mainClass.length() > 0) {
164+
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass);
160165
}
161166
try (JarOutputStream target = new JarOutputStream(new FileOutputStream(destination), manifest)) {
162167
Set<String> directories = new HashSet<>();
163-
for (String classpath : classpaths) {
164-
if (classpath != null) {
165-
if (classpath.endsWith(".jar")) {
166-
ZipFile zip = new ZipFile(classpath);
167-
writeArchive(zip, true, true, target, directories, monitor);
168-
} else {
169-
File folder = new File(classpath);
170-
writeFileRecursively(folder, target, directories, folder.getAbsolutePath().length() + 1);
168+
for (Classpath classpath : classpaths) {
169+
if (classpath.isArtifact) {
170+
writeArchive(new ZipFile(classpath.source), /* areDirectoryEntriesIncluded = */true,
171+
/* isCompressed = */true, target, directories, monitor);
172+
} else {
173+
try {
174+
writeFile(new File(classpath.source), new Path(classpath.destination), /* areDirectoryEntriesIncluded = */true,
175+
/* isCompressed = */true, target, directories);
176+
} catch (CoreException e) {
177+
// TODO: Collect reports
171178
}
172179
}
173180
}
174-
} catch (Exception e) {
181+
} catch (IOException e) {
175182
return new ExportResult(false, e.getMessage());
176183
}
177184
return new ExportResult(true);
178185
}
179186

180-
private static void writeFileRecursively(File folder, JarOutputStream jarOutputStream, Set<String> directories,
181-
int len) {
182-
File[] files = folder.listFiles();
183-
for (File file : files) {
184-
if (file.isDirectory()) {
185-
writeFileRecursively(file, jarOutputStream, directories, len);
186-
} else if (file.isFile()) {
187-
try {
188-
writeFile(file, new Path(file.getAbsolutePath().substring(len)), true, true, jarOutputStream, directories);
189-
} catch (Exception e) {
190-
// do nothing
191-
}
192-
}
193-
}
194-
}
195-
196-
public static List<MainClassInfo> getMainMethod(List<Object> arguments, IProgressMonitor monitor) throws Exception {
187+
public static List<MainClassInfo> getMainClasses(List<Object> arguments, IProgressMonitor monitor) throws Exception {
197188
List<PackageNode> projectList = listProjects(arguments, monitor);
198189
final List<MainClassInfo> res = new ArrayList<>();
199190
List<IJavaElement> searchRoots = new ArrayList<>();

package-lock.json

+119-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)