1616
1717import java .io .File ;
1818import java .io .FileOutputStream ;
19+ import java .io .IOException ;
1920import java .util .ArrayList ;
2021import java .util .Collections ;
2122import java .util .HashSet ;
6768public 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 <>();
0 commit comments