public Maven3ServerEmbedderImpl(MavenServerSettings settings) throws RemoteException { File mavenHome = settings.getMavenHome(); if (mavenHome != null) { System.setProperty("maven.home", mavenHome.getPath()); } myConsoleWrapper = new Maven3ServerConsoleLogger(); myConsoleWrapper.setThreshold(settings.getLoggingLevel()); ClassWorld classWorld = new ClassWorld("plexus.core", Thread.currentThread().getContextClassLoader()); MavenCli cli = new MavenCli(classWorld) { @Override protected void customizeContainer(PlexusContainer container) { ((DefaultPlexusContainer) container) .setLoggerManager( new BaseLoggerManager() { @Override protected Logger createLogger(String s) { return myConsoleWrapper; } }); } }; Class cliRequestClass; try { cliRequestClass = MavenCli.class.getClassLoader().loadClass("org.apache.maven.cli.MavenCli$CliRequest"); } catch (ClassNotFoundException e) { throw new RuntimeException("Class \"org.apache.maven.cli.MavenCli$CliRequest\" not found"); } Object cliRequest; try { String[] commandLineOptions = new String[settings.getUserProperties().size()]; int idx = 0; for (Map.Entry<Object, Object> each : settings.getUserProperties().entrySet()) { commandLineOptions[idx++] = "-D" + each.getKey() + "=" + each.getValue(); } Constructor constructor = cliRequestClass.getDeclaredConstructor(String[].class, ClassWorld.class); constructor.setAccessible(true); cliRequest = constructor.newInstance(commandLineOptions, classWorld); for (String each : new String[] {"initialize", "cli", "properties", "container"}) { Method m = MavenCli.class.getDeclaredMethod(each, cliRequestClass); m.setAccessible(true); m.invoke(cli, cliRequest); } } catch (InstantiationException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } // reset threshold myContainer = FieldAccessor.get(MavenCli.class, cli, "container"); myContainer.getLoggerManager().setThreshold(settings.getLoggingLevel()); mySystemProperties = FieldAccessor.<Properties>get(cliRequestClass, cliRequest, "systemProperties"); myMavenSettings = buildSettings( FieldAccessor.<SettingsBuilder>get(MavenCli.class, cli, "settingsBuilder"), settings, mySystemProperties, FieldAccessor.<Properties>get(cliRequestClass, cliRequest, "userProperties")); myLocalRepository = createLocalRepository(settings.getSnapshotUpdatePolicy()); }
/** * @author Vladislav.Soroka * @since 1/20/2015 */ public abstract class Maven3ServerEmbedder extends MavenRemoteObject implements MavenServerEmbedder { public static final boolean USE_MVN2_COMPATIBLE_DEPENDENCY_RESOLVING = System.getProperty("idea.maven3.use.compat.resolver") != null; private static final String MAVEN_VERSION = System.getProperty(MAVEN_EMBEDDER_VERSION); protected Maven3ServerEmbedder(MavenServerSettings settings) { initLog4J(settings); } private static void initLog4J(MavenServerSettings settings) { try { BasicConfigurator.configure(); final Level rootLoggerLevel = toLog4JLevel(settings.getLoggingLevel()); Logger.getRootLogger().setLevel(rootLoggerLevel); if (!rootLoggerLevel.isGreaterOrEqual(Level.ERROR)) { Logger.getLogger("org.apache.maven.wagon.providers.http.httpclient.wire") .setLevel(Level.ERROR); Logger.getLogger("org.apache.http.wire").setLevel(Level.ERROR); } } catch (Throwable ignore) { } } private static Level toLog4JLevel(int level) { switch (level) { case MavenServerConsole.LEVEL_DEBUG: return Level.ALL; case MavenServerConsole.LEVEL_ERROR: return Level.ERROR; case MavenServerConsole.LEVEL_FATAL: return Level.FATAL; case MavenServerConsole.LEVEL_DISABLED: return Level.OFF; case MavenServerConsole.LEVEL_INFO: return Level.INFO; case MavenServerConsole.LEVEL_WARN: return Level.WARN; } return Level.INFO; } protected abstract ArtifactRepository getLocalRepository(); @NotNull @Override public List<String> retrieveAvailableVersions( @NotNull String groupId, @NotNull String artifactId, @NotNull List<MavenRemoteRepository> remoteRepositories) throws RemoteException { try { Artifact artifact = new DefaultArtifact( groupId, artifactId, "", Artifact.SCOPE_COMPILE, "pom", null, new DefaultArtifactHandler("pom")); List<ArtifactVersion> versions = getComponent(ArtifactMetadataSource.class) .retrieveAvailableVersions( artifact, getLocalRepository(), convertRepositories(remoteRepositories)); return ContainerUtil.map( versions, new Function<ArtifactVersion, String>() { @Override public String fun(ArtifactVersion version) { return version.toString(); } }); } catch (Exception e) { Maven3ServerGlobals.getLogger().info(e); } return Collections.emptyList(); } @NotNull protected List<ProjectBuildingResult> getProjectBuildingResults( @NotNull MavenExecutionRequest request, @NotNull Collection<File> files) { final ProjectBuilder builder = getComponent(ProjectBuilder.class); CustomMaven3ModelInterpolator2 modelInterpolator = (CustomMaven3ModelInterpolator2) getComponent(ModelInterpolator.class); String savedLocalRepository = modelInterpolator.getLocalRepository(); modelInterpolator.setLocalRepository(request.getLocalRepositoryPath().getAbsolutePath()); List<ProjectBuildingResult> buildingResults = new SmartList<ProjectBuildingResult>(); final ProjectBuildingRequest projectBuildingRequest = request.getProjectBuildingRequest(); projectBuildingRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL); projectBuildingRequest.setResolveDependencies(false); try { buildingResults = builder.build(new ArrayList<File>(files), false, projectBuildingRequest); } catch (ProjectBuildingException e) { for (ProjectBuildingResult result : e.getResults()) { if (result.getProject() != null) { buildingResults.add(result); } else { try { ProjectBuildingResult build = builder.build(result.getPomFile(), projectBuildingRequest); buildingResults.add(build); } catch (ProjectBuildingException e2) { buildingResults.addAll(e2.getResults()); } } } } finally { modelInterpolator.setLocalRepository(savedLocalRepository); } return buildingResults; } protected void addMvn2CompatResults( MavenProject project, List<Exception> exceptions, List<ResolutionListener> listeners, ArtifactRepository localRepository, Collection<MavenExecutionResult> executionResults) { ArtifactResolutionRequest resolutionRequest = new ArtifactResolutionRequest(); resolutionRequest.setArtifactDependencies(project.getDependencyArtifacts()); resolutionRequest.setArtifact(project.getArtifact()); resolutionRequest.setManagedVersionMap(project.getManagedVersionMap()); resolutionRequest.setLocalRepository(localRepository); resolutionRequest.setRemoteRepositories(project.getRemoteArtifactRepositories()); resolutionRequest.setListeners(listeners); resolutionRequest.setResolveRoot(false); resolutionRequest.setResolveTransitively(true); ArtifactResolver resolver = getComponent(ArtifactResolver.class); ArtifactResolutionResult result = resolver.resolve(resolutionRequest); project.setArtifacts(result.getArtifacts()); executionResults.add(new MavenExecutionResult(project, exceptions)); } @NotNull protected abstract List<ArtifactRepository> convertRepositories( List<MavenRemoteRepository> repositories) throws RemoteException; @Nullable public String getMavenVersion() { return MAVEN_VERSION; } @SuppressWarnings({"unchecked"}) public abstract <T> T getComponent(Class<T> clazz, String roleHint); @SuppressWarnings({"unchecked"}) public abstract <T> T getComponent(Class<T> clazz); public abstract void executeWithMavenSession(MavenExecutionRequest request, Runnable runnable); public abstract MavenExecutionRequest createRequest( File file, List<String> activeProfiles, List<String> inactiveProfiles, List<String> goals) throws RemoteException; }