List of usage examples for org.apache.maven.artifact.repository.layout DefaultRepositoryLayout pathOf
public String pathOf(Artifact artifact)
From source file:org.apache.karaf.tooling.features.InstallKarsMojo.java
License:Apache License
@SuppressWarnings("deprecation") @Override/*from w w w . j av a2 s .c o m*/ public void execute() throws MojoExecutionException, MojoFailureException { this.dependencyHelper = DependencyHelperFactory.createDependencyHelper(this.container, this.project, this.mavenSession, getLog()); systemDirectory.mkdirs(); system = systemDirectory.toURI(); if (startupPropertiesFile.exists()) { try { InputStream in = new FileInputStream(startupPropertiesFile); try { startupProperties.load(in); } finally { in.close(); } } catch (IOException e) { throw new MojoFailureException( "Could not open existing startup.properties file at " + startupPropertiesFile, e); } } else { startupProperties .setHeader(Collections.singletonList("#Bundles to be started on startup, with startlevel")); if (!startupPropertiesFile.getParentFile().exists()) { startupPropertiesFile.getParentFile().mkdirs(); } } OfflineFeaturesService featuresService = new OfflineFeaturesService(); Collection<Artifact> dependencies = project.getDependencyArtifacts(); StringBuilder buf = new StringBuilder(); for (Artifact artifact : dependencies) { dontAddToStartup = "runtime".equals(artifact.getScope()); if ("kar".equals(artifact.getType()) && acceptScope(artifact)) { File file = artifact.getFile(); try { Kar kar = new Kar(file.toURI()); kar.extract(new File(system.getPath()), new File(workDirectory)); for (URI repoUri : kar.getFeatureRepos()) { featuresService.addRepository(repoUri); } } catch (Exception e) { throw new RuntimeException("Could not install kar: " + artifact.toString() + "\n", e); //buf.append("Could not install kar: ").append(artifact.toString()).append("\n"); //buf.append(e.getMessage()).append("\n\n"); } } if ("features".equals(artifact.getClassifier()) && acceptScope(artifact)) { String uri = this.dependencyHelper.artifactToMvn(artifact); File source = artifact.getFile(); DefaultRepositoryLayout layout = new DefaultRepositoryLayout(); //remove timestamp version artifact = factory.createArtifactWithClassifier(artifact.getGroupId(), artifact.getArtifactId(), artifact.getBaseVersion(), artifact.getType(), artifact.getClassifier()); File target = new File(system.resolve(layout.pathOf(artifact))); if (!target.exists()) { target.getParentFile().mkdirs(); try { copy(source, target); } catch (RuntimeException e) { getLog().error("Could not copy features " + uri + " from source file " + source, e); } // for snapshot, generate the repository metadata in order to avoid override of snapshot from remote repositories if (artifact.isSnapshot()) { getLog().debug( "Feature " + uri + " is a SNAPSHOT, generate the maven-metadata-local.xml file"); File metadataTarget = new File(target.getParentFile(), "maven-metadata-local.xml"); try { MavenUtil.generateMavenMetadata(artifact, metadataTarget); } catch (Exception e) { getLog().warn("Could not create maven-metadata-local.xml", e); getLog().warn( "It means that this SNAPSHOT could be overwritten by an older one present on remote repositories"); } } } try { featuresService.addRepository(URI.create(uri)); } catch (Exception e) { buf.append("Could not install feature: ").append(artifact.toString()).append("\n"); buf.append(e.getMessage()).append("\n\n"); } } } // install bundles listed in startup properties that weren't in kars into the system dir Set<?> keySet = startupProperties.keySet(); for (Object keyObject : keySet) { String key = (String) keyObject; String path = this.dependencyHelper.pathFromMaven(key); File target = new File(system.resolve(path)); if (!target.exists()) { install(key, target); } } // install bundles listed in install features not in system for (Feature feature : localRepoFeatures) { List<Bundle> bundles = new ArrayList<Bundle>(); bundles.addAll(feature.getBundle()); for (Conditional cond : feature.getConditional()) { bundles.addAll(cond.getBundle()); } for (Bundle bundle : bundles) { String key = bundle.getLocation(); // remove wrap: protocol to resolve from maven if (key.startsWith("wrap:")) { key = key.substring(5); } String path = this.dependencyHelper.pathFromMaven(key); File test = new File(system.resolve(path)); if (!test.exists()) { File target = new File(system.resolve(path)); if (!target.exists()) { install(key, target); Artifact artifact = this.dependencyHelper.mvnToArtifact(key); if (artifact.isSnapshot()) { // generate maven-metadata-local.xml for the artifact File metadataSource = new File( this.dependencyHelper.resolveById(key, getLog()).getParentFile(), "maven-metadata-local.xml"); File metadataTarget = new File(target.getParentFile(), "maven-metadata-local.xml"); metadataTarget.getParentFile().mkdirs(); try { if (!metadataSource.exists()) { // the maven-metadata-local.xml doesn't exist in the local repo, generate one MavenUtil.generateMavenMetadata(artifact, metadataTarget); } else { // copy the metadata to the target copy(metadataSource, metadataTarget); } } catch (IOException ioException) { getLog().warn(ioException); getLog().warn( "Unable to copy the maven-metadata-local.xml, it means that this SNAPSHOT will be overwritten by a remote one (if exist)"); } } } } } } try { OutputStream out = new FileOutputStream(startupPropertiesFile); try { startupProperties.save(out); } finally { out.close(); } } catch (IOException e) { throw new MojoFailureException("Could not write startup.properties file at " + startupPropertiesFile, e); } if (buf.length() > 0) { throw new MojoExecutionException("Could not unpack all dependencies:\n" + buf.toString()); } }