public static File exportProjectToJarFile(IProject project, boolean logInfo) { JarPackageData jarExportOps = new JarPackageData(); jarExportOps.setExportJavaFiles(false); jarExportOps.setExportClassFiles(true); jarExportOps.setIncludeDirectoryEntries(true); jarExportOps.setUsesManifest(false); jarExportOps.setOverwrite(true); jarExportOps.setJarBuilder(new LambdaFunctionJarBuilder()); try { Object[] elements = getElementsToExport(project); jarExportOps.setElements(elements); // prefix should be at least three characters long File jarFile = File.createTempFile(project.getName() + "-lambda", ".zip"); jarFile.deleteOnExit(); jarExportOps.setJarLocation(new Path(jarFile.getAbsolutePath())); if (logInfo) { LambdaPlugin.getDefault() .logInfo( String.format( "Exporting project [%s] to %s", project.getName(), jarFile.getAbsolutePath())); } IJarExportRunnable runnable = jarExportOps.createJarExportRunnable(null); runnable.run(null); if (logInfo) { LambdaPlugin.getDefault().logInfo("Project exported to " + jarFile.getAbsolutePath()); } return jarFile; } catch (Exception e) { LambdaPlugin.getDefault() .reportException( String.format("Unable to export project [%s] to jar file", project.getName()), e); return null; } }
public ApplicationArchive getApplicationArchive(IProgressMonitor monitor) throws CoreException { if (!initialized) { // Seems like initialize() wasn't invoked prior to this call throw CloudErrorUtil.toCoreException( Messages.JavaCloudFoundryArchiver_ERROR_ARCHIVER_NOT_INITIALIZED); } ApplicationArchive archive = JavaWebApplicationDelegate.getArchiveFromManifest(appModule, cloudServer); if (archive == null) { File packagedFile = null; IJavaProject javaProject = CloudFoundryProjectUtil.getJavaProject(appModule); if (javaProject == null) { handleApplicationDeploymentFailure( Messages.JavaCloudFoundryArchiver_ERROR_NO_JAVA_PROJ_RESOLVED); } JavaPackageFragmentRootHandler rootResolver = getPackageFragmentRootHandler(javaProject, monitor); IType mainType = rootResolver.getMainType(monitor); final IPackageFragmentRoot[] roots = rootResolver.getPackageFragmentRoots(monitor); if (roots == null || roots.length == 0) { handleApplicationDeploymentFailure( Messages.JavaCloudFoundryArchiver_ERROR_NO_PACKAGE_FRAG_ROOTS); } JarPackageData jarPackageData = getJarPackageData(roots, mainType, monitor); boolean isBoot = CloudFoundryProjectUtil.isSpringBoot(appModule); // Search for existing MANIFEST.MF IFile metaFile = getManifest(roots, javaProject); // Only use existing manifest files for non-Spring boot, as Spring // boot repackager will // generate it own manifest file. if (!isBoot && metaFile != null) { // If it is not a boot project, use a standard library jar // builder jarPackageData.setJarBuilder(getDefaultLibJarBuilder()); jarPackageData.setManifestLocation(metaFile.getFullPath()); jarPackageData.setSaveManifest(false); jarPackageData.setGenerateManifest(false); // Check manifest accessibility through the jar package data // API // to verify the packaging won't fail if (!jarPackageData.isManifestAccessible()) { handleApplicationDeploymentFailure( NLS.bind( Messages.JavaCloudFoundryArchiver_ERROR_MANIFEST_NOT_ACCESSIBLE, metaFile.getLocation().toString())); } InputStream inputStream = null; try { inputStream = new FileInputStream(metaFile.getLocation().toFile()); Manifest manifest = new Manifest(inputStream); Attributes att = manifest.getMainAttributes(); if (att.getValue("Main-Class") == null) { // $NON-NLS-1$ handleApplicationDeploymentFailure( Messages.JavaCloudFoundryArchiver_ERROR_NO_MAIN_CLASS_IN_MANIFEST); } } catch (FileNotFoundException e) { handleApplicationDeploymentFailure( NLS.bind( Messages.JavaCloudFoundryArchiver_ERROR_FAILED_READ_MANIFEST, e.getLocalizedMessage())); } catch (IOException e) { handleApplicationDeploymentFailure( NLS.bind( Messages.JavaCloudFoundryArchiver_ERROR_FAILED_READ_MANIFEST, e.getLocalizedMessage())); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException io) { // Ignore } } } } else { // Otherwise generate a manifest file. Note that manifest files // are only generated in the temporary jar meant only for // deployment. // The associated Java project is no modified. jarPackageData.setGenerateManifest(true); // This ensures that folders in output folders appear at root // level // Example: src/main/resources, which is in the project's // classpath, contains non-Java templates folder and // has output folder target/classes. If not exporting output // folder, // templates will be packaged in the jar using this path: // resources/templates // This may cause problems with the application's dependencies // if they are looking for just /templates at top level of the // jar // If exporting output folders, templates folder will be // packaged at top level in the jar. jarPackageData.setExportOutputFolders(true); } try { packagedFile = packageApplication(jarPackageData, monitor); } catch (CoreException e) { handleApplicationDeploymentFailure( NLS.bind(Messages.JavaCloudFoundryArchiver_ERROR_JAVA_APP_PACKAGE, e.getMessage())); } if (packagedFile == null || !packagedFile.exists()) { handleApplicationDeploymentFailure( Messages.JavaCloudFoundryArchiver_ERROR_NO_PACKAGED_FILE_CREATED); } if (isBoot) { bootRepackage(roots, packagedFile); } // At this stage a packaged file should have been created or found try { archive = new CloudZipApplicationArchive(new ZipFile(packagedFile)); } catch (IOException ioe) { handleApplicationDeploymentFailure( NLS.bind(Messages.JavaCloudFoundryArchiver_ERROR_CREATE_CF_ARCHIVE, ioe.getMessage())); } } return archive; }