protected @Override URLConnection openConnection(URL u) throws IOException { String path = u.getPath(); if (!path.startsWith("/")) { throw new IOException(path); } String stuff = path.substring(1); MavenEmbedder online = EmbedderFactory.getOnlineEmbedder(); Artifact a; String[] pieces = stuff.split(":"); if (pieces.length == 4) { a = online.createArtifact(pieces[0], pieces[1], pieces[2], pieces[3]); } else if (pieces.length == 5) { a = online.createArtifactWithClassifier(pieces[0], pieces[1], pieces[2], pieces[3], pieces[4]); } else { throw new IOException(stuff); } try { online.resolve(a, Collections.<ArtifactRepository>singletonList(online.createRemoteRepository(RepositorySystem.DEFAULT_REMOTE_REPO_URL, RepositorySystem.DEFAULT_REMOTE_REPO_ID)), online.getLocalRepository()); } catch (Exception x) { throw new IOException(stuff + ": " + x, x); } File f = a.getFile(); if (!f.isFile()) { throw new IOException("failed to download " + stuff); } Logger.getLogger(MavenProtocolHandler.class.getName()).log(Level.FINE, "resolved {0} -> {1}", new Object[] {stuff, f}); return Utilities.toURI(f).toURL().openConnection(); }
public static DependencyNode createDependencyTree(MavenProject project, MavenEmbedder embedder, String scope) { //TODO: check alternative for deprecated maven components DependencyTreeBuilder builder = embedder.lookupComponent(DependencyTreeBuilder.class); assert builder !=null : "DependencyTreeBuilder component not found in maven"; ArtifactFactory factory = embedder.lookupComponent(ArtifactFactory.class); assert factory !=null : "ArtifactFactory component not found in maven"; ArtifactMetadataSource source = embedder.lookupComponent(ArtifactMetadataSource.class); assert source !=null : "ArtifactMetadataSource component not found in maven"; ArtifactCollector collector = embedder.lookupComponent(ArtifactCollector.class); assert collector !=null : "ArtifactCollector component not found in maven"; embedder.setUpLegacySupport(); return createDependencyTree(project, builder, embedder.getLocalRepository(), factory, source, collector, scope); }
public ProjectBuildingResult buildProject(Artifact art, ProjectBuildingRequest req) throws ProjectBuildingException { if (req.getLocalRepository() == null) { req.setLocalRepository(getLocalRepository()); } MavenExecutionRequest request = createMavenExecutionRequest(); req.setProcessPlugins(false); req.setRepositorySession(maven.newRepositorySession(request)); ProjectBuildingResult res = projectBuilder.build(art, req); normalizePaths(res.getProject()); return res; }
@Override public File getLocation() { if (location != null) { return location; } MavenEmbedder embedder = EmbedderFactory.getProjectEmbedder(); Artifact art = embedder.createProjectArtifact(groupId, artifactId, version); File file = new File(embedder.getLocalRepositoryFile(), embedder.getLocalRepository().pathOf(art)); location = FileUtil.normalizeFile(file); return location; }
online.setUpLegacySupport(); final MavenProjectBuilder builder = online.lookupComponent(MavenProjectBuilder.class); assert builder !=null : "MavenProjectBuilder component not found in maven"; final Artifact projectArtifact = online.createArtifactWithClassifier(d.getGroupId(), d.getArtifactId(), d.getVersion(), d.getType(), d.getClassifier()); String localPath = online.getLocalRepository().pathOf(projectArtifact); File f = FileUtil.normalizeFile(new File(online.getLocalRepository().getBasedir(), localPath)); if (f.exists()) { cpFiles.add(f);
/** * Tries to download an artifact. * @param info a version of an artifact * @return the file in the local repository (might not exist if download failed) * @throws AbstractArtifactResolutionException currently never? * @since 1.17 */ public static File downloadArtifact(NBVersionInfo info) throws Exception { Artifact a = createArtifact(info); MavenEmbedder online = EmbedderFactory.getOnlineEmbedder(); List<ArtifactRepository> remotes; RepositoryInfo repo = RepositoryPreferences.getInstance().getRepositoryInfoById(info.getRepoId()); if (repo != null && repo.isRemoteDownloadable()) { remotes = Collections.singletonList(online.createRemoteRepository(repo.getRepositoryUrl(), repo.getId())); } else { remotes = Collections.singletonList(online.createRemoteRepository(RepositorySystem.DEFAULT_REMOTE_REPO_URL, RepositorySystem.DEFAULT_REMOTE_REPO_ID)); } online.resolve(a, remotes, online.getLocalRepository()); return a.getFile(); }
private static @NonNull Artifact createArtifact(@NonNull NBVersionInfo info, @NullAllowed String classifier) { Artifact art; MavenEmbedder online = EmbedderFactory.getOnlineEmbedder(); if (info.getClassifier() != null || classifier != null) { art = online.createArtifactWithClassifier(info.getGroupId(), info.getArtifactId(), info.getVersion(), info.getType() != null ? info.getType() : "jar", //NOI18N classifier == null ? info.getClassifier() : classifier); } else { art = online.createArtifact(info.getGroupId(), info.getArtifactId(), info.getVersion(), null, info.getType() != null ? info.getType() : "jar"); //NOI18N } ArtifactRepository repo = online.getLocalRepository(); String localPath = repo.pathOf(art); art.setFile(FileUtil.normalizeFile(new File(online.getLocalRepositoryFile(), localPath))); return art; }
@Override public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException { Artifact artifactParent = embedder.lookupComponent(RepositorySystem.class).createProjectArtifact(groupId, artifactId, version); try { embedder.resolve(artifactParent, remoteRepositories, embedder.getLocalRepository()); } catch (ArtifactResolutionException ex) { Exceptions.printStackTrace(ex); throw new UnresolvableModelException(ex.getMessage(), groupId , artifactId , version ); } catch (ArtifactNotFoundException ex) { Exceptions.printStackTrace(ex); throw new UnresolvableModelException( ex.getMessage(), groupId , artifactId , version ); } return new FileModelSource(artifactParent.getFile()); } }
session.setOffline(isOffline()); session.setLocalRepositoryManager(new SimpleLocalRepositoryManager(getLocalRepository().getBasedir())); Settings _settings = getSettings(); for (Mirror m : _settings.getMirrors()) { mirrorSelector.add(m.getId(), m.getUrl(), m.getLayout(), false, m.getMirrorOf(), m.getMirrorOfLayouts()); mavenExecutionRequest.setOffline(isOffline()); mavenExecutionRequest.setTransferListener(ProgressTransferListener.activeListener()); session.setTransferListener(ProgressTransferListener.activeListener()); lookupComponent(LegacySupport.class).setSession(new MavenSession(getPlexus(), session, mavenExecutionRequest, new DefaultMavenExecutionResult()));
String[] gav = extensionPlugin.split(":", 3); MavenEmbedder online = EmbedderFactory.getOnlineEmbedder(); Artifact art = online.createArtifact(gav[0], gav[1], gav[2], "maven-plugin"); online.resolve(art, Collections.<ArtifactRepository>emptyList(), online.getLocalRepository()); File jar = art.getFile(); if (jar.isFile()) {
MavenExecutionRequest req = new DefaultMavenExecutionRequest(); ArtifactRepository localRepository = getLocalRepository(); req.setLocalRepository(localRepository); req.setLocalRepositoryPath(localRepository.getBasedir()); req.setSystemProperties(getSystemProperties()); req.setUserProperties(embedderConfiguration.getUserProperties()); try { populator.populateFromSettings(req, getSettings()); populator.populateDefaults(req); } catch (MavenExecutionRequestPopulationException x) { req.setOffline(isOffline()); req.setRepositoryCache(new NbRepositoryCache());
@Override public void run() { try { //TODO add progress bar. // XXX does online.resolve(...) not suffice? online.setUpLegacySupport(); builder.buildFromRepository(projectArtifact, p.getMavenProject().getRemoteArtifactRepositories(), online.getLocalRepository()); synchronized (AuxPropsImpl.this) { recheck = true; } } catch (ProjectBuildingException ex) { ex.printStackTrace(); // Exceptions.printStackTrace(ex); } } });
/** * using this method one creates an ArtifactRepository instance with injected mirrors and proxies * @param url * @param id * @return */ public ArtifactRepository createRemoteRepository(String url, String id) { setUpLegacySupport(); ArtifactRepositoryFactory fact = lookupComponent(ArtifactRepositoryFactory.class); assert fact!=null : "ArtifactRepositoryFactory component not found in maven"; ArtifactRepositoryPolicy snapshotsPolicy = new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN); ArtifactRepositoryPolicy releasesPolicy = new ArtifactRepositoryPolicy(true, ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS, ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN); return fact.createArtifactRepository(id, url, new DefaultRepositoryLayout(), snapshotsPolicy, releasesPolicy); }
/** * * @return normalized File for local repository root * @since 2.26 */ public File getLocalRepositoryFile() { return FileUtil.normalizeFile(new File(getLocalRepository().getBasedir())); }
private MavenProject load(ArtifactInfo ai) { try { Artifact projectArtifact = embedder.createArtifact(ai.groupId, ai.artifactId, ai.version, ai.packaging != null ? ai.packaging : "jar"); ProjectBuildingRequest dpbr = embedder.createMavenExecutionRequest().getProjectBuildingRequest(); //mkleint: remote repositories don't matter we use project embedder. dpbr.setRemoteRepositories(remoteRepos); dpbr.setProcessPlugins(false); dpbr.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); ProjectBuildingResult res = embedder.buildProject(projectArtifact, dpbr); if (res.getProject() != null) { return res.getProject(); } else { LOG.log(Level.FINER, "No project model from repository for {0}: {1}", new Object[] {ai, res.getProblems()}); } } catch (ProjectBuildingException ex) { LOG.log(Level.FINER, "Failed to load project model from repository for {0}: {1}", new Object[] {ai, ex}); } catch (Exception exception) { LOG.log(Level.FINER, "Failed to load project model from repository for " + ai, exception); } return null; }
/** * * @param sources * @param remoteRepositories - these instances need to be properly mirrored and proxied. Either by creating via EmbedderFactory.createRemoteRepository() * or by using instances from MavenProject * @param localRepository * @throws ArtifactResolutionException * @throws ArtifactNotFoundException */ public void resolve(Artifact sources, List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepository) throws ArtifactResolutionException, ArtifactNotFoundException { setUpLegacySupport(); ArtifactResolutionRequest req = new ArtifactResolutionRequest(); req.setLocalRepository(localRepository); req.setRemoteRepositories(remoteRepositories); req.setArtifact(sources); req.setOffline(isOffline()); ArtifactResolutionResult result = repositorySystem.resolve(req); normalizePath(sources); // XXX check result for exceptions and throw them now? for (Exception ex : result.getExceptions()) { LOG.log(Level.FINE, null, ex); } }
/** * using this method one creates an ArtifactRepository instance with injected mirrors and proxies * @param embedder * @param url * @param id * @return * @deprecated use MavenEmbedder.createRemoteRepository */ @Deprecated public static ArtifactRepository createRemoteRepository(MavenEmbedder embedder, String url, String id) { return embedder.createRemoteRepository(url, id); }
@Messages({ "DownloadAction_sources_label=Download Sources", "DownloadAction_javadoc_label=Download Javadoc" }) DownloadAction(Artifact primaryArt, boolean javadoc) { super(javadoc ? DownloadAction_javadoc_label() : DownloadAction_sources_label()); art = EmbedderFactory.getProjectEmbedder().createArtifactWithClassifier(primaryArt.getGroupId(), primaryArt.getArtifactId(), primaryArt.getVersion(), "jar", javadoc ? "javadoc" : "sources"); primary = false; setEnabled((javadoc ? hasJavadoc : hasSources) && info.isRemoteDownloadable() && !FileUtilities.convertArtifactToLocalRepositoryFile(art).isFile()); } @Messages({"# {0} - artifact ID", "DownloadAction_downloading=Downloading {0}"})
/** * * @param pom * @return result object with access to effective pom model and raw models for each parent. * @throws ModelBuildingException if the POM or parents could not even be parsed; warnings are not reported */ public ModelBuildingResult executeModelBuilder(File pom) throws ModelBuildingException { ModelBuilder mb = lookupComponent(ModelBuilder.class); assert mb!=null : "ModelBuilder component not found in maven"; ModelBuildingRequest req = new DefaultModelBuildingRequest(); req.setPomFile(pom); req.setProcessPlugins(false); req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); req.setLocationTracking(true); req.setModelResolver(new NBRepositoryModelResolver(this)); req.setSystemProperties(getSystemProperties()); req.setUserProperties(embedderConfiguration.getUserProperties()); return mb.build(req); }
public List<String> getLifecyclePhases() { LifecycleMapping lifecycleMapping = lookupComponent(LifecycleMapping.class); if (lifecycleMapping != null) { Set<String> phases = new TreeSet<String>(); Map<String, Lifecycle> lifecycles = lifecycleMapping.getLifecycles(); for (Lifecycle lifecycle : lifecycles.values()) { phases.addAll(lifecycle.getPhases().keySet()); } return new ArrayList<String>(phases); } return Collections.<String>emptyList(); }