Example usage for org.apache.maven.model.profile DefaultProfileInjector DefaultProfileInjector

List of usage examples for org.apache.maven.model.profile DefaultProfileInjector DefaultProfileInjector

Introduction

In this page you can find the example usage for org.apache.maven.model.profile DefaultProfileInjector DefaultProfileInjector.

Prototype

DefaultProfileInjector

Source Link

Usage

From source file:org.jetbrains.idea.maven.server.Maven30ServerEmbedderImpl.java

License:Apache License

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;
        }//from  w ww  . j  ava  2  s  .co m

        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)));
}

From source file:org.jetbrains.idea.maven.server.Maven32ServerEmbedderImpl.java

License:Apache License

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;
        }//from   w w  w.j  ava  2  s . co  m

        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)));
}

From source file:org.vesalainen.test.pom.ModelFactory.java

private Model getModel(ModelSource modelSource, ModelResolver modelResolver) {
    ModelBuildingRequest req = new DefaultModelBuildingRequest();
    req.setProcessPlugins(false);/*from  www .  j a  v  a 2 s  .c o  m*/
    req.setModelSource(modelSource);
    req.setModelResolver(modelResolver);
    req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);

    DefaultModelProcessor modelProcessor = new DefaultModelProcessor().setModelReader(new DefaultModelReader());
    DefaultSuperPomProvider superPomProvider = new DefaultSuperPomProvider().setModelProcessor(modelProcessor);
    DefaultPathTranslator pathTranslator = new DefaultPathTranslator();
    DefaultUrlNormalizer urlNormalizer = new DefaultUrlNormalizer();
    StringSearchModelInterpolator stringSearchModelInterpolator = (StringSearchModelInterpolator) new StringSearchModelInterpolator()
            .setPathTranslator(pathTranslator).setUrlNormalizer(urlNormalizer);
    DefaultModelUrlNormalizer modelUrlNormalizer = new DefaultModelUrlNormalizer()
            .setUrlNormalizer(urlNormalizer);
    DefaultModelPathTranslator modelPathTranslator = new DefaultModelPathTranslator()
            .setPathTranslator(pathTranslator);
    ModelBuilder builder = new DefaultModelBuilder()
            .setDependencyManagementImporter(new DefaultDependencyManagementImporter())
            .setDependencyManagementInjector(new DefaultDependencyManagementInjector())
            .setInheritanceAssembler(new DefaultInheritanceAssembler())
            .setModelNormalizer(new DefaultModelNormalizer()).setModelPathTranslator(modelPathTranslator)
            .setModelProcessor(modelProcessor).setModelUrlNormalizer(modelUrlNormalizer)
            .setModelValidator(new DefaultModelValidator())
            .setPluginConfigurationExpander(new DefaultPluginConfigurationExpander())
            .setPluginManagementInjector(new DefaultPluginManagementInjector())
            .setProfileInjector(new DefaultProfileInjector()).setProfileSelector(new DefaultProfileSelector())
            .setReportConfigurationExpander(new DefaultReportConfigurationExpander())
            .setReportingConverter(new DefaultReportingConverter()).setSuperPomProvider(superPomProvider)
            .setModelInterpolator(stringSearchModelInterpolator);
    try {
        return builder.build(req).getEffectiveModel();
    } catch (ModelBuildingException ex) {
        throw new RuntimeException(ex);
    }
}