16
16
17
17
import java .io .File ;
18
18
import java .io .FileOutputStream ;
19
+ import java .io .IOException ;
19
20
import java .util .ArrayList ;
20
21
import java .util .Collections ;
21
22
import java .util .HashSet ;
67
68
public final class ProjectCommand {
68
69
69
70
private static class MainClassInfo {
70
-
71
71
public String name ;
72
-
73
72
public String path ;
74
73
75
74
public MainClassInfo (String name , String path ) {
@@ -78,6 +77,12 @@ public MainClassInfo(String name, String path) {
78
77
}
79
78
}
80
79
80
+ private static class Classpath {
81
+ public String source ;
82
+ public String destination ;
83
+ public boolean isArtifact ;
84
+ }
85
+
81
86
private static class ExportResult {
82
87
public boolean result ;
83
88
public String message ;
@@ -150,50 +155,36 @@ public static ExportResult exportJar(List<Object> arguments, IProgressMonitor mo
150
155
if (arguments .size () < 3 ) {
151
156
return new ExportResult (false , "Invalid export Arguments" );
152
157
}
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 );
155
160
String destination = gson .fromJson (gson .toJson (arguments .get (2 )), String .class );
156
161
Manifest manifest = new Manifest ();
157
162
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 );
160
165
}
161
166
try (JarOutputStream target = new JarOutputStream (new FileOutputStream (destination ), manifest )) {
162
167
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
171
178
}
172
179
}
173
180
}
174
- } catch (Exception e ) {
181
+ } catch (IOException e ) {
175
182
return new ExportResult (false , e .getMessage ());
176
183
}
177
184
return new ExportResult (true );
178
185
}
179
186
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 {
197
188
List <PackageNode > projectList = listProjects (arguments , monitor );
198
189
final List <MainClassInfo > res = new ArrayList <>();
199
190
List <IJavaElement > searchRoots = new ArrayList <>();
0 commit comments