protected void saveDependencyChanges( Collection<Bundle> addedBundles, Collection<Bundle> removedBundles, Collection<Dependency> addedDependencys, Collection<Dependency> removedDependencys, ObjectFactory objectFactory) throws Exception { File addedFile = new File(filteredDependencyCache.getParentFile(), "dependencies.added.xml"); Features added = toFeatures(addedBundles, addedDependencys, objectFactory); writeDependencies(added, addedFile); File removedFile = new File(filteredDependencyCache.getParentFile(), "dependencies.removed.xml"); Features removed = toFeatures(removedBundles, removedDependencys, objectFactory); writeDependencies(removed, removedFile); StringWriter out = new StringWriter(); out.write(saveTreeListing()); out.write("Dependencies have changed:\n"); if (!addedBundles.isEmpty() || !addedDependencys.isEmpty()) { out.write("\tAdded dependencies are saved here: " + addedFile.getAbsolutePath() + "\n"); if (logDependencyChanges) { JaxbUtil.marshal(added, out); } } if (!removedBundles.isEmpty() || !removedDependencys.isEmpty()) { out.write("\tRemoved dependencies are saved here: " + removedFile.getAbsolutePath() + "\n"); if (logDependencyChanges) { JaxbUtil.marshal(removed, out); } } out.write( "Delete " + dependencyCache.getAbsolutePath() + " if you are happy with the dependency changes."); if (failOnDependencyChange) { throw new MojoFailureException(out.toString()); } else { getLog().warn(out.toString()); } }
private void writeDependencies(Features features, File file) throws JAXBException, IOException { file.getParentFile().mkdirs(); if (!file.getParentFile().exists() || !file.getParentFile().isDirectory()) { throw new IOException("Cannot create directory at " + file.getParent()); } FileOutputStream out = new FileOutputStream(file); try { JaxbUtil.marshal(features, out); } finally { out.close(); } }
/* * Write all project dependencies as feature */ private void writeFeatures(PrintStream out) throws ArtifactResolutionException, ArtifactNotFoundException, IOException, JAXBException, SAXException, ParserConfigurationException, XMLStreamException, MojoExecutionException { getLog().info("Generating feature descriptor file " + outputFile.getAbsolutePath()); // read in an existing feature.xml ObjectFactory objectFactory = new ObjectFactory(); Features features; if (inputFile.exists()) { filter(inputFile, filteredInputFile); features = readFeaturesFile(filteredInputFile); } else { features = objectFactory.createFeaturesRoot(); } if (features.getName() == null) { features.setName(project.getArtifactId()); } Feature feature = null; for (Feature test : features.getFeature()) { if (test.getName().equals(project.getArtifactId())) { feature = test; } } if (feature == null) { feature = objectFactory.createFeature(); feature.setName(project.getArtifactId()); } if (!feature.hasVersion()) { feature.setVersion(project.getArtifact().getBaseVersion()); } if (feature.getDescription() == null) { feature.setDescription(project.getName()); } if (resolver != null) { feature.setResolver(resolver); } if (installMode != null) { feature.setInstall(installMode); } if (project.getDescription() != null && feature.getDetails() == null) { feature.setDetails(project.getDescription()); } if (includeProjectArtifact) { Bundle bundle = objectFactory.createBundle(); bundle.setLocation(this.dependencyHelper.artifactToMvn(project.getArtifact())); if (startLevel != null) { bundle.setStartLevel(startLevel); } feature.getBundle().add(bundle); } for (Map.Entry<?, String> entry : localDependencies.entrySet()) { Object artifact = entry.getKey(); if (excludedArtifactIds.contains(this.dependencyHelper.getArtifactId(artifact))) { continue; } if (this.dependencyHelper.isArtifactAFeature(artifact)) { if (aggregateFeatures && FEATURE_CLASSIFIER.equals(this.dependencyHelper.getClassifier(artifact))) { File featuresFile = this.dependencyHelper.resolve(artifact, getLog()); if (featuresFile == null || !featuresFile.exists()) { throw new MojoExecutionException( "Cannot locate file for feature: " + artifact + " at " + featuresFile); } Features includedFeatures = readFeaturesFile(featuresFile); // TODO check for duplicates? features.getFeature().addAll(includedFeatures.getFeature()); } } else if (addBundlesToPrimaryFeature) { String bundleName = this.dependencyHelper.artifactToMvn(artifact); File bundleFile = this.dependencyHelper.resolve(artifact, getLog()); Manifest manifest = getManifest(bundleFile); if (manifest == null || !ManifestUtils.isBundle(getManifest(bundleFile))) { bundleName = "wrap:" + bundleName; } Bundle bundle = null; for (Bundle b : feature.getBundle()) { if (bundleName.equals(b.getLocation())) { bundle = b; break; } } if (bundle == null) { bundle = objectFactory.createBundle(); bundle.setLocation(bundleName); if (!"provided".equals(entry.getValue()) || !ignoreScopeProvided) { feature.getBundle().add(bundle); } } if ("runtime".equals(entry.getValue())) { bundle.setDependency(true); } if (startLevel != null && bundle.getStartLevel() == 0) { bundle.setStartLevel(startLevel); } } } if ((!feature.getBundle().isEmpty() || !feature.getFeature().isEmpty()) && !features.getFeature().contains(feature)) { features.getFeature().add(feature); } JaxbUtil.marshal(features, out); try { checkChanges(features, objectFactory); } catch (Exception e) { throw new MojoExecutionException("Features contents have changed", e); } getLog().info("...done!"); }
private Features readFeaturesFile(File featuresFile) throws XMLStreamException, JAXBException, IOException { return JaxbUtil.unmarshal(featuresFile.toURI().toASCIIString(), false); }
/** * Runs schema validation. * * @param uri Uri to validate. * @throws Exception When validation fails. */ public static void validate(URI uri) throws Exception { JaxbUtil.unmarshal(uri.toASCIIString(), true); }