public List<IResource> exportArtifact(IProject project) throws Exception { List<IResource> exportResources = new ArrayList<IResource>(); List<String> exportedPackageList = new ArrayList<String>(); List<String> importededPackageList = new ArrayList<String>(); ArchiveManipulator archiveManipulator = new ArchiveManipulator(); NullProgressMonitor nullProgressMonitor = new NullProgressMonitor(); BundlesDataInfo bundleData = new BundlesDataInfo(); IFile bundleDataFile = project.getFile("bundles-data.xml"); File tempProject = createTempProject(); File libResources = createTempDir(tempProject, "lib_resources"); MavenProject mavenProject = MavenUtils.getMavenProject(project.getFile("pom.xml").getLocation().toFile()); if (bundleDataFile.exists()) { bundleData.deserialize(bundleDataFile); for (String lib : bundleData.getExportedPackageListsFromJar().keySet()) { IFile JarFile = project.getFile(lib); if (JarFile.exists()) { archiveManipulator.extract(JarFile.getLocation().toFile(), libResources); exportedPackageList.addAll(bundleData.getExportedPackageListsFromJar().get(lib)); } } for (IProject lib : bundleData.getExportedPackageListsFromProject().keySet()) { if (lib.isOpen()) { lib.build(IncrementalProjectBuilder.FULL_BUILD, nullProgressMonitor); IJavaProject javaLibProject = JavaCore.create(lib); for (IPackageFragment pkg : javaLibProject.getPackageFragments()) { if (pkg.getKind() == IPackageFragmentRoot.K_SOURCE) { if (pkg.hasChildren()) { exportedPackageList.add(pkg.getElementName()); } } } IPath outPutPath = getOutputPath(javaLibProject); // get resource location IPath resources = getResourcePath(lib); FileUtils.copyDirectoryContents(outPutPath.toFile(), libResources); if (resources.toFile().exists()) { FileUtils.copyDirectoryContents(resources.toFile(), libResources); } } } getPackages(exportedPackageList, mavenProject, "Export-Package"); getPackages(importededPackageList, mavenProject, "Import-Package"); BundleManifest manifest = new BundleManifest(); manifest.setBundleName(project.getName()); manifest.setBundleSymbolicName(project.getName()); if (null != mavenProject.getModel().getDescription() && !"".equals(mavenProject.getModel().getDescription())) { manifest.setBundleDescription(mavenProject.getModel().getDescription()); } else { manifest.setBundleDescription(project.getName()); } if (null != mavenProject.getModel().getVersion() && !"".equals(mavenProject.getDescription())) { manifest.setBundleVersion(mavenProject.getModel().getVersion()); } else { manifest.setBundleVersion("1.0.0"); } if (null != bundleData.getFragmentHost() && !"".equals(bundleData.getFragmentHost())) { manifest.setFragmentHost(bundleData.getFragmentHost()); } manifest.setExportPackagesList(exportedPackageList); if (importededPackageList.size() > 0) { manifest.setImportPackagesList(importededPackageList); manifest.setDynamicImports(false); } File metaInfDir = new File(libResources, "META-INF"); if (!metaInfDir.exists()) metaInfDir.mkdir(); File manifestFile = new File(metaInfDir, "MANIFEST.MF"); FileUtils.createFile(manifestFile, manifest.toString()); File tmpArchive = new File(tempProject, project.getName().concat(".jar")); archiveManipulator.archiveDir(tmpArchive.toString(), libResources.toString()); IFile libArchive = getTargetArchive(project, "jar"); FileUtils.copy(tmpArchive, libArchive.getLocation().toFile()); exportResources.add((IResource) libArchive); // cleaning temp project // if(!org.apache.commons.io.FileUtils.deleteQuietly(tempProject.getLocation().toFile())){ // tempProject.delete(true, getProgressMonitor()); } TempFileUtils.cleanUp(); return exportResources; }
public List<IResource> exportArtifact(IProject project) { List<IResource> exportResources = new ArrayList<IResource>(); if (!project.isOpen()) { return exportResources; } try { ArchiveManipulator archiveManipulator = new ArchiveManipulator(); NullProgressMonitor nullProgressMonitor = new NullProgressMonitor(); // cleaning target directory clearTarget(project); project.build(IncrementalProjectBuilder.FULL_BUILD, nullProgressMonitor); IJavaProject javaProject = JavaCore.create(project); List<String> exportPac = getExportPackages(javaProject); BundleManifest manifest = new BundleManifest(); manifest.setBundleName(project.getName()); manifest.setBundleSymbolicName(project.getName()); manifest.setBundleDescription(project.getName()); manifest.setBundleVersion("2.0"); manifest.setExportPackagesList(exportPac); // IPath outPutPath = javaProject.getOutputLocation(); IPath outPutPath = ResourcesPlugin.getWorkspace() .getRoot() .getFolder(javaProject.getOutputLocation()) .getLocation(); // Let's create a temp project IProject tempProject = ResourcesPlugin.getWorkspace().getRoot().getProject(".temp" + System.currentTimeMillis()); tempProject.create(nullProgressMonitor); tempProject.open(nullProgressMonitor); tempProject.setHidden(true); org.eclipse.osgi.storagemanager.StorageManager manager = new StorageManager(tempProject.getLocation().toFile(), "false"); File validatorResource = manager.createTempFile("validator_resources"); validatorResource.delete(); validatorResource.mkdir(); FileUtils.copyDirectoryContents(outPutPath.toFile(), validatorResource); // copy binaries /////////////////////// Create the Bundle********************************* File metainfPath = new File(validatorResource, "META-INF"); metainfPath.mkdir(); File manifestFile = new File(metainfPath, "MANIFEST.MF"); FileUtils.createFile(manifestFile, manifest.toString()); File tmpArchive = new File(tempProject.getLocation().toFile(), project.getName().concat(".jar")); archiveManipulator.archiveDir(tmpArchive.toString(), validatorResource.toString()); IFolder binaries = project.getFolder("target"); if (!binaries.exists()) { binaries.create(true, true, nullProgressMonitor); binaries.setHidden(true); } IFile serviceArchive = project.getFile("target" + File.separator + project.getName().concat(".jar")); FileUtils.copy(tmpArchive, serviceArchive.getLocation().toFile()); exportResources.add((IResource) serviceArchive); // cleaning temp project tempProject.delete(true, nullProgressMonitor); } catch (Exception e) { e.printStackTrace(); } return exportResources; }