private void validate( File file, Collection<Exception> exceptions, Collection<MavenProjectProblem> problems, Collection<MavenId> unresolvedArtifacts) throws RemoteException { for (Exception each : exceptions) { Maven3ServerGlobals.getLogger().info(each); if (each instanceof InvalidProjectModelException) { ModelValidationResult modelValidationResult = ((InvalidProjectModelException) each).getValidationResult(); if (modelValidationResult != null) { for (Object eachValidationProblem : modelValidationResult.getMessages()) { problems.add( MavenProjectProblem.createStructureProblem( file.getPath(), (String) eachValidationProblem)); } } else { problems.add( MavenProjectProblem.createStructureProblem( file.getPath(), each.getCause().getMessage())); } } else if (each instanceof ProjectBuildingException) { String causeMessage = each.getCause() != null ? each.getCause().getMessage() : each.getMessage(); problems.add(MavenProjectProblem.createStructureProblem(file.getPath(), causeMessage)); } else { problems.add(MavenProjectProblem.createStructureProblem(file.getPath(), each.getMessage())); } } unresolvedArtifacts.addAll(retrieveUnresolvedArtifactIds()); }
private static Settings buildSettings( SettingsBuilder builder, MavenServerSettings settings, Properties systemProperties, Properties userProperties) throws RemoteException { SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest(); settingsRequest.setGlobalSettingsFile(settings.getGlobalSettingsFile()); settingsRequest.setUserSettingsFile(settings.getUserSettingsFile()); settingsRequest.setSystemProperties(systemProperties); settingsRequest.setUserProperties(userProperties); Settings result = new Settings(); try { result = builder.build(settingsRequest).getEffectiveSettings(); } catch (SettingsBuildingException e) { Maven3ServerGlobals.getLogger().info(e); } if (settings.getLocalRepository() != null) { result.setLocalRepository(settings.getLocalRepository().getPath()); } if (result.getLocalRepository() == null) { result.setLocalRepository( new File(SystemProperties.getUserHome(), ".m2/repository").getPath()); } return result; }
private static Model doInterpolate(Model result, File basedir) throws RemoteException { try { AbstractStringBasedModelInterpolator interpolator = new CustomMaven3ModelInterpolator(new DefaultPathTranslator()); interpolator.initialize(); Properties props = MavenServerUtil.collectSystemProperties(); ProjectBuilderConfiguration config = new DefaultProjectBuilderConfiguration().setExecutionProperties(props); result = interpolator.interpolate(result, basedir, config, false); } catch (ModelInterpolationException e) { Maven3ServerGlobals.getLogger().warn(e); } catch (InitializationException e) { Maven3ServerGlobals.getLogger().error(e); } return result; }
private Artifact doResolve(Artifact artifact, List<ArtifactRepository> remoteRepositories) throws RemoteException { try { resolve(artifact, remoteRepositories); return artifact; } catch (Exception e) { Maven3ServerGlobals.getLogger().info(e); } return artifact; }
@NotNull @Override public List<MavenArtifact> resolveTransitively( @NotNull List<MavenArtifactInfo> artifacts, @NotNull List<MavenRemoteRepository> remoteRepositories) throws RemoteException, MavenServerProcessCanceledException { try { Set<Artifact> toResolve = new LinkedHashSet<Artifact>(); for (MavenArtifactInfo each : artifacts) { toResolve.add(createArtifact(each)); } Artifact project = getComponent(ArtifactFactory.class).createBuildArtifact("temp", "temp", "666", "pom"); Set<Artifact> res = getComponent(ArtifactResolver.class) .resolveTransitively( toResolve, project, Collections.EMPTY_MAP, myLocalRepository, convertRepositories(remoteRepositories), getComponent(ArtifactMetadataSource.class)) .getArtifacts(); return MavenModelConverter.convertArtifacts( res, new THashMap<Artifact, MavenArtifact>(), getLocalRepositoryFile()); } catch (ArtifactResolutionException e) { Maven3ServerGlobals.getLogger().info(e); } catch (ArtifactNotFoundException e) { Maven3ServerGlobals.getLogger().info(e); } catch (Exception e) { throw rethrowException(e); } return Collections.emptyList(); }
private List<ArtifactRepository> convertRepositories(List<MavenRemoteRepository> repositories) throws RemoteException { List<ArtifactRepository> result = new ArrayList<ArtifactRepository>(); for (MavenRemoteRepository each : repositories) { try { ArtifactRepositoryFactory factory = getComponent(ArtifactRepositoryFactory.class); result.add( ProjectUtils.buildArtifactRepository( MavenModelConverter.toNativeRepository(each), factory, myContainer)); } catch (InvalidRepositoryException e) { Maven3ServerGlobals.getLogger().warn(e); } } return result; }
private static ProfileActivator[] getProfileActivators(File basedir) throws RemoteException { SystemPropertyProfileActivator sysPropertyActivator = new SystemPropertyProfileActivator(); DefaultContext context = new DefaultContext(); context.put("SystemProperties", MavenServerUtil.collectSystemProperties()); try { sysPropertyActivator.contextualize(context); } catch (ContextException e) { Maven3ServerGlobals.getLogger().error(e); return new ProfileActivator[0]; } return new ProfileActivator[] { new MyFileProfileActivator(basedir), sysPropertyActivator, new JdkPrefixProfileActivator(), new OperatingSystemProfileActivator() }; }
public static ProfileApplicationResult applyProfiles( MavenModel model, File basedir, Collection<String> explicitProfiles, Collection<String> alwaysOnProfiles) throws RemoteException { Model nativeModel = MavenModelConverter.toNativeModel(model); List<Profile> activatedPom = new ArrayList<Profile>(); List<Profile> activatedExternal = new ArrayList<Profile>(); List<Profile> activeByDefault = new ArrayList<Profile>(); List<Profile> rawProfiles = nativeModel.getProfiles(); List<Profile> expandedProfilesCache = null; for (int i = 0; i < rawProfiles.size(); i++) { Profile eachRawProfile = rawProfiles.get(i); boolean shouldAdd = explicitProfiles.contains(eachRawProfile.getId()) || alwaysOnProfiles.contains(eachRawProfile.getId()); Activation activation = eachRawProfile.getActivation(); if (activation != null) { if (activation.isActiveByDefault()) { activeByDefault.add(eachRawProfile); } // expand only if necessary if (expandedProfilesCache == null) expandedProfilesCache = doInterpolate(nativeModel, basedir).getProfiles(); Profile eachExpandedProfile = expandedProfilesCache.get(i); for (ProfileActivator eachActivator : getProfileActivators(basedir)) { try { if (eachActivator.canDetermineActivation(eachExpandedProfile) && eachActivator.isActive(eachExpandedProfile)) { shouldAdd = true; break; } } catch (ProfileActivationException e) { Maven3ServerGlobals.getLogger().warn(e); } } } if (shouldAdd) { if (MavenConstants.PROFILE_FROM_POM.equals(eachRawProfile.getSource())) { activatedPom.add(eachRawProfile); } else { activatedExternal.add(eachRawProfile); } } } List<Profile> activatedProfiles = new ArrayList<Profile>(activatedPom.isEmpty() ? activeByDefault : activatedPom); activatedProfiles.addAll(activatedExternal); for (Profile each : activatedProfiles) { new DefaultProfileInjector().injectProfile(nativeModel, each, null, null); } return new ProfileApplicationResult( MavenModelConverter.convertModel(nativeModel, null), collectProfilesIds(activatedProfiles)); }
@Override public Collection<MavenArtifact> resolvePlugin( @NotNull MavenPlugin plugin, @NotNull List<MavenRemoteRepository> repositories, int nativeMavenProjectId, boolean transitive) throws RemoteException, MavenServerProcessCanceledException { try { Plugin mavenPlugin = new Plugin(); mavenPlugin.setGroupId(plugin.getGroupId()); mavenPlugin.setArtifactId(plugin.getArtifactId()); mavenPlugin.setVersion(plugin.getVersion()); MavenProject project = RemoteNativeMavenProjectHolder.findProjectById(nativeMavenProjectId); MavenExecutionRequest request = createRequest( null, Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String>emptyList()); DefaultMaven maven = (DefaultMaven) getComponent(Maven.class); RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request); if (plugin.getVersion() == null) { PluginVersionRequest versionRequest = new DefaultPluginVersionRequest( mavenPlugin, repositorySystemSession, project.getRemotePluginRepositories()); mavenPlugin.setVersion( getComponent(PluginVersionResolver.class).resolve(versionRequest).getVersion()); } PluginDescriptor result = getComponent(MavenPluginManager.class) .getPluginDescriptor( mavenPlugin, project.getRemotePluginRepositories(), repositorySystemSession); Map<MavenArtifactInfo, MavenArtifact> resolvedArtifacts = new THashMap<MavenArtifactInfo, MavenArtifact>(); Artifact pluginArtifact = result.getPluginArtifact(); MavenArtifactInfo artifactInfo = new MavenArtifactInfo( pluginArtifact.getGroupId(), pluginArtifact.getArtifactId(), pluginArtifact.getVersion(), pluginArtifact.getType(), null); resolveIfNecessary(artifactInfo, repositories, resolvedArtifacts); if (transitive) { // todo try to use parallel downloading for (Artifact each : result.getIntroducedDependencyArtifacts()) { resolveIfNecessary( new MavenArtifactInfo( each.getGroupId(), each.getArtifactId(), each.getVersion(), each.getType(), null), repositories, resolvedArtifacts); } for (ComponentDependency each : result.getDependencies()) { resolveIfNecessary( new MavenArtifactInfo( each.getGroupId(), each.getArtifactId(), each.getVersion(), each.getType(), null), repositories, resolvedArtifacts); } } return new THashSet<MavenArtifact>(resolvedArtifacts.values()); } catch (Exception e) { Maven3ServerGlobals.getLogger().info(e); return Collections.emptyList(); } }