use of org.apache.maven.model.Profile in project intellij-community by JetBrains.
the class Maven30ServerEmbedderImpl method applyProfiles.
public static ProfileApplicationResult applyProfiles(MavenModel model, File basedir, MavenExplicitProfiles explicitProfiles, Collection<String> alwaysOnProfiles) throws RemoteException {
Model nativeModel = MavenModelConverter.toNativeModel(model);
Collection<String> enabledProfiles = explicitProfiles.getEnabledProfiles();
Collection<String> disabledProfiles = explicitProfiles.getDisabledProfiles();
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;
List<Profile> deactivatedProfiles = new ArrayList<Profile>();
for (int i = 0; i < rawProfiles.size(); i++) {
Profile eachRawProfile = rawProfiles.get(i);
if (disabledProfiles.contains(eachRawProfile.getId())) {
deactivatedProfiles.add(eachRawProfile);
continue;
}
boolean shouldAdd = enabledProfiles.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), new MavenExplicitProfiles(collectProfilesIds(activatedProfiles), collectProfilesIds(deactivatedProfiles)));
}
use of org.apache.maven.model.Profile in project maven-plugins by apache.
the class AntBuildWriter method getUninterpolatedSystemPath.
private String getUninterpolatedSystemPath(Artifact artifact) {
String managementKey = artifact.getDependencyConflictId();
for (Dependency dependency : project.getOriginalModel().getDependencies()) {
if (managementKey.equals(dependency.getManagementKey())) {
return dependency.getSystemPath();
}
}
for (Profile profile : project.getOriginalModel().getProfiles()) {
for (Dependency dependency : profile.getDependencies()) {
if (managementKey.equals(dependency.getManagementKey())) {
return dependency.getSystemPath();
}
}
}
String path = artifact.getFile().getAbsolutePath();
Properties props = new Properties();
props.putAll(project.getProperties());
props.putAll(executionProperties);
props.remove("user.dir");
props.put("basedir", project.getBasedir().getAbsolutePath());
SortedMap<String, String> candidateProperties = new TreeMap<String, String>();
for (Object o : props.keySet()) {
String key = (String) o;
String value = new File(props.getProperty(key)).getPath();
if (path.startsWith(value) && value.length() > 0) {
candidateProperties.put(value, key);
}
}
if (!candidateProperties.isEmpty()) {
String value = candidateProperties.lastKey();
String key = candidateProperties.get(value);
path = path.substring(value.length());
path = path.replace('\\', '/');
return "${" + key + "}" + path;
}
return path;
}
use of org.apache.maven.model.Profile in project maven-plugins by apache.
the class AbstractInvokerMojo method collectProjects.
/**
* Collects all projects locally reachable from the specified project. The method will as such try to read the POM
* and recursively follow its parent/module elements.
*
* @param projectsDir The base directory of all projects, must not be <code>null</code>.
* @param projectPath The relative path of the current project, can denote either the POM or its base directory,
* must not be <code>null</code>.
* @param projectPaths The set of already collected projects to add new projects to, must not be <code>null</code>.
* This set will hold the relative paths to either a POM file or a project base directory.
* @param included A flag indicating whether the specified project has been explicitly included via the parameter
* {@link #pomIncludes}. Such projects will always be added to the result set even if there is no
* corresponding POM.
* @throws org.apache.maven.plugin.MojoExecutionException If the project tree could not be traversed.
*/
private void collectProjects(File projectsDir, String projectPath, Collection<String> projectPaths, boolean included) throws MojoExecutionException {
projectPath = projectPath.replace('\\', '/');
File pomFile = new File(projectsDir, projectPath);
if (pomFile.isDirectory()) {
pomFile = new File(pomFile, "pom.xml");
if (!pomFile.exists()) {
if (included) {
projectPaths.add(projectPath);
}
return;
}
if (!projectPath.endsWith("/")) {
projectPath += '/';
}
projectPath += "pom.xml";
} else if (!pomFile.isFile()) {
return;
}
if (!projectPaths.add(projectPath)) {
return;
}
getLog().debug("Collecting parent/child projects of " + projectPath);
Model model = PomUtils.loadPom(pomFile);
try {
String projectsRoot = projectsDir.getCanonicalPath();
String projectDir = pomFile.getParent();
String parentPath = "../pom.xml";
if (model.getParent() != null && StringUtils.isNotEmpty(model.getParent().getRelativePath())) {
parentPath = model.getParent().getRelativePath();
}
String parent = relativizePath(new File(projectDir, parentPath), projectsRoot);
if (parent != null) {
collectProjects(projectsDir, parent, projectPaths, false);
}
Collection<String> modulePaths = new LinkedHashSet<String>();
modulePaths.addAll(model.getModules());
for (Profile profile : model.getProfiles()) {
modulePaths.addAll(profile.getModules());
}
for (String modulePath : modulePaths) {
String module = relativizePath(new File(projectDir, modulePath), projectsRoot);
if (module != null) {
collectProjects(projectsDir, module, projectPaths, false);
}
}
} catch (IOException e) {
throw new MojoExecutionException("Failed to analyze POM: " + pomFile, e);
}
}
use of org.apache.maven.model.Profile in project maven-plugins by apache.
the class AllProfilesMojo method execute.
// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
/** {@inheritDoc} */
public void execute() throws MojoExecutionException, MojoFailureException {
StringBuilder descriptionBuffer = new StringBuilder();
for (MavenProject project : projects) {
descriptionBuffer.append("Listing Profiles for Project: ").append(project.getId()).append(LS);
Map<String, Profile> allProfilesByIds = new HashMap<String, Profile>();
addSettingsProfiles(allProfilesByIds);
addProjectPomProfiles(project, allProfilesByIds);
// now display
if (allProfilesByIds.isEmpty()) {
getLog().warn("No profiles detected!");
} else {
// active Profiles will be a subset of *all* profiles
List<Profile> activeProfiles = project.getActiveProfiles();
for (Profile activeProfile : activeProfiles) {
// we already have the active profiles for the project, so remove them from the list of all
// profiles.
allProfilesByIds.remove(activeProfile.getId());
}
// display active profiles
for (Profile p : activeProfiles) {
descriptionBuffer.append(" Profile Id: ").append(p.getId());
descriptionBuffer.append(" (Active: true , Source: ").append(p.getSource()).append(")");
descriptionBuffer.append(LS);
}
// display inactive profiles
for (Profile p : allProfilesByIds.values()) {
descriptionBuffer.append(" Profile Id: ").append(p.getId());
descriptionBuffer.append(" (Active: false , Source: ").append(p.getSource()).append(")");
descriptionBuffer.append(LS);
}
}
}
if (output != null) {
try {
writeFile(output, descriptionBuffer);
} catch (IOException e) {
throw new MojoExecutionException("Cannot write profiles description to output: " + output, e);
}
getLog().info("Wrote descriptions to: " + output);
} else {
getLog().info(descriptionBuffer.toString());
}
}
use of org.apache.maven.model.Profile in project maven-plugins by apache.
the class AllProfilesMojo method addSettingsProfiles.
/**
* Adds the profiles from <code>settings.xml</code>.
*
* @param allProfiles Map to add the profiles to.
*/
private void addSettingsProfiles(Map<String, Profile> allProfiles) {
getLog().debug("Attempting to read profiles from settings.xml...");
for (org.apache.maven.settings.Profile settingsProfile : settingsProfiles) {
Profile profile = SettingsUtils.convertFromSettingsProfile(settingsProfile);
allProfiles.put(profile.getId(), profile);
}
}
Aggregations