List of usage examples for org.apache.maven.execution MavenSession getRepositorySession
public RepositorySystemSession getRepositorySession()
From source file:com.doublefx.maven.utils.flexmojos.mavenValidator.FlexMojosExtensionInstallationHelper.java
License:Apache License
protected void copyExtension(MavenSession session, String artifactCoordinates) throws MavenExecutionException { Artifact artifact;/*from ww w. j a v a 2s .c o m*/ try { artifact = new DefaultArtifact(artifactCoordinates); } catch (IllegalArgumentException e) { throw newMavenExecutionException(e); } ArtifactRequest request = new ArtifactRequest(); request.setArtifact(artifact); final List<RemoteRepository> remoteRepos = session.getCurrentProject().getRemoteProjectRepositories(); request.setRepositories(remoteRepos); ArtifactResult result; try { result = repoSystem.resolveArtifact(session.getRepositorySession(), request); } catch (ArtifactResolutionException e) { logger.info("Resolving artifact " + artifact + " from " + remoteRepos); throw newMavenExecutionException(e); } final Artifact resultArtifact = result.getArtifact(); final String maven_home = System.getenv("MAVEN_HOME"); final File destination = new File(maven_home + File.separator + "lib" + File.separator + "ext" + File.separator + resultArtifact.getArtifactId() + ".jar"); if (!destination.exists()) { logger.info("Resolved artifact " + artifact + " to " + resultArtifact.getFile() + " from " + result.getRepository()); try { Files.copy(resultArtifact.getFile().toPath(), destination.toPath()); } catch (IOException ignored) { } logger.info( resultArtifact.getArtifactId() + " is now configured, it will be applied to your next builds."); } }
From source file:com.github.maven.plugins.core.GitHubProjectMojo.java
License:Open Source License
/** * Configure client with credentials from given server id * // w ww . j av a 2 s. c o m * @param client * @param serverId * @param settings * @param session * @return true if configured, false otherwise * @throws MojoExecutionException */ protected boolean configureServerCredentials(final GitHubClient client, final String serverId, final Settings settings, final MavenSession session) throws MojoExecutionException { if (StringUtils.isEmpty(serverId)) return false; String serverUsername = null; String serverPassword = null; if (session != null) { RepositorySystemSession systemSession = session.getRepositorySession(); if (systemSession != null) { Authentication authInfo = systemSession.getAuthenticationSelector() .getAuthentication(new RemoteRepository().setId(serverId)); if (authInfo != null) { serverUsername = authInfo.getUsername(); serverPassword = authInfo.getPassword(); } } } if (StringUtils.isEmpty(serverPassword)) { Server server = getServer(settings, serverId); if (server == null) throw new MojoExecutionException( MessageFormat.format("Server ''{0}'' not found in settings", serverId)); if (isDebug()) debug(MessageFormat.format("Using ''{0}'' server credentials", serverId)); serverUsername = server.getUsername(); serverPassword = server.getPassword(); } if (!StringUtils.isEmpty(serverUsername, serverPassword)) { if (isDebug()) debug("Using basic authentication with username: " + serverUsername); client.setCredentials(serverUsername, serverPassword); return true; } // A server password without a username is assumed to be an OAuth2 token if (!StringUtils.isEmpty(serverPassword)) { if (isDebug()) debug("Using OAuth2 access token authentication"); client.setOAuth2Token(serverPassword); return true; } if (isDebug()) debug(MessageFormat.format("Server ''{0}'' is missing username/password credentials", serverId)); return false; }
From source file:com.googlecode.ounit.maven.ReflectiveSurefireReportParser.java
License:Open Source License
public ReflectiveSurefireReportParser(MavenSession session, BuildPluginManager pluginManager) throws Exception { // TODO: Utter a warning if surefire used in build is different from surefire used for reporting /*/* w ww .ja va 2 s . c o m*/ MavenProject project = session.getCurrentProject(); for (Plugin p : project.getBuildPlugins()) { if (p.getId().startsWith("org.apache.maven.plugins:maven-surefire-plugin")) { System.out.println("Surefire version in project: " + p.getVersion()); System.out.println("Surefire conf: " + p.getConfiguration().toString()); } } */ String sfGrId = "org.apache.maven.plugins"; String sfVer = "2.8.1"; Plugin plugin = new Plugin(); plugin.setGroupId(sfGrId); plugin.setArtifactId("maven-surefire-report-plugin"); plugin.setVersion(sfVer); PluginDescriptor pluginDescriptor = pluginManager.loadPlugin(plugin, session.getCurrentProject().getRemotePluginRepositories(), session.getRepositorySession()); parserClass = pluginManager.getPluginRealm(session, pluginDescriptor) .loadClass("org.apache.maven.plugins.surefire.report.SurefireReportParser"); testCaseClass = pluginManager.getPluginRealm(session, pluginDescriptor) .loadClass("org.apache.maven.plugins.surefire.report.ReportTestCase"); }
From source file:com.jayway.maven.plugins.android.phase_prebuild.AarMavenLifecycleParticipant.java
License:Open Source License
@Override public void afterProjectsRead(MavenSession session) throws MavenExecutionException { log.debug(""); log.debug("AMLP afterProjectsRead"); log.debug(""); log.debug("CurrentProject=" + session.getCurrentProject()); final List<MavenProject> projects = session.getProjects(); for (MavenProject project : projects) { log.debug(""); log.debug("project=" + project.getArtifact()); final BuildHelper helper = new BuildHelper(repoSystem, session.getRepositorySession(), project, log); final Collection<Artifact> artifacts = getProjectsArtifacts(session, project); log.debug("projects deps: : " + artifacts); for (Artifact artifact : artifacts) { final String type = artifact.getType(); if (type.equals(AndroidExtension.AAR)) { // An AAR lib contains a classes jar that needs to be added to the classpath. // Create a placeholder classes.jar and add it to the compile classpath. // It will replaced with the real classes.jar by GenerateSourcesMojo. addClassesToClasspath(helper, project, artifact); } else if (type.equals(AndroidExtension.APK)) { // The only time that an APK will likely be a dependency is when this an an APK test project. // So add a placeholder (we cannot resolve the actual dep pre build) to the compile classpath. // The placeholder will be replaced with the real APK jar later. addClassesToClasspath(helper, project, artifact); }// w w w . j a v a 2 s . c o m } } }
From source file:com.jayway.maven.plugins.android.phase_prebuild.AarMavenLifecycleParticipant.java
License:Open Source License
private Collection<Artifact> getProjectsArtifacts(MavenSession session, MavenProject project) throws MavenExecutionException { final DependencyResolver resolver = new DependencyResolver(log, repoSystem, session.getRepositorySession(), project.getRemoteProjectRepositories(), artifactHandler); try {//from www .j a va 2s. co m return resolver.getDependenciesFor(project.getArtifact()); } catch (MojoExecutionException e) { throw new MavenExecutionException("Could not resolve dependencies for " + project.getArtifact(), e); } }
From source file:guru.nidi.maven.tools.MavenUtil.java
License:Apache License
public static MavenProject projectFromArtifact(MavenSession session, ProjectBuilder projectBuilder, Artifact artifact, boolean resolveDependencies) throws ProjectBuildingException { final ProjectBuildingRequest request = new DefaultProjectBuildingRequest() .setLocalRepository(session.getLocalRepository()) .setRepositorySession(session.getRepositorySession()).setSystemProperties(System.getProperties()) .setResolveDependencies(resolveDependencies); return projectBuilder.build(artifact, request).getProject(); }
From source file:it.session.maven.plugin.TilesMavenLifecycleParticipant.java
License:Apache License
private void mergeTiles(MavenProject currentProject, MavenSession mavenSession) throws MavenExecutionException { Enumeration propertyNames = currentProject.getProperties().propertyNames(); while (propertyNames.hasMoreElements()) { String propertyName = (String) propertyNames.nextElement(); if (propertyName.startsWith(TILE_PROPERTY_PREFIX)) { mergeTile(currentProject, propertyName, mavenSession.getRepositorySession()); }// ww w . j a v a2s . c o m } }
From source file:org.apache.karaf.tooling.features.DependencyHelperFactory.java
License:Apache License
/** * <p>Create a new {@link DependencyHelper} based on what has been found in {@link org.codehaus.plexus.PlexusContainer}</p> * * <p>{@code karaf-maven-plugin} depends on {@code maven-core:3.0}, so for Maven 3.0.x, it may use this API directly. * When using Maven 3.1.x/3.2.x, it should use reflection to invoke org.apache.maven.RepositoryUtils.toArtifact(Artifact) * as this method directly references specific Aether implementation.</p> * * <p>When {@code karaf-maven-plugin} switches to {@code maven-core:3.1.0+}, reflection should be use for Sonatype variant of Aether.</p> * * @param container//from ww w.j a v a2 s. c o m * @param mavenProject * @param mavenSession * @param log * @return * @throws MojoExecutionException */ public static DependencyHelper createDependencyHelper(PlexusContainer container, MavenProject mavenProject, MavenSession mavenSession, Log log) throws MojoExecutionException { try { if (container.hasComponent(org.sonatype.aether.RepositorySystem.class)) { org.sonatype.aether.RepositorySystem system = container .lookup(org.sonatype.aether.RepositorySystem.class); org.sonatype.aether.RepositorySystemSession session = mavenSession.getRepositorySession(); List<RemoteRepository> repositories = mavenProject.getRemoteProjectRepositories(); return new Dependency30Helper(repositories, session, system); } else if (container.hasComponent(org.eclipse.aether.RepositorySystem.class)) { org.eclipse.aether.RepositorySystem system = container .lookup(org.eclipse.aether.RepositorySystem.class); Object session; try { session = MavenSession.class.getMethod("getRepositorySession").invoke(mavenSession); } catch (Exception e) { throw new MojoExecutionException(e.getMessage(), e); } List<?> repositories = mavenProject.getRemoteProjectRepositories(); return new Dependency31Helper(repositories, session, system); } } catch (ComponentLookupException e) { throw new MojoExecutionException(e.getMessage(), e); } throw new MojoExecutionException( "Cannot locate either org.sonatype.aether.RepositorySystem or org.eclipse.aether.RepositorySystem"); }
From source file:org.apache.karaf.tooling.utils.DependencyHelperFactory.java
License:Apache License
/** * <p>Create a new {@link DependencyHelper} based on what has been found in {@link org.codehaus.plexus.PlexusContainer}</p> * * <p>{@code karaf-maven-plugin} depends on {@code maven-core:3.0}, so for Maven 3.0.x, it may use this API directly. * When using Maven 3.1.x/3.2.x, it should use reflection to invoke org.apache.maven.RepositoryUtils.toArtifact(Artifact) * as this method directly references specific Aether implementation.</p> * * <p>When {@code karaf-maven-plugin} switches to {@code maven-core:3.1.0+}, reflection should be use for Sonatype variant of Aether.</p> * * @param container//from w w w .j a va 2s . c o m * @param mavenProject * @param mavenSession * @param log * @return * @throws MojoExecutionException */ public static DependencyHelper createDependencyHelper(PlexusContainer container, MavenProject mavenProject, MavenSession mavenSession, Log log) throws MojoExecutionException { try { if (container.hasComponent("org.sonatype.aether.RepositorySystem")) { org.sonatype.aether.RepositorySystem system = container .lookup(org.sonatype.aether.RepositorySystem.class); org.sonatype.aether.RepositorySystemSession session = mavenSession.getRepositorySession(); List<RemoteRepository> repositories = mavenProject.getRemoteProjectRepositories(); return new Dependency30Helper(repositories, session, system); } else if (container.hasComponent("org.eclipse.aether.RepositorySystem")) { org.eclipse.aether.RepositorySystem system = container .lookup(org.eclipse.aether.RepositorySystem.class); Object session; try { session = MavenSession.class.getMethod("getRepositorySession").invoke(mavenSession); } catch (Exception e) { throw new MojoExecutionException(e.getMessage(), e); } List<?> repositories = mavenProject.getRemoteProjectRepositories(); return new Dependency31Helper(repositories, session, system); } } catch (ComponentLookupException e) { throw new MojoExecutionException(e.getMessage(), e); } throw new MojoExecutionException( "Cannot locate either org.sonatype.aether.RepositorySystem or org.eclipse.aether.RepositorySystem"); }
From source file:org.codehaus.mojo.sonar.Bootstraper.java
License:Open Source License
private void executeMojo(MavenProject project, MavenSession session) throws MojoExecutionException { ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader(); try {//from ww w .jav a 2s. c om RepositoryRequest repositoryRequest = new DefaultRepositoryRequest(); repositoryRequest.setLocalRepository(session.getLocalRepository()); repositoryRequest.setRemoteRepositories(project.getPluginArtifactRepositories()); Plugin plugin = createSonarPlugin(); List<RemoteRepository> remoteRepositories = session.getCurrentProject().getRemotePluginRepositories(); PluginDescriptor pluginDescriptor = pluginManager.getPluginDescriptor(plugin, remoteRepositories, session.getRepositorySession()); String goal = "sonar"; MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(goal); if (mojoDescriptor == null) { throw new MojoExecutionException("Unknown mojo goal: " + goal); } MojoExecution mojoExecution = new MojoExecution(plugin, goal, "sonar" + goal); mojoExecution.setConfiguration(convert(mojoDescriptor)); mojoExecution.setMojoDescriptor(mojoDescriptor); // olamy : we exclude nothing and import nothing regarding realm import and artifacts DependencyFilter artifactFilter = new DependencyFilter() { public boolean accept(DependencyNode arg0, List<DependencyNode> arg1) { return true; } }; pluginManager.setupPluginRealm(pluginDescriptor, session, Thread.currentThread().getContextClassLoader(), Collections.<String>emptyList(), artifactFilter); Mojo mojo = pluginManager.getConfiguredMojo(Mojo.class, session, mojoExecution); Thread.currentThread().setContextClassLoader(pluginDescriptor.getClassRealm()); mojo.execute(); } catch (Exception e) { throw new MojoExecutionException("Can not execute Sonar", e); } finally { Thread.currentThread().setContextClassLoader(originalClassLoader); } }