Search in sources :

Example 6 with Profile

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)));
}
Also used : Activation(org.apache.maven.model.Activation) Profile(org.apache.maven.model.Profile) DefaultProfileInjector(org.apache.maven.model.profile.DefaultProfileInjector) Model(org.apache.maven.model.Model)

Example 7 with Profile

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;
}
Also used : Dependency(org.apache.maven.model.Dependency) Properties(java.util.Properties) TreeMap(java.util.TreeMap) File(java.io.File) Profile(org.apache.maven.model.Profile)

Example 8 with Profile

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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Model(org.apache.maven.model.Model) IOException(java.io.IOException) File(java.io.File) Profile(org.apache.maven.model.Profile)

Example 9 with Profile

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());
    }
}
Also used : MavenProject(org.apache.maven.project.MavenProject) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) IOException(java.io.IOException) Profile(org.apache.maven.model.Profile)

Example 10 with Profile

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);
    }
}
Also used : Profile(org.apache.maven.model.Profile)

Aggregations

Profile (org.apache.maven.model.Profile)15 Model (org.apache.maven.model.Model)5 File (java.io.File)4 MavenProject (org.apache.maven.project.MavenProject)4 Activation (org.apache.maven.model.Activation)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 Build (org.apache.maven.model.Build)2 Dependency (org.apache.maven.model.Dependency)2 DefaultProfileInjector (org.apache.maven.model.profile.DefaultProfileInjector)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MavenProfile (org.eclipse.che.maven.data.MavenProfile)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1