Skip to content

Support exporting jar with custom task #350

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

Merged
merged 19 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -35,6 +35,7 @@
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
Expand Down Expand Up @@ -78,6 +79,12 @@ public MainClassInfo(String name, String path) {
}
}

private static class ClassPath {
public String source;
public String destination;
public boolean isDependency;
}

private static class exportResult {
public boolean result;
public String message;
Expand Down Expand Up @@ -151,7 +158,7 @@ public static exportResult exportJar(List<Object> arguments, IProgressMonitor mo
return new exportResult(false, "Invalid export Arguments");
}
String mainMethod = gson.fromJson(gson.toJson(arguments.get(0)), String.class);
String[] classpaths = gson.fromJson(gson.toJson(arguments.get(1)), String[].class);
ClassPath[] classpaths = gson.fromJson(gson.toJson(arguments.get(1)), ClassPath[].class);
String destination = gson.fromJson(gson.toJson(arguments.get(2)), String.class);
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
Expand All @@ -160,15 +167,11 @@ public static exportResult exportJar(List<Object> arguments, IProgressMonitor mo
}
try (JarOutputStream target = new JarOutputStream(new FileOutputStream(destination), manifest)) {
Set<String> directories = new HashSet<>();
for (String classpath : classpaths) {
if (classpath != null) {
if (classpath.endsWith(".jar")) {
ZipFile zip = new ZipFile(classpath);
writeArchive(zip, true, true, target, directories, monitor);
} else {
File folder = new File(classpath);
writeFileRecursively(folder, target, directories, folder.getAbsolutePath().length() + 1);
}
for (ClassPath classpath : classpaths) {
if (classpath.isDependency) {
writeArchive(new ZipFile(classpath.source), true, true, target, directories, monitor);
} else {
writeFile(new File(classpath.source), new Path(classpath.destination), true, true, target, directories);
}
}
} catch (Exception e) {
Expand All @@ -177,22 +180,6 @@ public static exportResult exportJar(List<Object> arguments, IProgressMonitor mo
return new exportResult(true);
}

private static void writeFileRecursively(File folder, JarOutputStream jarOutputStream, Set<String> directories,
int len) {
File[] files = folder.listFiles();
for (File file : files) {
if (file.isDirectory()) {
writeFileRecursively(file, jarOutputStream, directories, len);
} else if (file.isFile()) {
try {
writeFile(file, new Path(file.getAbsolutePath().substring(len)), true, true, jarOutputStream, directories);
} catch (Exception e) {
// do nothing
}
}
}
}

public static List<MainClassInfo> getMainMethod(List<Object> arguments, IProgressMonitor monitor) throws Exception {
List<PackageNode> projectList = listProjects(arguments, monitor);
final List<MainClassInfo> res = new ArrayList<>();
Expand Down
133 changes: 119 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,28 @@
"contents": "You can create a new Java project by clicking the below button.\n[Create new Java project](command:java.project.create)",
"when": "!java:projectManagerActivated || java:serverMode != Standard"
}
],
"taskDefinitions": [
{
"type": "java",
"properties": {
"elements": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A reference about how to support intellisense for constant literals when configuring elements.

https://github.com/microsoft/vscode-java-debug/blob/master/package.json#L346-L373

constant

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@testforstephen beautiful!

"type": "array",
"items": {
"type": "string"
},
"description": "%taskDefinitions.java.project.exportJar.elements%"
},
"mainMethod": {
"type": "string",
"description": "%taskDefinitions.java.project.exportJar.mainMethod%"
},
"targetPath": {
"type": "string",
"description": "%taskDefinitions.java.project.exportJar.targetPath%"
}
}
}
]
},
"scripts": {
Expand Down Expand Up @@ -506,6 +528,7 @@
"webpack-cli": "^3.3.12"
},
"dependencies": {
"globby": "11.0.1",
"fs-extra": "^7.0.1",
"lodash": "^4.17.20",
"minimatch": "^3.0.4",
Expand Down
5 changes: 4 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
"configuration.java.dependency.autoRefresh": "Synchronize Java Projects explorer with changes",
"configuration.java.dependency.refreshDelay": "The delay time (ms) the auto refresh is invoked when changes are detected",
"configuration.java.dependency.packagePresentation": "Package presentation mode: flat or hierarchical",
"configuration.java.project.exportJar.targetPath": "The default output path of export jar"
"configuration.java.project.exportJar.targetPath": "The default output path of export jar",
"taskDefinitions.java.project.exportJar.elements": "The content list of export jar.",
"taskDefinitions.java.project.exportJar.mainMethod": "The main method in the manifest of exported jar.",
"taskDefinitions.java.project.exportJar.targetPath": "The output path of the exported jar. This will override the setting `configuration.java.project.exportJar.targetPath`."
}
Loading