2929import java .util .zip .ZipFile ;
3030
3131import org .apache .commons .io .FilenameUtils ;
32+ import org .apache .commons .lang3 .StringUtils ;
3233import org .eclipse .core .resources .IFile ;
3334import org .eclipse .core .resources .IProject ;
3435import org .eclipse .core .resources .IWorkspaceRoot ;
3536import org .eclipse .core .resources .ResourcesPlugin ;
3637import org .eclipse .core .runtime .CoreException ;
3738import org .eclipse .core .runtime .IPath ;
3839import org .eclipse .core .runtime .IProgressMonitor ;
40+ import org .eclipse .core .runtime .IStatus ;
41+ import org .eclipse .core .runtime .MultiStatus ;
3942import org .eclipse .core .runtime .NullProgressMonitor ;
4043import org .eclipse .core .runtime .Path ;
4144import org .eclipse .jdt .core .IJavaElement ;
6770
6871public final class ProjectCommand {
6972
73+ private static String COMMAND_EXPORT_JAR_REPORT = "java.view.package.exportJarReport" ;
74+
7075 private static class MainClassInfo {
7176 public String name ;
7277 public String path ;
@@ -83,23 +88,6 @@ private static class Classpath {
8388 public boolean isArtifact ;
8489 }
8590
86- private static class ExportResult {
87- public boolean result ;
88- public String message ;
89- public String log ;
90-
91- ExportResult (boolean result ) {
92- this .result = result ;
93- this .log = "" ;
94- }
95-
96- ExportResult (boolean result , String message ) {
97- this .result = result ;
98- this .message = message ;
99- this .log = "" ;
100- }
101- }
102-
10391 private static final Gson gson = new GsonBuilder ().registerTypeAdapterFactory (new CollectionTypeAdapter .Factory ())
10492 .registerTypeAdapterFactory (new EnumTypeAdapter .Factory ()).create ();
10593
@@ -151,16 +139,14 @@ private static IWorkspaceRoot getWorkspaceRoot() {
151139 return ResourcesPlugin .getWorkspace ().getRoot ();
152140 }
153141
154- public static ExportResult exportJar (List <Object > arguments , IProgressMonitor monitor ) {
155- if (arguments .size () < 3 ) {
156- return new ExportResult (false , "Invalid export Arguments" );
157- }
158- if (monitor .isCanceled ()) {
159- return new ExportResult (false , "User cancelled" );
142+ public static boolean exportJar (List <Object > arguments , IProgressMonitor monitor ) {
143+ if (arguments .size () < 4 ) {
144+ return false ;
160145 }
161146 String mainClass = gson .fromJson (gson .toJson (arguments .get (0 )), String .class );
162147 Classpath [] classpaths = gson .fromJson (gson .toJson (arguments .get (1 )), Classpath [].class );
163148 String destination = gson .fromJson (gson .toJson (arguments .get (2 )), String .class );
149+ String taskLabel = gson .fromJson (gson .toJson (arguments .get (3 )), String .class );
164150 Manifest manifest = new Manifest ();
165151 manifest .getMainAttributes ().put (Attributes .Name .MANIFEST_VERSION , "1.0" );
166152 if (mainClass .length () > 0 ) {
@@ -170,24 +156,39 @@ public static ExportResult exportJar(List<Object> arguments, IProgressMonitor mo
170156 Set <String > directories = new HashSet <>();
171157 for (Classpath classpath : classpaths ) {
172158 if (monitor .isCanceled ()) {
173- return new ExportResult ( false , "User cancelled" ) ;
159+ return false ;
174160 }
175161 if (classpath .isArtifact ) {
176- writeArchive (new ZipFile (classpath .source ), /* areDirectoryEntriesIncluded = */ true ,
177- /* isCompressed = */ true , target , directories , monitor );
162+ MultiStatus resultStatus = writeArchive (new ZipFile (classpath .source ),
163+ /* areDirectoryEntriesIncluded = */ true , /* isCompressed = */ true , target , directories , monitor );
164+ int severity = resultStatus .getSeverity ();
165+ if (severity == IStatus .OK ) {
166+ java .nio .file .Path path = java .nio .file .Paths .get (classpath .source );
167+ reportExportJarMessage (taskLabel , IStatus .OK , "Successfully added the file to the exported jar: " + path .getFileName ().toString ());
168+ continue ;
169+ }
170+ if (resultStatus .isMultiStatus ()) {
171+ for (IStatus childStatus : resultStatus .getChildren ()) {
172+ reportExportJarMessage (taskLabel , severity , childStatus .getMessage ());
173+ }
174+ } else {
175+ reportExportJarMessage (taskLabel , severity , resultStatus .getMessage ());
176+ }
178177 } else {
179178 try {
180179 writeFile (new File (classpath .source ), new Path (classpath .destination ), /* areDirectoryEntriesIncluded = */ true ,
181180 /* isCompressed = */ true , target , directories );
181+ reportExportJarMessage (taskLabel , IStatus .OK , "Successfully added the file to the exported jar: " + classpath .destination );
182182 } catch (CoreException e ) {
183- // TODO: Collect reports
183+ reportExportJarMessage ( taskLabel , IStatus . ERROR , e . getMessage ());
184184 }
185185 }
186186 }
187187 } catch (IOException e ) {
188- return new ExportResult (false , e .getMessage ());
188+ reportExportJarMessage (taskLabel , IStatus .ERROR , e .getMessage ());
189+ return false ;
189190 }
190- return new ExportResult ( true ) ;
191+ return true ;
191192 }
192193
193194 public static List <MainClassInfo > getMainClasses (List <Object > arguments , IProgressMonitor monitor ) throws Exception {
@@ -253,4 +254,28 @@ public static String getModuleName(IJavaProject project) {
253254 }
254255 }
255256
257+ private static void reportExportJarMessage (String taskLabel , int severity , String message ) {
258+ if (StringUtils .isNotBlank (message ) && StringUtils .isNotBlank (taskLabel )) {
259+ String readableSeverity = getSeverityString (severity );
260+ JavaLanguageServerPlugin .getInstance ().getClientConnection ().executeClientCommand (COMMAND_EXPORT_JAR_REPORT ,
261+ taskLabel , "[" + readableSeverity + "] " + message );
262+ }
263+ }
264+
265+ private static String getSeverityString (int severity ) {
266+ switch (severity ) {
267+ case IStatus .INFO :
268+ return "INFO" ;
269+ case IStatus .WARNING :
270+ return "WARNING" ;
271+ case IStatus .ERROR :
272+ return "ERROR" ;
273+ case IStatus .CANCEL :
274+ return "CANCEL" ;
275+ case IStatus .OK :
276+ return "OK" ;
277+ default :
278+ return "UNKNOWN STATUS" ;
279+ }
280+ }
256281}
0 commit comments